ggeffect() 不返回所有预测
使用安全带(包含在 R 中)数据,我想要预测“前”的边际效应。该变量有 170 个值。 ggeffect() 仅返回值 400、500、600、...、1300 处的 front 预测。我怎样才能返回所有的预测?
Seatbelts <- data.frame(Seatbelts)
head(Seatbelts)
Seatbelts <- Seatbelts[complete.cases(Seatbelts), ]
## 75% of the sample size
smp_size <- floor(0.75 * nrow(Seatbelts))
## set the seed to make your partition reproducible
set.seed(123)
train_ind <- sample(seq_len(nrow(Seatbelts)), size = smp_size)
train <- Seatbelts[train_ind, ]
test <- Seatbelts[-train_ind, ]
# glm()
m1 <- glm(DriversKilled ~ front + rear + kms + PetrolPrice +
VanKilled + law,
family=poisson(link = "log"),
weights = drivers,
data=train)
ggeffect(m1, terms = c("front"), typical = "average")
Using the Seatbelts (included in R) data, I want the predictions for the marginal effect of "front". This variable has 170 values. ggeffect()
is only returning predictions for front at the values 400, 500, 600, ...,1300. How can I return all of the predictions?
Seatbelts <- data.frame(Seatbelts)
head(Seatbelts)
Seatbelts <- Seatbelts[complete.cases(Seatbelts), ]
## 75% of the sample size
smp_size <- floor(0.75 * nrow(Seatbelts))
## set the seed to make your partition reproducible
set.seed(123)
train_ind <- sample(seq_len(nrow(Seatbelts)), size = smp_size)
train <- Seatbelts[train_ind, ]
test <- Seatbelts[-train_ind, ]
# glm()
m1 <- glm(DriversKilled ~ front + rear + kms + PetrolPrice +
VanKilled + law,
family=poisson(link = "log"),
weights = drivers,
data=train)
ggeffect(m1, terms = c("front"), typical = "average")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在底层,函数
ggeffect
使用effects
库中的Effect
函数来获取用于绘图的数据。如果您有兴趣查看特定变量各个级别的预测实际数字,最好直接获取它们,因为返回任意预测列表不是 ggeffect 的工作。我们可以通过 xlevels 参数传递您选择的任何级别的预测和置信区间的良好数据框。
因此,我们的数据框顶部如下所示:
由 reprex 包于 2022 年 3 月 1 日创建 (v2.0.1)
Under the hood, the function
ggeffect
uses theEffect
function from theeffects
library to get its data for plotting. If you are interested in seeing the actual numbers predicted at various levels of a particular variable it would be best to get them directly, since it is not the job ofggeffect
to return an arbitrary list of predictions.We can get a nice data frame of the prediction and confidence interval at any levels you choose by passing them via the
xlevels
parameter.So the top of our data frame looks like this:
Created on 2022-03-01 by the reprex package (v2.0.1)