用`ggeffects::ggpredict`控制网格。

huangapple go评论72阅读模式
英文:

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 &lt;- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)
ggpredict(model, c(&quot;wt&quot;)) |&gt; plot()

答案1

得分: 1

ggpredict的界面有点不同寻常,因为你需要在传递给terms的字符串中,使用方括号指定预测变量的范围和密度。如果你将其设置为,比如&quot;wt [0:6 by=0.01]&quot;,预测值应该足够接近,以呈现为一个阶梯函数。

这是一个完整的reprex示例:

model <- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)

ggeffects::ggpredict(model, terms = &quot;wt [0:6 by=0.01]&quot;) |&gt; plot()

用`ggeffects::ggpredict`控制网格。

创建于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 &quot;wt [0:6 by=0.01]&quot;, the predictions should be close enough together to appear as a step function.

Here's a full reprex:

model &lt;- lm(mpg ~ cut(wt, c(0,2,4,6)), data = mtcars)

ggeffects::ggpredict(model, terms = &quot;wt [0:6 by=0.01]&quot;) |&gt; plot()

用`ggeffects::ggpredict`控制网格。

<sup>Created on 2023-07-10 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年7月10日 17:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定