Question

I was running a casecross function in season package with this code

library(season)
library(splines)

data(CVDdaily)
CVDdaily<-subset(CVDdaily,date<=as.Date('1987-12-31')) 

# Effect of ozone on CVD death

model1<- casecross(cvd ~ o3mean+tmpd+Mon+Tue+Wed+Thu+Fri+Sat, data=CVDdaily)

But when I use the natural splines I get an error message "Error in ns(tmpd, df = 6) : object 'tmpd' not found"

model2<-casecross(cvd ~ o3mean + ns(tmpd, df=6) +Mon+Tue+Wed+Thu+Fri+Sat, data=CVDdaily)

Does it mean that the package has no support for splines? If yes, I would appreciate any ideas on how to adjust the nonlinear effect of temperature using a season package?

Était-ce utile?

La solution

You can fit splines in season using the dlnm package. (I've used 4 degrees of freedom rather than 6).

library(season)
library(dlnm)
data(CVDdaily)
CVDdaily = subset(CVDdaily,date<=as.Date('1987-12-31')) 
t.spline = crossbasis(CVDdaily$tmpd,lag=0,argvar=list(fun="ns",df=4))
model2 = casecross(cvd ~ o3mean + t.spline +Mon+Tue+Wed+Thu+Fri+Sat, data=CVDdaily)
summary(model2)

To get a nice plot of the estimated spline you need to extract the coefficients and the variance-covariance matrix.

coef = coefficients(model2$c.model)[2:5]
var.cov = model2$c.model$var[2:5,2:5]
pred.t = crosspred(basis=t.spline, at=45:86, vcov=var.cov, coef=coef, model.link='identity')
plot(pred.t,"overall")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top