Print R-Squared for All of the Models Fit with Lmlist

Print R-squared for all of the models fit with lmList

Here you go:

sapply(fit,function(x) summary(x)$r.squared)
11 12
0.9657143 0.9657143

Or to do everything at once:

sumfun <- function(x) c(coef(x),summary(x)$r.squared)
t(sapply(fit,sumfun))

(you need to transpose the results from sapply to get the table as specified above). Then use names() <- or setNames() to get the column names the way you want them.

Extract Adj R Square from a list of lm results

Sounds like Filter() could some in handy here

bb <- Filter(function(x) {
summary(x)$adj.r.squared > .7 & all(vif(x) <4)
}, b)

You just pass it a function to tell it which objects you want to keep and the list if items you want to filter.

Extract Adj R Square from a list of lm results

Sounds like Filter() could some in handy here

bb <- Filter(function(x) {
summary(x)$adj.r.squared > .7 & all(vif(x) <4)
}, b)

You just pass it a function to tell it which objects you want to keep and the list if items you want to filter.

Extracting a list of R2 from within lm() based on variable in multiple regression in R

The point is r.squared is stored in summary(lm(...)) not in lm(...). Here is another version of your function to extract R2:

library(plyr)
df <- iris
#Create MR function for extracting coefficients and R2
getCoef <- function(df) {
model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = df)
coefs <- model$coef
names(coefs) <- c("intercept", "Sepal.Width", "Petal.Length", "Petal.Width")
R2 <- summary(model)$r.squared
names(R2) <- c("R2")
c(coefs, R2)
}
#Extract coefficients and R2 for each Species
coefs.MR_uM <- ddply(df, ~ Species, getCoef)
coefs.MR_uM # output
Species intercept Sepal.Width Petal.Length Petal.Width R2
1 setosa 2.351890 0.6548350 0.2375602 0.2521257 0.5751375
2 versicolor 1.895540 0.3868576 0.9083370 -0.6792238 0.6050314
3 virginica 0.699883 0.3303370 0.9455356 -0.1697527 0.7652193

As suggested by Parfait, you don't need plyr::ddply(), you can use do.call(rbind, by(df, df$Species, getCoef))

Hope this helps !

Writing loop/function to generate various linear regressions on same dataframe

Here is a base R approach to the problem with lapply loops.

First if you want to automatically extract the variable names ending in _2 which should be all of your dependent variables you could implement the following code:

dep_vars<-grep("_2$",colnames(dataset),value = T) #This selects all variables ending in `_2` which should all be dependent variables.

reg_vars<-gsub("_2$","",dep_vars) #This removes the `_2` from the dependent variables which should give you the common stem which can be used to select both dependent and independent variables from your data frame.

Then you can use this in your lapply loop for creating your formulas:

full_results <- lapply(reg_vars, function(i) summary(lm(paste0("log(",i,"_2)~",i,"_1+AGE"),data=dataset)))

Now you will have a list of linear regression summaries where you can extract the info you want. I think this is what you want for the output but please clarify if not:

print_results<-lapply(full_results,function(i) data.frame(
Dep_va = names(attributes(i[["terms"]])$dataClasses)[1],
Ind_var = names(attributes(i[["terms"]])$dataClasses)[2],
Covariates = names(attributes(i[["terms"]])$dataClasses)[3],
Pvalue = i[["coefficients"]][2,4],
upper.cI = i[["coefficients"]][2,1]+1.96*i[["coefficients"]][2,2],
low.cI = i[["coefficients"]][2,1]-1.96*i[["coefficients"]][2,2]))

That code will give you a list of data frames and if you want to combine it into one data.frame:

final_results<-do.call("rbind",print_results)

Output Results:

Dep_va Ind_var Covariates     Pvalue upper.cI     low.cI
1 A_2 A_1 AGE 0.25753805 1.113214 -0.1877324
2 B_2 B_1 AGE 0.68452053 1.211355 -1.9292236
3 C_2 C_1 AGE 0.04827506 1.497688 0.3661343

Hope that helps! Let me know if you were looking for different output results.

How to get r.squared for each regression?

r.squared can be accessed via the summary of the model, try this:

m <- lm(y ~ x)
rs <- summary(m)$r.squared

The summary object of the linear regression result contains almost everything you need:

output_reg<-list() 
B<-(unique(my_df$field))
for (i in 1:length(B)) {
index <- my_df[my_df$field==B[i],]
x<- index$predictor
y<- index$response
m <- lm (y ~ x)
s <- summary(m) # get the summary of the model
# extract every thing you need from the summary object
output_reg[[i]] <- c(s$coefficients[, 'Estimate'], r.squared = s$r.squared)
}
output_reg
#[[1]]
#(Intercept) x r.squared
# 10.7537594 -1.3195489 0.3176692

#[[2]]
#(Intercept) x r.squared
# 8.8473684 -0.3368421 0.1389040

#[[3]]
#(Intercept) x r.squared
#-0.30500000 0.35963455 0.03788593

To bind the result together:

do.call(rbind, output_reg)
# (Intercept) x r.squared
# [1,] 10.753759 -1.3195489 0.31766917
# [2,] 8.847368 -0.3368421 0.13890396
# [3,] -0.305000 0.3596346 0.03788593

dplyr version of grouping a dataframe then creating regression model on each group

Returning a list from dplyr is not possible yet. If you just need the intercept and slope @jazzurro 's answer is the way, but if you need the whole model you need to do something like

library(dplyr)
models <- df %>% group_by(country) %>% do(mod = lm(BirthRate ~ US., data = .))

Then if you want to perform ANOVA on each fitted model, you can do it using rowwise

models %>% rowwise %>% do(anova(.$mod))

but again the result is coerced to a data frame and is not quite the same as doing lapply(models$mod, anova).

For now (ie until the next version of dplyr) if you need to store the whole result in a list, you can just use dlply from plyr, like plyr::dlply(df, "country", function(d) anova(lm(BirthRate ~ US., data = d))), or of course if you do not absolutely have to use dplyr you can go for @SvenHohenstein 's answer which looks like a better way of doing this anyway.

How do you get stargazer to recognise a model from lmList?

It should work fine with lmList() from the nlme package (not the one from the lme4). Try out:

fit1 <- nlme::lmList((lndeltaoms) ~ size + factor(gender)| year, data = tser)
stargazer(fit1[["2008"]]) # ok

fit2 <- lme4::lmList((lndeltaoms) ~ size + factor(gender)| year, data = tser)
stargazer(fit2[["2008"]]) # this does not work

It looks like stargazer() works fine with objects of class lmList but not with lmList4 object resulting from lme4::lmList().

Also, be careful while loading nlme since its function lmList() is masked from lme4::lmList().



Related Topics



Leave a reply



Submit