[R] R-Square in WLS
frespider
frespider at hotmail.com
Fri Nov 16 04:48:35 CET 2012
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.
More information about the R-help
mailing list