ggeffect() 不返回所有预测

发布于 2025-01-10 19:16:09 字数 916 浏览 0 评论 0原文

使用安全带(包含在 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")

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2025-01-17 19:16:09

在底层,函数ggeffect使用effects库中的Effect函数来获取用于绘图的数据。如果您有兴趣查看特定变量各个级别的预测实际数字,最好直接获取它们,因为返回任意预测列表不是 ggeffect 的工作。

我们可以通过 xlevels 参数传递您选择的任何级别的预测和置信区间的良好数据框。

predictions <- effects::Effect("front", m1, xlevels = list(front = seq(400, 1300, 5)))

df <-  data.frame(front = predictions$x, 
                  fit   = exp(predictions$fit),
                  lower = exp(predictions$lower),
                  upper = exp(predictions$upper))

因此,我们的数据框顶部如下所示:

head(df)
#>   front      fit    lower    upper
#> 1   400 68.16168 67.99573 68.32803
#> 2   405 68.62226 68.45703 68.78788
#> 3   410 69.08595 68.92146 69.25083
#> 4   415 69.55277 69.38904 69.71689
#> 5   420 70.02275 69.85979 70.18609
#> 6   425 70.49590 70.33373 70.65845

reprex 包于 2022 年 3 月 1 日创建 (v2.0.1)

Under the hood, the function ggeffect uses the Effect function from the effects 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 of ggeffect 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.

predictions <- effects::Effect("front", m1, xlevels = list(front = seq(400, 1300, 5)))

df <-  data.frame(front = predictions$x, 
                  fit   = exp(predictions$fit),
                  lower = exp(predictions$lower),
                  upper = exp(predictions$upper))

So the top of our data frame looks like this:

head(df)
#>   front      fit    lower    upper
#> 1   400 68.16168 67.99573 68.32803
#> 2   405 68.62226 68.45703 68.78788
#> 3   410 69.08595 68.92146 69.25083
#> 4   415 69.55277 69.38904 69.71689
#> 5   420 70.02275 69.85979 70.18609
#> 6   425 70.49590 70.33373 70.65845

Created on 2022-03-01 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文