[R] Getting a table of coefficients from R

Greg Snow 538280 at gmail.com
Thu Oct 18 19:21:57 CEST 2012


You have a list of models, the coef and confint functions only work on
a single model, so you need to use lapply or sapply to get the
information from each model.  Possibly something like (untested):

tableOfOddsRatios <- sapply( models, function(x) exp(c( coef(x)[2],
confint(x)[2,]) )

I included the [2] and [2,] to exclude the intercept from each model,
but this could be changed if you want that information as well.  You
may need to transpose the results of the above.

On Thu, Oct 18, 2012 at 9:06 AM,  <danibelle at talk21.com> wrote:
> Hi,
>
> I'm trying to obtain a table of coefficients and confidence intervals from a logistic regression analysis in R. My code is as follows:
>
> # read in csv file
>
> datafile<-read.csv("file.csv", row.names=1)
> # read in the variable list
> varlist<-names(datafile)[66:180]
> models<-lapply(varlist, function(x) {glm(substitute(outcome ~ i, list(i=as.name(x))), data=datafile, family = "binomial")})
>
> # apply summary to each model stored in the list, models
>
> lapply(models, summary)
>
> tableofOddsRatios<-exp(cbind(OR = coef(models), confint(models)))
> write.table(table, "tableofOddsRatios_outcome.txt", sep="\t", row.names=T, col.names=T, quote=F)
>
> When I try to run this, I get the following error message:
>> tableofOddsRatios<-exp(cbind(OR = coef(models), confint(models)))
> Error in UseMethod("vcov") :
>   no applicable method for 'vcov' applied to an object of class "list"
>
> Should I be doing this another way?
> Thanks
>
> Danielle
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com




More information about the R-help mailing list