R 中变量的拟合值

发布于 2025-01-11 05:40:14 字数 652 浏览 0 评论 0原文

我想查看变量的拟合值,同时仍然考虑模型中的所有其他变量。我将这些描述为部分/边缘拟合值。我下面有一个玩具示例。该数据集内置于 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 技术交流群。

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

发布评论

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

评论(2

|煩躁 2025-01-18 05:40:14

我不确定,但我认为您正在寻找效果图(或相应的数据)。

library(effects)
plot(allEffects(m1))

效果图

要获取特定值,您可以使用(例如)as.data.frame(allEffects(m1))(它返回数据的列表框架,每个框架一个预测变量)。

emmeans 包具有类似/重叠的功能。

I'm not sure, but I think you're looking for an effects plot (or the corresponding data).

library(effects)
plot(allEffects(m1))

effects plot

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.

抱着落日 2025-01-18 05:40:14

在这里,您可以获取数据框中具有 glm 模型拟合值的变量值:

data.frame(front = train$front, rear = train$rear, kms = train$kms, 
           PetrolPrice = train$PetrolPrice, VanKilled = train$VanKilled, 
           law = train$law, Fitted = fitted(m1))

Here you get the values of the variables with the fitted value of the glm model in a dataframe:

data.frame(front = train$front, rear = train$rear, kms = train$kms, 
           PetrolPrice = train$PetrolPrice, VanKilled = train$VanKilled, 
           law = train$law, Fitted = fitted(m1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文