R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
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.
Natural language support but running in an English locale
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 an HTML browser interface to help.
Type 'q()' to quit R.
> suppressPackageStartupMessages(library(Roux)) # Added by roux
> ## BASICS
> # Setup some scalars
> x <- 3
> y <- 4
> z <- sqrt(x*x + y*y)
> use.dots.in.names <- z - 5
>
> # Print out z
> print(z)
[1] 5
> # The default action is print()
> z
[1] 5
>
> # So you have a neat scientific calculator
> sin(log(2.718281828)*pi)
[1] 5.305313e-10
>
> ## SCALARS
> # The basic scalar types
> x <- 3
> take.me <- FALSE
> s <- "this is a string"
> x
[1] 3
> take.me
[1] FALSE
> s
[1] "this is a string"
>
> # Exploring objects using str() --
> str(x)
num 3
> str(take.me)
logi FALSE
> str(s)
chr "this is a string"
>
> ## DATES: A SPECIAL KIND OF SCALAR
> s <- "2004-04-22"
> d <- as.Date(s)
> d
[1] "2004-04-22"
> str(d)
Class 'Date' num 12530
> s <- "22-Apr-2004"
> d <- as.Date(s, format="%d-%b-%Y")
> d
[1] "2004-04-22"
> as.Date("22-04-2004", format="%d-%m-%Y")
[1] "2004-04-22"
> as.Date("22-04-04", format="%d-%m-%y")
[1] "2004-04-22"
> as.Date("22-Apr-04", format="%d-%b-%y")
[1] "2004-04-22"
>
> ## VECTORS
> x <- c(2,3,7, 9, 10, 11) # the c() operator, for concatenation
> x[1]
[1] 2
> x[6]
[1] 11
> length(x)
[1] 6
>
> # Help about all R functions is online!
> ?length
length package:base R Documentation
_L_e_n_g_t_h _o_f _a_n _O_b_j_e_c_t
_D_e_s_c_r_i_p_t_i_o_n:
Get or set the length of vectors (including lists) and factors,
and of any other R object for which a method has been defined.
_U_s_a_g_e:
length(x)
length(x) <- value
_A_r_g_u_m_e_n_t_s:
x: an R object. For replacement, a vector or factor.
value: an integer.
_D_e_t_a_i_l_s:
Both functions are generic: you can write methods to handle
specific classes of objects, see InternalMethods. 'length<-' has
a '"factor"' method.
The replacement form can be used to reset the length of a vector.
If a vector is shortened, extra values are discarded and when a
vector is lengthened, it is padded out to its new length with
'NA's ('nul' for raw vectors).
These functions are primitive, so argument names are ignored (but
this might not be true of their methods).
_V_a_l_u_e:
The default method currently returns an 'integer' of length 1.
Since this may change in the future and may differ for other
methods, programmers should not rely on it. (Should the length
exceed the maximum representable integer, it is returned as 'NA'.)
For vectors (including lists) and factors the length is the number
of elements. For an environment it is the number of objects in
the environment, and 'NULL' has length 0. For expressions and
pairlists (including language objects and dotlists) it is the
length of the pairlist chain. All other objects (including
functions) have length one: note that for functions this differs
from S.
The replacement form removes all the attributes of 'x' except its
names.
_R_e_f_e_r_e_n_c_e_s:
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
Language_. Wadsworth & Brooks/Cole.
_S_e_e _A_l_s_o:
'nchar' for counting the number of characters in character
vectors.
_E_x_a_m_p_l_e_s:
length(diag(4))# = 16 (4 x 4)
length(options())# 12 or more
length(y ~ x1 + x2 + x3)# 3
length(expression(x, {y <- x^2; y+2}, x^y)) # 3
## from example(warpbreaks)
require(stats)
fm1 <- lm(breaks ~ wool * tension, data = warpbreaks)
length(fm1$call) # 3, lm() and two arguments.
length(formula(fm1)) # 3, ~ lhs rhs
>
> # Arithmetic that gobbles a vector at a time
> x
[1] 2 3 7 9 10 11
> x+2
[1] 4 5 9 11 12 13
> y <- log(x)
> y
[1] 0.6931472 1.0986123 1.9459101 2.1972246 2.3025851 2.3978953
>
> # Clever vector addressing
> x[1]
[1] 2
> x[6]
[1] 11
> x[c(1,6)]
[1] 2 11
> indexes <- c(1,3,5)
> x[indexes]
[1] 2 7 10
>
> # Another shorthand
> 1:4
[1] 1 2 3 4
> x[1:4]
[1] 2 3 7 9
> indexes <- 1:4
> x[indexes]
[1] 2 3 7 9
>
> # Another shorthand
> switches <- c(TRUE,FALSE,FALSE,FALSE,FALSE,TRUE)
> x[switches]
[1] 2 11
>
> # Let's stay in touch with how to explore objects --
> str(x)
num [1:6] 2 3 7 9 10 11
> str(x[1])
num 2
>
> ## FACTORS: A SPECIAL KIND OF SCALAR
> names <- c("Payal", "Shraddha", "Aditi", "Kritika", "Diwakar")
> attire <- c("Sari", "Salwar", "Sari", "Sari", "Kurta")
> # attire is a "categorical variable"
> attire <- factor(attire)
> attire
[1] Sari Salwar Sari Sari Kurta
Levels: Kurta Salwar Sari
> table(attire)
attire
Kurta Salwar Sari
1 1 3
> table(names, attire)
attire
names Kurta Salwar Sari
Aditi 0 0 1
Diwakar 1 0 0
Kritika 0 0 1
Payal 0 0 1
Shraddha 0 1 0
>
> ## NOT AVAILABLE: A SPECIAL SCALAR
> x <- c(2,3,NA,4,5,NA)
> x
[1] 2 3 NA 4 5 NA
> x + 2 # Automatic rules about NA arithmetic
[1] 4 5 NA 6 7 NA
>
> ## MATRICES
> X <- matrix(NA, nrow=7, ncol=3)
> X
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
[4,] NA NA NA
[5,] NA NA NA
[6,] NA NA NA
[7,] NA NA NA
> X[4,] <- c(2,3,4)
> X
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
[4,] 2 3 4
[5,] NA NA NA
[6,] NA NA NA
[7,] NA NA NA
> X[,3] <- 1:7
> X
[,1] [,2] [,3]
[1,] NA NA 1
[2,] NA NA 2
[3,] NA NA 3
[4,] 2 3 4
[5,] NA NA 5
[6,] NA NA 6
[7,] NA NA 7
> str(X)
num [1:7, 1:3] NA NA NA 2 NA NA NA NA NA NA ...
> nrow(X)
[1] 7
> ncol(X)
[1] 3
>
> # As with vectors, arithmetic that attacks a full matrix at a time!
> X + 1
[,1] [,2] [,3]
[1,] NA NA 2
[2,] NA NA 3
[3,] NA NA 4
[4,] 3 4 5
[5,] NA NA 6
[6,] NA NA 7
[7,] NA NA 8
>
> ## LISTS
> # Ability to make a parcel of apparently unrelated materials.
> results <- list(mu=0.2, sigma=0.9, x=c(1,2,3,9))
> str(results)
List of 3
$ mu : num 0.2
$ sigma: num 0.9
$ x : num [1:4] 1 2 3 9
> # Accessing elements --
> results$mu
[1] 0.2
> results$sigma <- 9
> results$new <- "Hello"
> results
$mu
[1] 0.2
$sigma
[1] 9
$x
[1] 1 2 3 9
$new
[1] "Hello"
>
> ## DATASET, TERMED `DATA FRAME' in R
> # A dataset is a list of vectors, all of the same length.
> names <- c("Payal", "Shraddha", "Aditi", "Kritika", "Diwakar")
> age <- c(15, 17, 15, 19, 20)
> iq <- c(90, 100, 110, 120, 160)
> # By default, data.frame() forces the names to be factors...
> D <- data.frame(names=names, age=age, iq=iq)
> str(D)
'data.frame': 5 obs. of 3 variables:
$ names: Factor w/ 5 levels "Aditi","Diwakar",..: 4 5 1 3 2
$ age : num 15 17 15 19 20
$ iq : num 90 100 110 120 160
> D
names age iq
1 Payal 15 90
2 Shraddha 17 100
3 Aditi 15 110
4 Kritika 19 120
5 Diwakar 20 160
> nrow(D)
[1] 5
> ncol(D)
[1] 3
> summary(D)
names age iq
Aditi :1 Min. :15.0 Min. : 90
Diwakar :1 1st Qu.:15.0 1st Qu.:100
Kritika :1 Median :17.0 Median :110
Payal :1 Mean :17.2 Mean :116
Shraddha:1 3rd Qu.:19.0 3rd Qu.:120
Max. :20.0 Max. :160
> # Accessing a column in a data frame
> D$age
[1] 15 17 15 19 20
> # Accessing an observation in a data frame - use matrix-like notation
> D[3,]
names age iq
3 Aditi 15 110
> # Accessing the 3rd value of age
> D$age[3]
[1] 15
>
> ## REGRESSION
> m <- lm(iq ~ age, D)
> m
Call:
lm(formula = iq ~ age, data = D)
Coefficients:
(Intercept) age
-52.692 9.808
> summary(m)
Call:
lm(formula = iq ~ age, data = D)
Residuals:
1 2 3 4 5
-4.423 -14.038 15.577 -13.654 16.538
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -52.692 66.478 -0.793 0.4859
age 9.808 3.838 2.555 0.0836 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 17.5 on 3 degrees of freedom
Multiple R-squared: 0.6852, Adjusted R-squared: 0.5803
F-statistic: 6.53 on 1 and 3 DF, p-value: 0.08355
> # Remember that shoe size is strongly correlated with IQ
> plot(D$age, D$iq, xlab="Age (years)", ylab="IQ")
png_filename:270630449.560913.png
> abline(h=100, lty=3)
> lines(D$age, fitted.values(m), col="blue", lwd=2)
>
> ## SIMULATION
> runif(10)
[1] 0.8217094 0.6192698 0.9856793 0.5711301 0.9048608 0.8406845 0.7962942
[8] 0.3196529 0.7688444 0.7815745
> runif(10)
[1] 0.42827847 0.78416458 0.60033519 0.10123555 0.52825379 0.51456850
[7] 0.03949651 0.73648896 0.62001077 0.78683739
> set.seed(133); runif(10)
[1] 0.5360112 0.8462701 0.6358006 0.4231022 0.2135632 0.6291572 0.6158544
[8] 0.3543251 0.8921125 0.8915516
> set.seed(133); runif(10)
[1] 0.5360112 0.8462701 0.6358006 0.4231022 0.2135632 0.6291572 0.6158544
[8] 0.3543251 0.8921125 0.8915516
> summary(runif(100))
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0004662 0.2961000 0.6408000 0.5568000 0.7745000 0.9799000
> summary(runif(1000))
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0001383 0.2295000 0.4775000 0.4864000 0.7382000 0.9999000
> summary(runif(10000))
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0001874 0.2526000 0.5041000 0.5030000 0.7514000 0.9999000
> summary(runif(100000))
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.696e-06 2.495e-01 5.011e-01 5.002e-01 7.495e-01 1.000e+00
>
> cat("See how large datasets stabilise sample statistics\n")
See how large datasets stabilise sample statistics
> summary(runif(100)); summary(runif(100))
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.006302 0.213900 0.582600 0.546200 0.803100 0.995600
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.008849 0.275200 0.553700 0.524800 0.772000 0.991200
> summary(runif(10000)); summary(runif(10000))
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.941e-05 2.512e-01 5.008e-01 5.011e-01 7.519e-01 9.999e-01
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000911 0.2461000 0.4952000 0.4992000 0.7492000 0.9997000
>
> plot(density(runif(1000)))
png_filename:775704563.779454.png
> plot(density(rnorm(1000)))
png_filename:608291089.656157.png
> x <- seq(-4,4,0.01)
> lines(x, dnorm(x), col="blue", lwd=2)
>
> ## SIMPLE SUMMARY STATISTICS
> x <- rnorm(10000)
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-3.8240000 -0.6855000 -0.0001786 -0.0111000 0.6704000 3.4020000
> quantile(x, probs=c(0.01, 0.025, 0.25, 0.5, 0.75, 0.975, 0.99))
1% 2.5% 25% 50% 75%
-2.3347398580 -1.9981726703 -0.6854983749 -0.0001785747 0.6703738590
97.5% 99%
1.9327101529 2.2524411558
> IQR(x)
[1] 1.355872
> fivenum(x)
[1] -3.8236766870 -0.6855711288 -0.0001785747 0.6705088526 3.4015477674
> sd(x)
[1] 0.9950824
> range(x)
[1] -3.823677 3.401548
> mean(x)
[1] -0.01109757
> mean(x, trim=0.1) # Delete 10% of data at each end first
[1] -0.006787843
>
> ## REGRESSION ON A SIMULATED DATASET
> x <- 2 + 3*runif(100)
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.002 2.857 3.838 3.634 4.400 4.963
> y <- -7 + 4*x + rnorm(100)
> summary(lm(y ~ x)) # Convince yourself the intercept is ok
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-2.27366 -0.58675 -0.02053 0.58907 2.14452
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.5911 0.4032 -16.35 <2e-16 ***
x 3.8592 0.1078 35.79 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.9469 on 98 degrees of freedom
Multiple R-squared: 0.9289, Adjusted R-squared: 0.9282
F-statistic: 1281 on 1 and 98 DF, p-value: < 2.2e-16
> # Most statistical functions give back results of results.
> res.1 <- lm(y ~ x)
> str(res.1) # There's a lot of goodies here!
List of 12
$ coefficients : Named num [1:2] -6.59 3.86
..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
$ residuals : Named num [1:100] -0.1272 0.0514 0.5728 0.4123 -0.4388 ...
..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
$ effects : Named num [1:100] -74.331 33.885 0.58 0.426 -0.424 ...
..- attr(*, "names")= chr [1:100] "(Intercept)" "x" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:100] 9.06 3.61 5.23 8.83 9.32 ...
..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
..$ qr : num [1:100, 1:2] -10 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:100] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$ qraux: num [1:2] 1.1 1.12
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 98
$ xlevels : list()
$ call : language lm(formula = y ~ x)
$ terms :Classes 'terms', 'formula' length 3 y ~ x
.. ..- attr(*, "variables")= language list(y, x)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. ..$ : chr "x"
.. ..- attr(*, "term.labels")= chr "x"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, x)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y" "x"
$ model :'data.frame': 100 obs. of 2 variables:
..$ y: num [1:100] 8.93 3.66 5.8 9.24 8.88 ...
..$ x: num [1:100] 4.06 2.64 3.06 4 4.12 ...
..- attr(*, "terms")=Classes 'terms', 'formula' length 3 y ~ x
.. .. ..- attr(*, "variables")= language list(y, x)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. .. ..$ : chr "x"
.. .. ..- attr(*, "term.labels")= chr "x"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(y, x)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
- attr(*, "class")= chr "lm"
> res.1$coefficients
(Intercept) x
-6.591107 3.859227
> # In this case, detailed calculations require summary() to get what
> # I call "level 2 results".
> res.2 <- summary(res.1)
> str(res.1) # There's even more goodies here!!
List of 12
$ coefficients : Named num [1:2] -6.59 3.86
..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
$ residuals : Named num [1:100] -0.1272 0.0514 0.5728 0.4123 -0.4388 ...
..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
$ effects : Named num [1:100] -74.331 33.885 0.58 0.426 -0.424 ...
..- attr(*, "names")= chr [1:100] "(Intercept)" "x" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:100] 9.06 3.61 5.23 8.83 9.32 ...
..- attr(*, "names")= chr [1:100] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
..$ qr : num [1:100, 1:2] -10 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:100] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "(Intercept)" "x"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$ qraux: num [1:2] 1.1 1.12
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 98
$ xlevels : list()
$ call : language lm(formula = y ~ x)
$ terms :Classes 'terms', 'formula' length 3 y ~ x
.. ..- attr(*, "variables")= language list(y, x)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. ..$ : chr "x"
.. ..- attr(*, "term.labels")= chr "x"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, x)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y" "x"
$ model :'data.frame': 100 obs. of 2 variables:
..$ y: num [1:100] 8.93 3.66 5.8 9.24 8.88 ...
..$ x: num [1:100] 4.06 2.64 3.06 4 4.12 ...
..- attr(*, "terms")=Classes 'terms', 'formula' length 3 y ~ x
.. .. ..- attr(*, "variables")= language list(y, x)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "y" "x"
.. .. .. .. ..$ : chr "x"
.. .. ..- attr(*, "term.labels")= chr "x"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(y, x)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
- attr(*, "class")= chr "lm"
> res.2$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.591107 0.4031560 -16.34877 9.391598e-30
x 3.859227 0.1078389 35.78696 4.537876e-58
> res.2$r.squared
[1] 0.9289189
> res.2$sigma
[1] 0.946855
> # There are useful generic functions which work for a lot of models --
> logLik(res.1)
'log Lik.' -135.4228 (df=3)
> AIC(res.1)
[1] 276.8456
>
> ## WRITING AND THEN READING FILES
> D
names age iq
1 Payal 15 90
2 Shraddha 17 100
3 Aditi 15 110
4 Kritika 19 120
5 Diwakar 20 160
> write.csv(D, file="demo.csv")
> system("cat demo.csv") # Say "type" if you're on DOS
"","names","age","iq"
"1","Payal",15,90
"2","Shraddha",17,100
"3","Aditi",15,110
"4","Kritika",19,120
"5","Diwakar",20,160
> E <- read.table("demo.csv", skip=1, sep=",",
+ col.names=c("obsnum","name","age","iq"))
> E
obsnum name age iq
1 1 Payal 15 90
2 2 Shraddha 17 100
3 3 Aditi 15 110
4 4 Kritika 19 120
5 5 Diwakar 20 160
> E$obsnum <- NULL
> E
name age iq
1 Payal 15 90
2 Shraddha 17 100
3 Aditi 15 110
4 Kritika 19 120
5 Diwakar 20 160
> # Compare against --
> D
names age iq
1 Payal 15 90
2 Shraddha 17 100
3 Aditi 15 110
4 Kritika 19 120
5 Diwakar 20 160
>
> ## DAILY TIME-SERIES USING THE `ZOO' PACKAGE
> # Zoo is a very powerful, high-level time-series system. To learn more:
> library(help=zoo)
Information on package 'zoo'
Description:
Package: zoo
Version: 1.5-8
Date: 2009-07-22
Title: Z's ordered observations
Author: Achim Zeileis, Gabor Grothendieck
Maintainer: Achim Zeileis <Achim.Zeileis@R-project.org>
Description: An S3 class with methods for totally ordered
indexed observations. It is particularly aimed at
irregular time series of numeric vectors/matrices
and factors. zoo's key design goals are
independence of a particular index/date/time class
and consistency with ts and base R by providing
methods to extend standard generics.
Depends: R (>= 2.4.1), stats
Suggests: coda, chron, DAAG, fame, fCalendar, fSeries, fts,
its, lattice, strucchange, timeDate, timeSeries,
tseries, xts
Imports: stats, utils, graphics, grDevices, lattice
LazyLoad: yes
License: GPL-2
URL: http://R-Forge.R-project.org/projects/zoo/
Packaged: 2009-07-22 17:21:04 UTC; zeileis
Repository: CRAN
Date/Publication: 2009-07-22 19:20:13
Built: R 2.9.1; ; 2009-07-23 07:18:21 UTC; unix
Index:
MATCH Value Matching
ORDER Ordering Permutation
aggregate.zoo Compute Summary Statistics of zoo Objects
as.Date.numeric Date Conversion Functions from Numeric, Integer
and ts Objects
as.zoo Coercion from and to zoo
coredata Extracting/Replacing the Core Data of Objects
frequency<- Replacing the Index of Objects
index Extracting/Replacing the Index of Objects
is.regular Check Regularity of a Series
lag.zoo Lags and Differences of zoo Objects
make.par.list Make a List from a Parameter Specification
merge.zoo Merge Two or More zoo Objects
na.approx Replace NA by Interpolation
na.locf Last Observation Carried Forward
na.trim Trim Leading/Trailing Missing Observations
plot.zoo Plotting zoo Objects
read.zoo Reading and Writing zoo Series
rollapply Apply Rolling Functions
rollmean Rolling Means/Maximums/Medians
window.zoo Extract/Replacing the Time Windows of Objects
xyplot.zoo Plot zoo Series with Lattice
yearmon An Index Class for Monthly Data
yearqtr An Index Class for Quarterly Data
zoo Z's Ordered Observations
zooreg Regular zoo Series
Further information is available in the following vignettes in
directory '/Library/Frameworks/R.framework/Resources/library/zoo/doc':
zoo-faq: zoo FAQ (source, pdf)
zoo-quickref: zoo Quick Reference (source, pdf)
zoo: zoo: An S3 Class and Methods for Indexed Totally Ordered
Observations (source, pdf)
> vignette("zoo-quickref")
>
> # Let's do a daily time-series
> dates <- as.Date(c("2004-04-01", "2004-04-02", "2004-04-03", "2004-04-05"))
> values <- rnorm(4)
> library(zoo)
Attaching package: 'zoo'
The following object(s) are masked from package:base :
as.Date.numeric
> z <- zoo(values, order.by=dates)
> z
2004-04-01 2004-04-02 2004-04-03 2004-04-05
-1.1597774 0.6536548 -1.7800565 -0.5708740
> plot(z)
png_filename:880809754.584986.png
>
> ## TIME SERIES ANALYSIS
> # ACF of white noise
> z <- rnorm(100)
> acf(z)
> z <- rnorm(1000)
> acf(z)
>
> # Simulate data from an AR(1) process
> x <- arima.sim(model=list(ar=c(0.5)), n=1000)
> arima(x, order=c(1,0,0))
Call:
arima(x = x, order = c(1, 0, 0))
Coefficients:
ar1 intercept
0.5071 0.0757
s.e. 0.0273 0.0641
sigma^2 estimated as 1.001: log likelihood = -1419.62, aic = 2845.24
> acf(x)
> # Let's try to estimate a wrong model --
> arima(x, order=c(3,0,0))
Call:
arima(x = x, order = c(3, 0, 0))
Coefficients:
ar1 ar2 ar3 intercept
0.4959 0.0221 0.0004 0.0761
s.e. 0.0316 0.0354 0.0318 0.0656
sigma^2 estimated as 1.001: log likelihood = -1419.37, aic = 2848.74
>
> proc.time()
user system elapsed
1.745 0.198 3.407