##### Linear Regression Model ###### weight<-c(3.4,3.8,4.1,2.2,2.6,2.9,2.0,2.7,1.9,3.4) 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 seq1<-seq(0, 4, 0.5) # sequence command (equally-spaced) seq1 re0<-rep(2:4, 2) # replicate command rep0 re1<-rep(2:4, 1:3) rep1 car<-cbind(obsno, weight, consum) ## combine vectors by columns car car2<-rbind(weight, consum) ## combine vectors by rows car2 # first row (observation) in car data car[1,] # second column in the car data car[,2] # linear model fit lm(y ~ x) lm(consum ~ weight ) # model y=a+b*x lm(consum ~ weight -1) # model y=b*x, no intercept beta<-c(-0.363,1.639) # input coefficients as a vector beta # transpose of a vector M<-matrix(1:4, ncol=2, byrow=T) # create a matrix solve(M) # inverse matrix of M X <- cbind(rep(1,n),car[,2]) X fitted.values <- X %*% beta # '%*%' matrix multiplication fitted.values residuals <- consum - fitted.values residuals sum(residuals) sum(t(residuals)%*%weight) ### Descriptive Statistics ### summary(lm(consum ~ weight ) ) ### One-Way ANOVA Table for Simple Linear Regression### anova(lm(consum~weight))