Reputation: 85
HEALTH CONINC MARITAL Index MARITAL2 HAPPY
3 441 5 1 2 3
1 1764 5 1 2 2
2 3087 5 1 2 2
3 3087 5 2 2 3
1 3969 2 2 5 1
1 3969 5 2 2 3
2 4852 5 2 2 2
3 5734 3 2 3 3
Is there anyway to get liner regression of each column with respect to one column(eg, Happy) while breakdown by index?
Expected output would be Rsquare value:
HEALTH CONINC MARITAL Index MARITAL2
x x x 1 x
x x x 2 x
Tried lapply
, while not sure how to incorporate to regress by index.
Upvotes: 1
Views: 1563
Reputation: 5068
Here's how to do a single column regression by index using the dplyr
library, collecting the R-squared values as you go:
library(dplyr)
df %>% group_by(Index) %>%
do(data.frame(HEALTH = summary(lm(HEALTH ~ HAPPY, data = .))$r.squared))
So to apply it to columns 1 to 3 and 5, you can try this:
l = lapply(c(1:3,5), function(i) df %>% group_by(Index) %>%
do(data.frame(r.squared = summary(lm(as.formula(paste(colnames(df)[i], "~ HAPPY")), data = .))$r.squared)))
Upvotes: 2