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? xnam <-colnames(X) # colnames Design Matrix fmla1 <- as.formula(paste("Y ~",paste(xnam, collapse= "+"),"-1",sep="")) fitlm <- lm(formula=fmla1,data = data.frame(cbind(X,Y))) ResiSqr <- (residuals(fitlm))*(residuals(fitlm)) fmla2 <- as.formula(paste("ResiSqr ~ ", paste(xnam, collapse"+"),"-1",sep="")) fitResi <- lm(formula=fmla2,data = data.frame(cbind(x,ResiSqr))) # This fit is to calculate the weights bResi <- coef(fitResi) Sigma2 <- X%*%bResi # Var(Y) Weights <- 1/Sigma2 for(i in 1:length(Weights)){ Weights[which(ifelse(Weights<=0.5,TRUE,FALSE))] <- 0.5 Weights[which(ifelse(Weights>=200,TRUE,FALSE))] <- 200 Sigma2[which(ifelse(Sigma2<=0,TRUE,FALSE))] <- 0 # set -ve Var(Y) to zero #+++++++++++++++++++++++++++ Fit WLS +++++++++++++++++++++++++++++++++++++++++++ fitwls <- lm(formula = fmla1,weights = Weights,data data.frame(cbind(X,Y))) bhat <- coef(fitwls) ############################## 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? -- View this message in context: http://r.789695.n4.nabble.com/R-Square-in-WLS-tp4649693.html Sent from the R help mailing list archive at Nabble.com.
On Fri, Nov 16, 2012 at 4:48 PM, frespider <frespider@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. -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]]
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