#Simple Linear Regression – R code # Car Weight (in 1000 pounds) weight <-c(3.4,3.8,4.1,2.2,2.6,2.9,2.0,2.7,1.9,3.4) # Fuel Consumption (gallons/100miles) consum <- c(5.5,5.9,6.5,3.3,3.6,4.6,2.9,3.6,3.1,4.9) n <- length(weight) obsno <- 1:n # sequence command (equally-spaced) seq1 <- seq(0, 4, 0.5) seq1 # replicate command rep0 <- rep(2:4, 2) rep0 rep1 <- rep(2:4, 1:3) rep1 ## combine vectors by columns car<-cbind(obsno, weight, consum) # first row (observation) in car data car[1,] # second column in the car data car[,2] # linear model fit lm(y ~ x), model y=a+b*x lm(consum ~ weight ) # linear model y=b*x, no intercept lm(consum ~ weight -1) M <- matrix(1:4, ncol=2, byrow=T) # create a matrix t(M) # transpose of a vector solve(M) # inverse matrix of M X <- cbind(rep(1,n),car[,2]) # design matrix beta <- c(-0.363,1.639) # Calculate yhat, '%*%' matrix multiplication fitted.values <- X %*% beta # res = y - yhat residuals <- consum - fitted.values sum(residuals) # sum(e_i)=0 sum(t(residuals)%*%weight) # sum(x_i * e_i)=0 ## linear regression model lm(consum ~ weight ) # model y=a+b*x summary(lm(consum ~ weight ) ) ## ANOVA table for linear regression anova(lm(consum~weight))