atsa-es / safs-timeseries

Repository for miscellaneous code and data used in FISH 550 (Applied Time Series Analysis) at University of Washington
8 stars 3 forks source link

HW1 problem 5 #4

Closed bran1685 closed 7 years ago

bran1685 commented 7 years ago

I am struggling with creating the Z matrix for problem 5 and solving for the parameters. I presently have the following for the y and Z matrices-

y=matrix(homeworkdat$Ozone, nrow=1)

fit=lm(Ozone ~ -1 + region, data=homeworkdat) Z=rbind(c(1,0,1,0,1), c(0,1,0,1,0))

I can't get the solve to run (solve(t(Z)%%Z)%%t(Z)%*%y) and I figure it's because I'm not setting the Z matrix up properly. Can anyone help?

ericward-noaa commented 7 years ago

Yep! Here's the answer I think,

create matrix of the response

y=matrix(homeworkdat$Ozone, nrow=1)

get the model matrix out of the model

fit=lm(Ozone ~ -1 + region, data=homeworkdat) Z = model.matrix(fit)

we could just use Eli's code, and pass in t(Z) = d

d=t(Z) y%%t(d)%%solve(d%*%t(d))

or alternatively, get rid of some of the redundant transposes

d=Z y%%d%%solve(t(d)%*%d)