Re: [R] question re: "summarry.lm" and NA values

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 16 Aug 2006 - 01:40:03 EST

On Tue, 15 Aug 2006, Petr Pikal wrote:

> Hi
>
> On 15 Aug 2006 at 7:01, r user wrote:
>
> Date sent: Tue, 15 Aug 2006 07:01:13 -0700 (PDT)
> From: r user <ruser2006@yahoo.com>
> To: rhelp <r-help@stat.math.ethz.ch>
> Subject: [R] question re: "summarry.lm" and NA values
>
> > Is there a way to get the following code to include
> > NA values where the coefficients are NA ?
> >
> > ((summary(reg))$coefficients)
>
> better
> coef(reg)

coef(summary(reg)), perhaps.

> > explanation:
> >
> > Using a loop, I am running regressions on several
> > subsets of data1 .
> >
> > reg <- ( lm(lm(data1[,1] ~., data1[,2:l])) )
> >
> > My regression has 10 independent variables, and I
> > therefore expect 11 coefficients.
> > After each regression, I wish to save the coefficients
> > and standard errors of the coefficients in a table
> > with 22 columns.
> >
> > I successfully extract the coefficients using the
> > following code:
> > reg$coefficients

> >
> > I attempt to extract the standard errors using :
> >
> > aperm((summary(reg))$coefficients)[2,]
> >
> > ((summary(reg))$coefficients)
> >
> > My problem:
> > For some of my subsets, I am missing data for one or
> > more of the independent variables. This of course
> > causes the coefficients and standard erros for this
> > variable to be NA .
>
> ??%^&*^??
>
> What version? My lm behaves in accordance with na.action and it
> throws an error in case na.fail, computes a value in case of na.omit
> or na.exclude and again throws an error if the variable consist
> exclusively from NA values.
>
> The only way how to get NA in coeficient is when a variable is either
> constant or linear combination of other variable(s). Then
> coef(reg)
> will give you correctly NA in the variable which appears constant and
> in this case you could use it for setting standard error also as NA
> let say by using ifelse statement and matching of names.

That happens in the print method, stats:::print.summary.lm contains

        coefs <- x$coefficients
        if (!is.null(aliased <- x$aliased) && any(aliased)) {
            cn <- names(aliased)
            coefs <- matrix(NA, length(aliased), 4, dimnames = list(cn,
                colnames(coefs)))
            coefs[!aliased, ] <- x$coefficients
        }

so the code is already available

> >
> > Is there a way to include the NA standard errors, so
> > that I have the same number of standard erros and
> > coefficients for each regression, and can then store
> > the coefficients and standard erros in my table of 22
> > columns?
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch 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.
>
> Petr Pikal
> petr.pikal@precheza.cz
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch 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 Aug 16 01:52:39 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 16 Aug 2006 - 02:22:47 EST.

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