# Sequential Sum of Squares Code

R programming language resources Forums Statistical analyses Sequential Sum of Squares Code

Viewing 2 posts - 1 through 2 (of 2 total)
• Author
Posts
• #300
Tabby
Member

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

#318
bryan
Participant

Tabby,

What error(s) are you getting?

Viewing 2 posts - 1 through 2 (of 2 total)
• You must be logged in to reply to this topic.
Scroll to top