Bug 17313 - Error in the sigma() function
Summary: Error in the sigma() function
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Models (show other bugs)
Version: R 3.3.*
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2017-07-22 14:03 UTC by Terry Therneau
Modified: 2017-07-25 15:11 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Terry Therneau 2017-07-22 14:03:33 UTC
The manual page for sigma() claims that, for a linear model at least, the result is the same as summary(fit)$sigma.  This is untrue if the lm fit has NA coefficients.  The problem is that sigma.default uses the length of coef as a surrogate for degrees of freedom, it should use the number of non NA coefficients.

Simple example:
temp <- data.frame(x1 = LETTERS[c(1,2,3,1,2,3,1,2,3)],
                   x2 = letters[c(1,2,1,2,1,1,1,2,1)],
                   y =  1:9)
fit <- lm(y ~ x1*x2, data=temp)

sigma(fit)   # 4.24
summary(fit)$sigma  # 3.67
Comment 1 Martin Maechler 2017-07-22 18:32:40 UTC
You are right.

I tend to agree that probably
   length(coef(object))
should be changed to
   sum(!is.na(coef(object)))
  
but  I do not start any action right now...
Comment 2 Martin Maechler 2017-07-25 15:11:23 UTC
fixed in both R-devel and (R 3.4.1)-patched