data <- read.table("d:/PubH5470/LinearModelExample.txt", header=T) #Reading the response Y <- data$Y #Length of Y length(Y) #Reading X X <- as.matrix(data[,2:7]) #X <- cbind(data$X1,data$X2,data$X3,data$X4,data$X5,data$X6) ones <- rep(1,times=length(Y)) #ones <- rep(1,times=nrow(X)) X <- cbind(ones,X) #dimension of X dim(X) #Transpose of X is obtained by function t() X.t <- t(X) #Checking dimension of X.t dim(X.t) #Accessing Multivariate Normal in R library(MASS) help(mvrnorm) #Detaching MASS detach(package:MASS) #Multiplication of matrices is done by %*% X.tX = X.t%*%X #Inverse of a matrix is obtained by solve() X.tX.inv <- solve(X.tX) #Least squares estimate: beta.hat <- X.tX.inv%*%X.t%*%Y example.lm <- lm(Y ~ X[,1]+X[,2]+X[,3]+X[,4]+X[,5]+X[,6]) summary(example.lm)