Computation of Least square estimate using Linear algebra in R (Case study: Carbohydrate Diet)
In this dataset (taken from: An Introduction to Generalized Linear Models, A. J. Dobson & A. G. Barnett, 3rd edition p.96),
the response variable y corresponds to the percentage of total calories
obtained from complex carbohydrates for 20 male insulin-dependent diabetics who have been on a high carbohydrate diet for six months.
Additional information is collected about the individuals taking part in the study including age (in years), weight (relative to ideal weight)
and other calories intake from protein (as percentage).
This code used linear algebra to recover some of the results found by functions lm() and glm() used in the previous example (same dataset).
carbohydrate=c(33,40,37,27,30,43,34,48,30,38,50,51,30,36,41,42,46,24,35,37) # response vector
# least square estimate
fitted =X %*% BetaHat
#Residual standard error
# uncertainty of BetaHat (standard error)
#Multiple R-squared: (coefficient of determination)