I estimated a robust linear model in R
with MM weights using the rlm()
in the MASS package. `R`` does not provide an $R^2$ value for the model, but I would like to have one if it is a meaningful quantity. I am also interested to know if there is any meaning in having an $R^2$ value that weighs the total and residual variance in the same way that observations were weighted in the robust regression. My general thinking is that, if, for the purposes of the regression, we are essentially with the weights giving some of the estimates less influence because they are outliers in some way, then maybe for the purpose of calculating $r^2$ we should also give those same estimates less influence?
I wrote two simple functions for the $R^2$ and the weighted $R^2$, they are below. I also included the results of running these functions for my model which is called HI9. EDIT: I found web page of Adelle Coster of UNSW that gives a formula for R2
that includes the weights vector in calculating the calculation of both SSe
and SSt
just as I did, and asked her for a more formal reference: http://web.maths.unsw.edu.au/~adelle/Garvan/Assays/GoodnessOfFit.html (still looking for help from Cross Validated on how to interpret this weighted $r^2$.)
#I used this function to calculate a basic r-squared from the robust linear model
r2 <- function(x){
+ SSe <- sum((x$resid)^2);
+ observed <- x$resid+x$fitted;
+ SSt <- sum((observed-mean(observed))^2);
+ value <- 1-SSe/SSt;
+ return(value);
+ }
r2(HI9)
[1] 0.2061147
#I used this function to calculate a weighted r-squared from the robust linear model
> r2ww <- function(x){
+ SSe <- sum((x$w*x$resid)^2); #the residual sum of squares is weighted
+ observed <- x$resid+x$fitted;
+ SSt <- sum((x$w*(observed-mean(observed)))^2); #the total sum of squares is weighted
+ value <- 1-SSe/SSt;
+ return(value);
+ }
> r2ww(HI9)
[1] 0.7716264
Thanks to anyone who spends time answering this. Please accept my apologies if there is already some very good reference on this which I missed, or if my code above is hard to read (I am not a code guy).