##let's load some data
data(mesa.data)
##let's compute two smooth trend functions
trend <- calc.smooth.trends(mesa.data, n.basis = 2)
##and study the trends
par(mfrow=c(2,1), mar=c(2.5,2.5,.5,.5))
plot(trend$svd$date, trend$svd$V1, type="l", ylab="",xlab="",
ylim=range(c(trend$svd$V1,trend$svd$V2)))
lines(trend$svd$date, trend$svd$V2, col=2)
##Let's exclude locations with fewer than 100 observations
IND <- names(which(table(mesa.data$obs$ID) >= 100))
##now we also compute the CV trends.
trend2 <- calc.smooth.trends(mesa.data, n.basis = 2, subset=IND,
cv = TRUE)
##Let's compare to the previous result
lines(trend2$svd$date, trend2$svd$V1, lty=2)
lines(trend2$svd$date, trend2$svd$V2, lty=2, col=2)
##we can also study the cross validated results to examine the
##possible variation in the estimated trends.
plot(trend$svd$date, trend2$svd$V1, type="n", ylab="",xlab="",
ylim=range(c(trend2$svd$V1,trend2$svd$V2)))
for(i in 1:length(trend2$svd.cv)){
lines(trend2$svd.cv[[i]]$date, trend2$svd.cv[[i]]$V1, col=1)
lines(trend2$svd.cv[[i]]$date, trend2$svd.cv[[i]]$V2, col=2)
}
##Let's use our new trend functions in the mesa.data
mesa.data$trend <- trend2$svd
Run the code above in your browser using DataLab