[R] R-Square in WLS

peter dalgaard pdalgd at gmail.com
Mon Nov 19 08:26:05 CET 2012


On Nov 18, 2012, at 21:32 , Thomas Lumley wrote:

> On Fri, Nov 16, 2012 at 4:48 PM, frespider <frespider at hotmail.com> wrote:
> 
>> Hi,
>> 
>> I am fitting a weighted least square regression and trying to compute
>> SSE,SST and SSReg but I am not getting SST = SSReg + SSE and I dont know
>> what I am coding wrong. Can you help please?
>> 
> 
> 
> For a start, you need to replace your mu and muZ by weighted means.

The -1 in the model formulas also suggests that there will be problems even in the non-weighted case. The addition formula for SSDs works for successive model reductions, so it is required that the span of the design matrix X contains the vector of all ones.

> 
>    -thomas
> 
> [snip]
> 
>> ############################## Y = Log(Z) Scale
>> ####################################
>> Yhat <- X%*%bhat                 # predicted values
>> mu <- mean(Y)
>> To <- Y - mu
>> Er <- Y - Yhat
>> Re <- Yhat - mu
>> lgSST <- sum(Weights*(To)^2)                # log SST
>> lgSSE <- sum(Weights*(Er)^2)                # log SSE
>> lgSSR <- sum(Weights*(Re)^2)                # log SSR
>> lgR-sq <- lgSSR/lgSST
>> ###############################  Z Scale
>> ######################################
>> Z <- exp(Y)
>> muZ <- mean(Z)
>> Zhat <- exp(Yhat+0.5*Sigma2)
>> ToZ <- Z-muZ
>> ErZ <- Z - Zhat
>> ReZ <- Zhat - muZ
>> SST <- sum(Weights*(ToZ)^2)          # SST
>> SSE <- sum(Weights*(ErZ)^2)          # SSE
>> SSR <- sum(Weights*(ReZ)^2)          # SSR
>> Rsq <- SSR/SST
>> 
>> I don't understand what is wrong with the code.  The sum square regression
>> plus the sum square error do not add up to the sum square total in both the
>> Y scale and Z scale.  Y is a normal distribution and Z is log normally
>> distributed.  Where is the error?
>> Also, is there a way to calculate the weighted sum square?
>> 
>> 
> 
>   -thomas
> 
> -- 
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com




More information about the R-help mailing list