## Define the directory where the data set is setwd("C:/UConn/242_3115/R/") ## Load the data set and prepare for use data <- read.table("Multiple_regress.txt") dimnames(data)[[2]] <- c("X1","X2","X3","X4","X5","X6","X7","Y") data <- as.data.frame(data) ## Fit a multiple linear regression with the data fit <- lm(Y~X2+X3+X4+X5+X6+X7, data=data) X <- model.matrix(fit) beta <- coefficients(fit) e <- residuals(fit) n <- nrow(X) p <- ncol(X) ##F test to add an extra variable (e.g. X7) given all others are in the model ## Fit a multiple linear regression with the data fit <- lm(Y~X2+X3+X4+X5+X6+X7, data=data) ## Fit a multiple linear regression with the data fit2 <- lm(Y~X2+X3+X4+X5+X6, data=data) ##anova() function perform the F-test for the addition of 1 extra variable anova(fit2,fit) ##Do the F-test by hand q <- 1 C <- c(rep(0,6),1) Q <- t((t(C) %*% beta)) %*% solve(t(C) %*% solve(t(X)%*%X) %*% C) %*% (t(C) %*% beta) SSE <- t(e) %*% e F <- (Q/q) / (SSE/(n-p)) 1-pf(F,q,n-p) 2*(1-pt(sqrt(F),n-p)) anova(fit2,fit) ##F test to add a group variables (e.g. X2,X5,X7) given all others are in the model ## Fit a multiple linear regression with the data fit <- lm(Y~X2+X3+X4+X5+X6+X7, data=data) ## Fit a multiple linear regression with the data fit2 <- lm(Y~X3+X4+X6, data=data) ##anova() function perform the F-test for the addition of 1 extra variable anova(fit2,fit) ##Do the F-test by hand q <- 3 C <- matrix(0,nrow=p,ncol=q) C[2,1] = C[5,2] = C[7,3] <- 1 d <- c(0,0,0) Q <- t((t(C) %*% beta - d)) %*% solve(t(C) %*% solve(t(X)%*%X) %*% C) %*% (t(C) %*% beta -d) SSE <- t(e) %*% e F <- (Q/q) / (SSE/(n-p)) 1-pf(F,q,n-p) anova(fit2,fit)