R objects and functions
Søren Højsgaard Department of Mathematical Sciences Aalborg University, Denmark October 15, 2012
Printed: October 15, 2012 File: objects-slides.tex
R objects and functions Sren Hjsgaard Department of Mathematical - - PowerPoint PPT Presentation
R objects and functions Sren Hjsgaard Department of Mathematical Sciences Aalborg University, Denmark October 15, 2012 Printed: October 15, 2012 File: objects-slides.tex 2 Contents 1 Attributes 3 1.1 class attributes . . . . . . . . .
Printed: October 15, 2012 File: objects-slides.tex
2
1 Attributes 3 1.1 class attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2 More about classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 The S3 object system 7 2.1 Example: Single exponential smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2 Generic functions and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 More about generic functions and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.4 Multiple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5 Example: Double exponential smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.6 EXERCISES: Exponential smoothing and the S3 system . . . . . . . . . . . . . . . . . . . . . 23
3
R> x <- 123 R> attr(x,"foo") <- "something" R> attr(x,"bar") <- c(1,10) R> x [1] 123 attr(,"foo") [1] "something" attr(,"bar") [1] 1 10 R> attr(x,"foo") [1] "something" R> attributes(x) $foo [1] "something" $bar [1] 1 10
4
R> (today <- Sys.Date()) [1] "2012-10-15" R> dput(today) structure(15628, class = "Date") R> ff <- factor(c(1,2,1,2)) R> dput(ff) structure(c(1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor")
R> class(today) [1] "Date" R> class(ff) [1] "factor"
5
R> MM <- matrix(1:4, nrow=2) R> dput(MM) structure(1:4, .Dim = c(2L, 2L)) R> class(MM) [1] "matrix" R> AA <- array(1:8, dim=c(2,2,2)) R> dput(AA) structure(1:8, .Dim = c(2L, 2L, 2L)) R> class(AA) [1] "array"
6 R> VV <- 1:8 R> dim(VV) <- 8 R> dput(VV) structure(1:8, .Dim = 8L) R> class(VV) [1] "array"
R> is(MM, "array") [1] TRUE
7
8
R> data(Nile) R> plot(Nile)
Time Nile 1880 1900 1920 1940 1960 600 800 1200
9
10
R> ses <- function(yvar, alpha=.1){ n.obs <- length(yvar) sss <- rep.int(NA, n.obs) sss[1] <- yvar[1] for (ii in 2:n.obs){ sss[ii] <- alpha * yvar[ii] + (1-alpha)*sss[ii-1] } res <- list(x=1:n.obs, y=sss, y.obs=yvar) class(res) <- c('sesObj','list') res } R> sss <- ses(as.numeric(Nile)) R> str(sss) List of 3 $ x : int [1:100] 1 2 3 4 5 6 7 8 9 10 ... $ y : num [1:100] 1120 1124 1108 1118 1122 ... $ y.obs: num [1:100] 1120 1160 963 1210 1160 1160 813 1230 1370 1140 ...
11
R> plot(as.numeric(Nile)) R> lines(y~x, data=sss)
40 60 80 100 600 800 1200 Index as.numeric(Nile)
12
R> plot.sesObj <- function(x,...){ plot(x$x, x$y.obs,...) lines(x$x, x$y, col='red',...) } R> plot(sss,pch=16)
13
40 60 80 100 600 800 1200 x$x x$y.obs
14
R> residuals.sesObj <- function(object, ...) {
} R> fitted.sesObj <- function(object, ...) {
}
R> aa <- as.data.frame(sss) R> head(aa) x y y.obs 1 1 1120.000 1120 2 2 1124.000 1160 3 3 1107.900 963 4 4 1118.110 1210 5 5 1122.299 1160 6 6 1126.069 1160
15 R> class(sss) [1] "sesObj" "list"
16
R> ## Generic function: R> goodFun <- function(x){ UseMethod('goodFun') } R> ## Default method: R> goodFun.default <- function(x){ cat("Don't know what to do here\n") } R> zzz <- 123 R> goodFun(zzz) Don't know what to do here R> class(zzz) <- 'foo' R> goodFun(zzz) This is a 'foo' object: 123
17
R> ## Method for foo objects R> goodFun.foo <- function(x){ cat(sprintf("This is a 'foo' object: %s\n", toString(x))) } R> goodFun(zzz) This is a 'foo' object: 123
18
R> ## Method for bar objects R> goodFun.bar <- function(x){ cat(sprintf("This is a 'bar' object: %s\n", toString(x))) } R> class(zzz) <- c('bar', 'foo') R> goodFun(zzz) This is a 'bar' object: 123
19
R> ## Method for bar objects R> goodFun.bar <- function(x){ cat(sprintf("This is a 'bar' object: %s\n", toString(x))) NextMethod(x) } R> class(zzz) <- c('bar', 'foo') R> goodFun(zzz) This is a 'bar' object: 123 This is a 'foo' object: 123
20
R> y <- log(as.numeric(JohnsonJohnson)) R> se <- ses(y,.1) R> plot(se,xlab='')
40 60 80 1 2 x$y.obs
21
α where β = 1 − α.
t
t
α. From these considerations therefore have
t }α
t ) = 2Stt − S[2] t
22
t } = 2St − S[2] t
t }α
23