Model 1: SSreg(X1 | X2)

# Full model

f.x1 <- model.matrix(Y ~ X1 + X2) # Design matrix for x (Predictors)

f.y1 <- as.matrix(cementdata[,1]) # Response variable

betas.fullmodel1 <- solve(crossprod(f.x1))%*%crossprod(f.x1,f.y1) # Beta values

anova.fullmodel1 <- anova(lm(Y ~ X1 + X2)) # Fitting the full model

fullmodel1.rsquare <- (1450.1 + 1207.8)/(1450.1 + 1207.8 + 57.9)  # R-Square for full model

fullmodel1.adjrsquare <- 1 - (57.9/10)/(2715.8/12) # Adjusted R-Sqaure for the full model


# Restricted model

r.x1 <- model.matrix(Y ~ X2) # Design matrix for x (Predictors)

r.y1 <- as.matrix(cementdata[,1]) # Response variable

betas.restrictemodel1 <- solve(crossprod(r.x1))%*%crossprod(r.x1,r.y1) # Beta values

anova.restrictemodel1 <- anova(lm(Y ~ X2)) # Fitting the restricted model

resmodel1.rsqaure <-  1809.43 / ( 1809.43 + 906.34) # R-Square for the restricted model

resmodel1.adjrsqaure <- 1 - (82.39455/226.3142) # Ajusted R-Square for the restricted model


# Sum of squares

ssreg.model1 <- (1450.1 + 1207.8) - 1809.43 # SSreg(X1, X2) - SSreg(X2)
sserror.model <- ( 906.34 - 57.9) # SSerror(X2) - SSerror(X1, X2)

Model 2: SSreg(X1,X2 | X3,X4)

# Full model

f.x2 <- model.matrix(Y ~ X1 + X2 + X3 + X4)

f.y2 <- as.matrix(cementdata[,1]) # Response variable

betas.fullmodel2 <- solve(crossprod(f.x2))%*%crossprod(f.x2,f.y2) # Beta values

anova.fullmodel2 <- anova(lm(Y ~ X1 + X2 + X3 + X4)) # Fitting the full model

fullmodel2.rsquare <- (1450.08 + 1207.78 + 9.79 + 0.25) / (1450.08 + 1207.78 + 9.79 + 0.25+ 47.86)  # R-Square for full model

fullmodel2.adjrsquare <-  1 - (5.9825 / 226.3133) # Adjusted R-Sqaure for the full model


# Restricted model

r.x2 <- model.matrix(Y ~ X3 + X4)

r.y2 <- as.matrix(cementdata[,1]) # Response variable

betas.restrictemodel2 <- solve(crossprod(r.x2))%*%crossprod(r.x2,r.y2) # beta values

anova.restrictemodel2 <- anova(lm(Y ~ X3 + X4)) # Fitting the restricted model

resmodel2.rsqaure <- (776.36 + 1763.66) / (776.36 + 1763.66 + 175.74 )# R-Square for the restricted model

resmodel2.adjrsqaure <-  1 - (17.574 / 226.3133 ) # Ajusted R-Square for the restricted model
  
  
# Sum of squares

ssreg.model2 <- 2667.9 - 2540.02 # SSreg(X1, X2, X3, X4) - SSreg(X3, X4)
sserror.model2 <- 175.74 - 47.86  # SSerror(X3, X4) - SSerror(X1, X2, X3, X4)

Model 3: SSreg(X1 | X2, X3)

# Full Model 

f.x3 <- model.matrix(Y ~ X1 + X2 + X3) # Design matrix for the predictors

f.y3 <- as.matrix(cementdata[,1]) # Response variable

betas.fullmodel3 <- solve(crossprod(f.x3))%*%crossprod(f.x3,f.y3) # Beta values

anova.fullmodel3 <- anova(lm(Y ~ X1 + X2 + X3)) # Fitting the full model

fullmodel3.rsquare <- (1450.08 + 1207.78 + 9.79) / (1450.08 + 1207.78 + 9.79 +  48.11) # R-Square for full model

fullmodel3.adjrsquare <- 1 - (5.345556/226.3133) # Adjusted R-Sqaure for the full model


#Restricted model

r.x3 <- model.matrix(Y ~ X2 + X3) # Design matrix for predictors

r.y3 <- as.matrix(cementdata[,1]) # Response variable

betas.restrictemodel3 <- solve(crossprod(r.x3))%*%crossprod(r.x3,r.y3)  # beta values

anova.restrictemodel3 <- anova(lm(Y ~ X2 + X3))  # Fitting the restricted model

resmodel3.rsqaure <- (1809.43 + 490.89)/(1809.43 + 490.89 + 415.44) # R-Square for the restricted model

resmodel3.adjrsqaure <- 1 - (41.544 / 226.3133) # Ajusted R-Square for the restricted model


# Sum of squares

ssreg.model3 <- 2667.65 - 2300.32 # SSreg(X1, X2, X3) - SSreg(X2, X3)
sserror.model3 <- 415.44 - 48.11 # SSerror(X2, X3) - SSerror(X1, X2, X3) 

Inference

When a variable is added to the model, if it reduces the sum of sqaures of error or alternatively, increases the sum of squares of regression, implies that the variable (predictor) when added to the model helps in explaining the variablity in the response varaible.

In the below table,

  1. For model 1, when X1 is added to the model while X2 is already present in the model, the sum of squares of errors reduce by 848.47

  2. For model 2, when X1 and X2 are added to the model while X3 and X4 are already present in the model, the sum of squares of errors reduce by 127.88

  3. For model 3, when X1 is added to the model while X2 and X3 are already present in the model, the sum of sqaures of errors reduced by 367.33

results <- data.frame(Model = c("Model 1", "Model 2", "Model 3"),Full.Model = c("SSreg(X1, X2)","SSreg(X1,X2, X3,X4)", "SSreg(X1, X2, X3)"), Restricted.Model = c("SSreg(X1 | X2)", "SSreg(X1,X2 | X3,X4)", "SSreg(X1| X2, X3)"), R.Square = c(resmodel1.rsqaure, resmodel2.rsqaure, resmodel3.rsqaure), Adj.RSquare = c(resmodel1.adjrsqaure, resmodel2.adjrsqaure, resmodel3.adjrsqaure), Increase_in_SSreg = c(ssreg.model1, ssreg.model2, ssreg.model3), Decrease_in_SSerror = c(sserror.model, sserror.model2, sserror.model3))


knitr::kable(
  head(results), booktabs = TRUE,
  caption = 'Sequential sum of squares'
)
Sequential sum of squares
Model Full.Model Restricted.Model R.Square Adj.RSquare Increase_in_SSreg Decrease_in_SSerror
Model 1 SSreg(X1, X2) SSreg(X1 | X2) 0.6662678 0.6359285 848.47 848.44
Model 2 SSreg(X1,X2, X3,X4) SSreg(X1,X2 | X3,X4) 0.9352888 0.9223466 127.88 127.88
Model 3 SSreg(X1, X2, X3) SSreg(X1| X2, X3) 0.8470262 0.8164315 367.33 367.33