本期回答一下上期中小彩蛋
部分的问题,如下:
Q: 不同的
department
的base
不同,raise
也不同,我们得出不同的α
和β
。 可否等价为,先按照department
分组,然后分别计算α
和β
。 A: 不等价!
rm(list = ls())
library(tidyverse)
library(lme4)
library(modelr)
library(broom)
library(ggsci)
library(broom.mixed)
数据描述的是不同部门(department
)的老师的收入(salary
)情况。
具体可见上期。
m1 <- lmer(salary ~ experience + (1 + experience | department), data = df)
m1
broom.mixed::tidy(m1, effects = "ran_vals")
df1 <- df %>%
add_predictions(m1)
df1
p1 <- df1 %>%
ggplot(aes(
x = experience, y = salary, group = department,
colour = department
)) +
geom_point() +
geom_line(aes(x = experience, y = pred)) +
labs(x = "Experience", y = "Predicted Salary") +
ggtitle("Varying Intercept and Slopes Salary Prediction") +
scale_color_npg()
p1
这里我们使用nest
函数容纳一下建模的大量数据。这里我就不做具体展示了,大家可以自己试一下。
m2 <- df %>%
group_by(department) %>%
nest() %>%
mutate(mdl = map(data, ~ lm(salary ~ 1 + experience, data=.))) %>%
mutate(fit = map(mdl, ~ .$fitted.values))
m2
df2 <- m2 %>%
mutate(., data = map2(data, mdl, add_predictions)) %>%
select(., -mdl, -fit) %>%
unnest()
df2
p2 <- df2 %>%
ggplot(aes(
x = experience, y = salary, group = department,
colour = department
)) +
geom_point() +
geom_line(aes(x = experience, y = pred)) +
labs(x = "Experience", y = "Predicted Salary") +
ggtitle("Varying Intercept and Slopes Salary Prediction") +
scale_color_npg()
p2
差异不是很大,但还是不同的。🤣
df3 <- df1 %>%
dplyr::select(.,ids,pred) %>%
left_join(.,df2[,c(2,7)],by = "ids")
df3
library(patchwork)
p1 + p2
Note! 大家不要认为差异不大就随便选用建模方法,当我们纳入更多变量的时候,可能不同建模方式的差异就会显现啦!🥰
最后祝大家早日不卷!~