R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("dyn-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('dyn') Loading required package: zoo > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "dyn-package" > > ### * dyn-package > > flush(stderr()); flush(stdout()) > > ### Name: dyn-package > ### Title: Time Series Regression > ### Aliases: dyn-package > ### Keywords: models > > ### ** Examples > > x <- ts(seq(10)^2) > dyn$lm(x ~ lag(x)) Call: lm(formula = dyn(x ~ lag(x))) Coefficients: (Intercept) lag(x) -4.134 0.839 > dyn$glm(x ~ lag(x)) Call: glm(formula = dyn(x ~ lag(x))) Coefficients: (Intercept) lag(x) -4.134 0.839 Degrees of Freedom: 8 Total (i.e. Null); 7 Residual Null Deviance: 6308 Residual Deviance: 8.261 AIC: 30.77 > dyn$loess(x ~ lag(x)) Call: loess(formula = dyn(x ~ lag(x))) Number of Observations: 9 Equivalent Number of Parameters: 5.38 Residual Standard Error: 0.118 > > > > cleanEx(); ..nameEx <- "dyn" > > ### * dyn > > flush(stderr()); flush(stdout()) > > ### Name: dyn > ### Title: dynamic regression class > ### Aliases: dyn $.dyn model.frame.dyn model.frame.irts model.frame.its > ### model.frame.ts model.frame.zoo model.frame.zooreg predict.dyn > ### predict.ts predict.zoo fitted.dyn fitted.ts fitted.zoo residuals.dyn > ### residuals.ts residuals.zoo update.dyn anova.dyn fitted.irts > ### fitted.its fitted.zooreg predict.irts predict.its predict.zooreg > ### residuals.irts residuals.its residuals.zooreg > ### Keywords: regression > > ### ** Examples > > y <- ts(1:12, start = c(2000,2), freq = 4)^3 > x <- ts(1:9, start = c(2000,3), freq = 4)^2 > > # can be used with numerous different regression functions > y.lm <- dyn$lm( window(y, start = c(2000,4)) ~ diff(x) ) > y.lm <- dyn$lm( y ~ diff(x) ) > y.glm <- dyn$glm( y ~ diff(x) ) > y.loess <- dyn$loess( y ~ diff(x) ) > > y.lm <- dyn(lm(dyn(y ~ diff(x)))) # same > y.lm Call: lm(formula = dyn(y ~ diff(x))) Coefficients: (Intercept) diff(x) -303 68 > summary(y.lm) Call: lm(formula = dyn(y ~ diff(x))) Residuals: Min 1Q Median 3Q Max -102.00 -75.00 -18.00 51.75 147.00 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -303.000 87.975 -3.444 0.013731 * diff(x) 68.000 7.998 8.502 0.000145 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 103.7 on 6 degrees of freedom Multiple R-Squared: 0.9234, Adjusted R-squared: 0.9106 F-statistic: 72.29 on 1 and 6 DF, p-value: 0.0001449 > residuals(y.lm) Qtr1 Qtr2 Qtr3 Qtr4 2000 126 2001 27 -48 -93 -102 2002 -69 12 147 > fitted(y.lm) Qtr1 Qtr2 Qtr3 Qtr4 2000 -99 2001 37 173 309 445 2002 581 717 853 > y2.lm <- update(y.lm, . ~ . + lag(x,-1)) > y2.lm Call: lm(formula = y ~ diff(x) + lag(x, -1)) Coefficients: (Intercept) diff(x) lag(x, -1) 77.25 -19.75 19.50 > anova(y.lm, y2.lm) Analysis of Variance Table Model 1: y ~ diff(x) Model 2: y ~ diff(x) + lag(x, -1) Res.Df RSS Df Sum of Sq F Pr(>F) 1 6 64476 2 5 594 1 63882 537.73 2.775e-06 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > > # examples of using data > dyn$lm(y ~ diff(x), list(y = y, x = x)) Call: lm(formula = dyn(y ~ diff(x)), data = list(y = y, x = x)) Coefficients: (Intercept) diff(x) -303 68 > dyn$lm(y ~ diffx, list(y = y, diffx = diff(x))) Call: lm(formula = dyn(y ~ diffx), data = list(y = y, diffx = diff(x))) Coefficients: (Intercept) diffx -303 68 > > # invoke model.frame on formula as a dyn object > dyn$model.frame( y ~ diff(x) ) y diff(x) 3 27 3 4 64 5 5 125 7 6 216 9 7 343 11 8 512 13 9 729 15 10 1000 17 > > > > > ### *