'Fitted Values by Variable in R
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)
Solution 1:[1]
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))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quinten |
