Regression in R with grouped variables -
the dependent variable value
of data frame df
predicted using independent variables mean
, x
, y
in following way:
df <- df %>% group_by(country, sex) %>% do({ mod = lm(value ~ mean + x + y, data = .) <- predict(mod, .) data.frame(., a) })
data grouped country
, sex
. so, fitting formula can expressed as:
value(country, sex) = a0(country, sex) + a1(country, sex) mean + a2(country, sex) x + a3(country, sex) y
however, want use formula:
value(country, sex) = a0(country, sex) + a1(country, sex) mean + a2(country) x + a3(country) y
where a2
, a3
independent of sex
. how can it?
i don't think can when grouping country
, sex
. group country
, add interactions sex
:
df <- df %>% group_by(country) %>% do({ mod = lm(value ~ sex + mean*sex + x + y, data = .) <- predict(mod, .) data.frame(., a) })
or estimate model in 1 go adding interactions sex
, country
:
mod <- lm(value ~ sex*country*mean + country*x + country*y <- predict(mod)
Comments
Post a Comment