R 中变量的拟合值
我想查看变量的拟合值,同时仍然考虑模型中的所有其他变量。我将这些描述为部分/边缘拟合值。我下面有一个玩具示例。该数据集内置于 R 中。这可能吗?
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"),
data=train)
fitted(m1)
I want to see the fitted values by variable while still taking into account all other variables in the model. I would describe these as partial/marginal fitted values. I have a toy example below. This dataset is built into R. Is this possible?
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"),
data=train)
fitted(m1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定,但我认为您正在寻找效果图(或相应的数据)。
要获取特定值,您可以使用(例如)
as.data.frame(allEffects(m1))
(它返回数据的列表框架,每个框架一个预测变量)。emmeans
包具有类似/重叠的功能。I'm not sure, but I think you're looking for an effects plot (or the corresponding data).
To get the specific values, you could use (e.g.)
as.data.frame(allEffects(m1))
(which returns a list of data frames, one for each predictor variable).The
emmeans
package has similar/overlapping functionality.在这里,您可以获取数据框中具有 glm 模型拟合值的变量值:
Here you get the values of the variables with the fitted value of the glm model in a dataframe: