3. The length of a species of fish is to be represented as a function of the age
ID: 3305934 • Letter: 3
Question
3. The length of a species of fish is to be represented as a function of the age and water tem- perature. The fish are kept in tanks at 25, 27, 29 and 31 degrees Celsius. After birth, a test specimen is chosen at random every 14 days and its length measured. There are 44 rows of data in the data set "fish.csv" You can get this into R using the syntax: data-read.csv("http://bsmith.mathstat.dal.ca/stat3340/Data/fish.csv" ,header-T) The matrix data includes 3 columns: age the age of the fish; temp the water temperature in degrees Celsius; length - the length of the fish Suppose you want to fit the model length = 0+1age + Atempte using matrix comm ands n R. (a) Form the X matrix and y vector. Make sure to include the column of 1's as the first column of X. Something like: X-cbind(rep( 1 ,dim (data) ) ,data[.1],data[2] ) y-datal3 should work. (Make sure the X and y you created are correct!) (b) Calculate and print XTX and XTy (c) Calculate and print (XTx)-1 (d) Calculate and print the least squares estimates. (check your results by running the command "lm(y ~x1+x2, data-data)". (e) Calculate and print the fitted values. (f) Calculate and print the residuals.Explanation / Answer
data = read.csv("http://bsmith.mathstat.dal.ca/stat3340/Data/fish.csv",header = T)
X =cbind(rep(1,dim(data)[1]),data[,1],data[,2])
> y = data[,3]
b)
(XTX)
z <- t(X) %*%X
> z
[,1] [,2] [,3]
[1,] 44 3660 1232
[2,] 3660 389684 102480
[3,] 1232 102480 34716
(XTy)
h <- t(X)%*%y
> h
[,1]
[1,] 136727
[2,] 13609918
[3,] 3804945
c)
inv(X'X)
solve(z)
[,1] [,2] [,3]
[1,] 3.6675383542 -9.758709e-04 -0.127272727
[2,] -0.0009758709 1.173178e-05 0.000000000
[3,] -0.1272727273 -1.545481e-18 0.004545455
d)
b <- solve(z)%*%h
> b
[,1]
[1,] 3904.26602
[2,] 26.24068
[3,] -106.41364
lenght^ = 3904.26602 + 26.4068*x1 -106.41364*x2
Please rate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.