Re: [R] glm syntax question

From: <Bill.Venables_at_csiro.au>
Date: Wed, 16 Apr 2008 10:37:20 +1000

Henrique Dallazuanna from Curitiba writes:

>
> Try
>
> On Tue, Apr 15, 2008 at 6:07 AM, maud <maud.july@gmail.com> wrote:
>
[snip]
> >
> > (1) I need to be able to have my list of variables in the regression
> > be based from a variable vector instead of hard code.
> > (2) I need to be able to collect the significant variables from the
> > output of the regression in a vector.
> >
> > As example code consider:
> >
> > out <- glm(Var_0~Var_1+Var_2, family=binomial(link=logit),
> > data=MyData)
> >
[snip]
> >
> > For (1) I would like some code analogous to the following (which
> > doesn't work)
> >
> > VarVec <- c("Var_1","Var_2")
> > out <- glm(Var_0~VarVec, family=binomial(link=logit), data=MyData)
>
> form <- as.formula(paste("Var_0", paste(VarVec, collapse="+"), sep = "
~ "))
> out <- glm(form, family=binomial(link=logit), data = MyData)

OK this works for fitting models, but it has a big drawback in that if you get rid of the variable 'form' you cannot predict from it. The fitted model object comes in two bits and that's not a good idea.

You can repair things in a fairly ugly way, namely

out$call$formula <- form

and the trap is unset once more. This really points to the need for a generic function, "formula<-", to make this a more natural and safer operation itself. It would not be needed all that often, though...

> >
> > For (2) I would like to be able to access the table shown above
(which
> > is only part of what summary(out) displays). I'd like something like
> >
>
> SigVars <- summary(out)$coefficients[,4]
> SigVars[SigVars < .001]
>

This is a bit picky, but I would use the column label to make clear which one I wanted:

SigVars <- summary(out)$coefficients[, "P(>|z|)", drop = FALSE] rownames(SigVars)[SigVars < 0.001]

The drop = FALSE ensures it stays as a matrix, keeping its row names, as presumably you want to know which ones are significant by name.

>
> >
> > table <- summary(out)
> > SigVars <- table[Pr(>|z|) < .001]
> >
> > that is collect all of the variables with a Pr(>|z|) value less than
.
> > 001.

Bill Venables.



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. Received on Wed 16 Apr 2008 - 00:45:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 16 Apr 2008 - 01:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive