# examples of parametric modeling using R # I extracted july values and year info from # co2, supplied with the R system # fit a linear model co2.trend <- lm(co2.july~co2.year) # or a quadratic co2.year2 <- co2.year^2 co2.trend <- lm(co2.july~co2.year + co2.year2) summary(co2.trend) # plot the autocorrelation function acf(resid(co2.trend)) # or partial autocorrelation function acf(resid(co2.trend),type='partial') #lme function with correlation=corAR1() is supposed to fit # a regression with ar(1) errors, but lme() and I don't # get along well. # I think the problem lme() expects subgroups of correl. obs # here there is only one group. # co2.ar1 <- lme(co2.july ~ co2.year + co2.year2, correlation=corAR1() ) # fit loess regression co2.loess <- loess(co2.july ~ co2.year) # span = specifies span (f in my notation), default 0.75 # degree = specifies degree, default 2 summary(co2.loess) plot(co2.year,co2.july) lines(co2.year,predict(co2.loess)) # use loess to test lack of fit of a linear model # anova.loess can only compare multiple loess fits # here's how to approximate a quadratic model by a loess fit co2.loess2 <- loess(co2.july ~ co2.year, span = 100) anova(co2.loess,co2.loess2) # a linear model would be obtained by adding degree=1