英文:
Controlling the grid with `ggeffects::ggpredict`
问题
假设我拟合了一个具有连续预测变量的线性模型,该变量已被分组化。现在我想绘制均值结果作为连续预测变量的函数。这将是一个阶梯函数。我可以通过手动指定一个非常精细的网格并使用stats::predict()来创建这个图表。然而,我对ggeffects::ggpredict()是否能够实现这一点感兴趣。下面的示例图表表明使用的网格不够精细。是否有任何方法来控制网格?
model <- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)
ggpredict(model, c("wt")) |> plot()
英文:
Suppose I fit a linear model with a continuous predictor that has been categorized. Now I want to make a plot of the mean outcome as a function of the continuous predictor. This would be a staircase function. I could create this manually by specifying a very fine grid and use stats::predict(). I'm however interested in whether this is is possible with ggeffects::ggpredict(). The plot in the example below suggests that the grid used isn't fine enough. Is there any way to control the grid?
model <- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)
ggpredict(model, c("wt")) |> plot()
答案1
得分: 1
ggpredict的界面有点不同寻常,因为你需要在传递给terms的字符串中,使用方括号指定预测变量的范围和密度。如果你将其设置为,比如"wt [0:6 by=0.01]",预测值应该足够接近,以呈现为一个阶梯函数。
这是一个完整的reprex示例:
model <- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)
ggeffects::ggpredict(model, terms = "wt [0:6 by=0.01]") |> plot()
创建于2023-07-10,使用 reprex v2.0.2
英文:
The interface for ggpredict is a bit unusual, in that you specify the range and density of the predictor variables inside square brackets as part of the string passed to terms. If you set this to, say "wt [0:6 by=0.01]", the predictions should be close enough together to appear as a step function.
Here's a full reprex:
model <- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)
ggeffects::ggpredict(model, terms = "wt [0:6 by=0.01]") |> plot()
<sup>Created on 2023-07-10 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论