Bug 14840 - buglet with label for summary of weighted residuals in output from summary(lm(...))
buglet with label for summary of weighted residuals in output from summary(lm...
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Models
R 2.14.1
All All
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-03-11 20:31 UTC by Tony Plate
Modified: 2012-03-26 11:31 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 Tony Plate 2012-03-11 20:31:30 UTC
print(summary(lm(...))) prefaces its summary of residuals with "Residuals" when it should use "Weighted Residuals".

summary.lm(object) returns weighted residuals for a weighted fit, which print.summary.lm summarizes.

print.summary.lm(x) checks to see if x has a non-null component w, and if it does, it uses the title "Weighted Residuals" rather than "Residuals".

However, summary.lm(object) never includes a 'w' component in its return value, so print(summary(lm(...))) doesn't print the right heading for the residuals when weights are used in the fit.

One possible fix: 

in summary.lm, add
    ans$w <- w
after each of the 2 occurrences of
    ans$residuals <- r

(the exact name of the component could be 'w' or 'weights', but print.summary.lm looks at x$w)

Example:

> d <- data.frame(x=1:9, y=c(1:9)+c(1,2,-1,0,1,1000,0,1,-1)/10, w=c(1,0.5,2,1,2,0,1,2,1))
> summary(m <- lm(y ~ x, data=d, weights=w))

Call:
lm(formula = y ~ x, data = d, weights = w)

Residuals:
    Min      1Q  Median      3Q     Max 
-0.1883 -0.0310  0.0000  0.1006  0.1165 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.03949    0.08612   0.459    0.663    
x            0.99788    0.01502  66.419 7.83e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

Residual standard error: 0.1232 on 6 degrees of freedom
Multiple R-squared: 0.9986,     Adjusted R-squared: 0.9984 
F-statistic:  4412 on 1 and 6 DF,  p-value: 7.834e-10 

> cbind(OrdinaryResiduals=m$residuals, WeightedResiduals=summary(m)$residuals)
  OrdinaryResiduals WeightedResiduals
1        0.06263270        0.06263270
2        0.16475584        0.11649997
3       -0.13312102       -0.18826155
4       -0.03099788       -0.03099788
5        0.07112527        0.10058631
6       99.97324841        0.00000000
7       -0.02462845       -0.02462845
8        0.07749469        0.10959404
9       -0.12038217       -0.12038217
> # the printout from print(summary(m)) matches the weighted residuals
> quantile(summary(m)$residuals)
         0%         25%         50%         75%        100% 
-0.18826155 -0.03099788  0.00000000  0.10058631  0.11649997 
> quantile(m$residuals)
         0%         25%         50%         75%        100% 
-0.13312102 -0.03099788  0.06263270  0.07749469 99.97324841 
>
Comment 1 Brian Ripley 2012-03-26 11:31:45 UTC
changed for 2.15.0 patched