R programming language resources › Forums › Statistical analyses › Sequential Sum of Squares Code
- This topic has 1 reply, 2 voices, and was last updated 12 years, 8 months ago by bryan.
- AuthorPosts
- December 31, 2011 at 2:11 pm #300TabbyMember
Hello,
I am new to r and programming in general. I cant figure out why the following code is not working.
###############################################################################################
#Sequentail Sum of Squares--------------------------------------------------------------------#
###############################################################################################SS.seq = function(y,predictor,all.predictors)
{SS = vector(,length=all.dim(predictors)[2])
mean = mean(y)
yhat = lm(y~all.predictors[,1])sum.RSS.full = 0 #initialize sum#
sum.RSS.red = 0 #initialize sum#
sum.SSyy = 0 #initialize sum$for (i in 1:length(y)) #calculate RSS#
{sum.RSS.full = (yhat.full[i]-y[i])^2 + sum.RSS.full
sum.RSS.red = (yhat.red[i]-y[i])^2 + sum.RSS.red
sum.SSyy = (y[i]-mean)^2 + sum.SSyy}
SS.seq = sum.RSS.red-sum.RSS.full
SSyy = sum.SSyy
SS = 100*(SS.seq/SSyy)cat("SS1: ",SS.seq) #print result#
cat("\n")for (i in 2:dim(predictors)[2])
{yhat.full = lm(y~all.predictors[,i])
yhat.red = lm(y~subset(all.predictors,c(1:i-1,i+1:dim(predictors)[2]))sum.RSS.full = 0 #initialize sum#
sum.RSS.red = 0 #initialize sum#
sum.SSyy = 0 #initialize sum#for (j in 1:length(y)) #calculate RSS#
{sum.RSS.full = (yhat.full[j]-y[j])^2 + sum.RSS.full
sum.RSS.red = (yhat.red[j]-y[j])^2 + sum.RSS.red
sum.SSyy = (y[j]-mean)^2 + sum.SSyy
}SS.seq[i] = sum.RSS.red-sum.RSS.full
SSyy[i] = sum.SSyy
SS[i] = 100*(SS.seq/SSyy)}
cat("Sequential Sum of Sqaures: ",SS)
}
###############################################################################################
#Sequentail Sum of Squares--------------------------------------------------------------------#
###############################################################################################
I am not sure if my loops are correct.Any help would be great.
Thansk
January 3, 2012 at 2:45 pm #318bryanParticipantTabby,
What error(s) are you getting?
- AuthorPosts
- You must be logged in to reply to this topic.