In R, when using lm()
, if I set na.action = na.pass
inside the call to lm()
, then in the summary table there is an NA for any coefficient that cannot be estimated (because of missing cells in this case).
If, however, I extract just the coefficients from the summary object, using either summary(myModel)$coefficients
or coef(summary(myModel))
, then the NA's are omitted.
I want the NA's to be included when I extract the coefficients the same way that they are included when I print the summary. Is there a way to do this?
Setting options(na.action = na.pass)
does not seem to help.
Here is an example:
> set.seed(534)
> myGroup1 <- factor(c("a","a","a","a","b","b"))
> myGroup2 <- factor(c("first","second","first","second","first","first"))
> myDepVar <- rnorm(6, 0, 1)
> myModel <- lm(myDepVar ~ myGroup1 + myGroup2 + myGroup1:myGroup2)
> summary(myModel)
Call:
lm(formula = myDepVar ~ myGroup1 + myGroup2 + myGroup1:myGroup2)
Residuals:
1 2 3 4 5 6
-0.05813 0.55323 0.05813 -0.55323 -0.12192 0.12192
Coefficients: (1 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.15150 0.23249 -0.652 0.561
myGroup11 0.03927 0.23249 0.169 0.877
myGroup21 -0.37273 0.23249 -1.603 0.207
myGroup11:myGroup21 NA NA NA NA
Residual standard error: 0.465 on 3 degrees of freedom
Multiple R-squared: 0.5605, Adjusted R-squared: 0.2675
F-statistic: 1.913 on 2 and 3 DF, p-value: 0.2914
> coef(summary(myModel))
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.15149826 0.2324894 -0.6516352 0.5611052
myGroup11 0.03926774 0.2324894 0.1689012 0.8766203
myGroup21 -0.37273117 0.2324894 -1.6032180 0.2072173
> summary(myModel)$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.15149826 0.2324894 -0.6516352 0.5611052
myGroup11 0.03926774 0.2324894 0.1689012 0.8766203
myGroup21 -0.37273117 0.2324894 -1.6032180 0.2072173