#Linear Algebra using R set.seed(302010) #Set the dimensions n<-10 p<-3 #Generate the values for the X matrix #As an example, use rnorm to generate Normal variates. xvals<-rnorm(n*p) (X<-matrix(xvals,nrow=n,ncol=p)) #Set the n values for y y<-rnorm(n) ########################################## #Matrix multiplication b<-c(3,-2,1) #Result of Xb multiplication (X%*%b) #System Xb = y is over-determined. XtX<-t(X) %*% X Xty<-t(X) %*% y #Solve the now exactly determined system b<-solve(XtX,Xty) #Now use solve in a slightly different way #Compute the inverse first XtXinv<-solve(XtX) (b<-XtXinv %*% Xty)