嗨:我有六个连续因变量和三个国家/地区的一个自变量。我想看看每个国家的 y1 到 y6 ~ x1 的系数是多少。有没有办法用 dplyr 和 broom 巧妙地做到这一点?我对 dplyr 相当了解,但对扫帚很陌生。
#one random independent variable
x1<-rnorm(100, mean=5, sd=1)
#one random dependent variable
y1<-rnorm(100, mean=2, sd=2)
#two random dependent variables, in reality I have six
y2<-rnorm(100, mean=3, sd=1)
#Grouping variable.
country<-sample(seq(1,3,1), size=100, replace=T)
#data frame
df<-data.frame(x1, y1, y2, country)
#I would like to see what the coefficients are for y1~x1
and then y2 ~x2 for country 1, country 2, country 3, etc.
library(dplyr)
#Fit one model for each of three countries
test<-df%>%
group_by(country) %>%
do(mod.y1=lm(y1~x1, data=.))
#print results
test$mod.y1