An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20070722/a1106c4f/attachment.pl
Zoo NA handling documentation?
5 messages · icosa atropa, Gabor Grothendieck
There are some example here: https://stat.ethz.ch/pipermail/r-sig-finance/2006q2/
On 7/22/07, icosa atropa <icos.atropa at gmail.com> wrote:
Thank you so much for your previous help with zoo and lattice. Following your guidance, I've found the zoo package very powerful. The rollapply demos were also illuminating! I'm wondering if I've found some inconsistency with zoo documentation r.e. NA handling. Both na.omit and na.contiguous are mentioned as methods in the current CRAN pdf reference documentation. The hyperlink for na.omit on p18 lead to p1, and there are no function defs for either in the docs. Similarly, both ?na.omit and ?na.contiguous lead to function defs. Are these unimplemented as generic functions? I'm a bit confused as to the proper context in which to use na.omit and na.contiguous. best, christian On 7/8/07, Gabor Grothendieck <ggrothendieck at gmail.com > wrote:
Try this (and see ?xyplot.zoo for more info):
# first run code in your post that creates the experiment object
# create a time series from experiment
z <- zoo(matrix(experiment$meas, 25, byrow = TRUE))
colnames(z) <- c("NC", "NT", "SC", "ST")
# add grid to existing plot
addGrid <- function() for(i in 1:2) {
trellis.focus("panel", i, 1); panel.grid(); trellis.unfocus()
}
zz <- z
colnames(zz) <- c("N", "NT", "S", "ST")
xyplot(zz, screens = c(1, 1, 2, 2), col = 1:2, layout = 2:1, type = "b")
addGrid()
zz <- z
colnames(zz) <- c("C", "T", "SC", "ST")
xyplot(zz, screens = 1:2, col = c(1, 1, 2, 2), layout = 2:1, type = "b")
addGrid()
On 7/8/07, icosa atropa <icos.atropa at gmail.com> wrote:
Please provide something reproducible and show what you want to do without zoo.
The following example is contrived, but closely resembles my data. I pull my data from a database using RODBC - it comes in as a data.frame with factors, timestamp, and data all globbed together. This is very convenient for plotting with lattice, but complicates time series
analysis.
I can't turn the object into a zoo directly due to the mixed
factor/data.
I've looked at fCalendar, whose timeSeries object has an recordtID
slot...
# Everything from here down tested with R 2.5.0
experiment.layout = expand.grid( tx=c('control', 'treatment'), rep=c(
'N',
'S'), time=1:25)
# ^^ block design experiment
experiment = data.frame( experiment.layout, meas=rnorm(100) )
# ^^the time-indexed dataframe of observations by
experiment$meas=experiment$meas + c(1,-1,2,-2)
# ^^experimental effects differ by bloco and treatment
summary(experiment)
require(lattice)
#
#First split condition on rep, grouping by tx
#
xyplot(meas ~ time | rep, data=experiment, groups=tx,
panel=function(x, y, subscripts, groups) {
panel.grid()
panel.superpose(x, y, subscripts, groups)
}
)
#
#Next condition on tx, grouping by experiment
#
xyplot(meas ~ time | tx, data=experiment, groups=rep,
panel=function(x, y, subscripts, groups) {
panel.grid()
panel.superpose(x, y, subscripts, groups)
}
)
thanks again for your time!
christian
-- Far better an approximate answer to the right question, which is often vague, than the exact answer to the wrong question, which can always be made precise -- j.w. tukey
Sorry, bad link. Here it is again. https://stat.ethz.ch/pipermail/r-sig-finance/2006q2/000777.html
On 7/22/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
There are some example here: https://stat.ethz.ch/pipermail/r-sig-finance/2006q2/ On 7/22/07, icosa atropa <icos.atropa at gmail.com> wrote:
Thank you so much for your previous help with zoo and lattice. Following your guidance, I've found the zoo package very powerful. The rollapply demos were also illuminating! I'm wondering if I've found some inconsistency with zoo documentation r.e. NA handling. Both na.omit and na.contiguous are mentioned as methods in the current CRAN pdf reference documentation. The hyperlink for na.omit on p18 lead to p1, and there are no function defs for either in the docs. Similarly, both ?na.omit and ?na.contiguous lead to function defs. Are these unimplemented as generic functions? I'm a bit confused as to the proper context in which to use na.omit and na.contiguous. best, christian On 7/8/07, Gabor Grothendieck <ggrothendieck at gmail.com > wrote:
Try this (and see ?xyplot.zoo for more info):
# first run code in your post that creates the experiment object
# create a time series from experiment
z <- zoo(matrix(experiment$meas, 25, byrow = TRUE))
colnames(z) <- c("NC", "NT", "SC", "ST")
# add grid to existing plot
addGrid <- function() for(i in 1:2) {
trellis.focus("panel", i, 1); panel.grid(); trellis.unfocus()
}
zz <- z
colnames(zz) <- c("N", "NT", "S", "ST")
xyplot(zz, screens = c(1, 1, 2, 2), col = 1:2, layout = 2:1, type = "b")
addGrid()
zz <- z
colnames(zz) <- c("C", "T", "SC", "ST")
xyplot(zz, screens = 1:2, col = c(1, 1, 2, 2), layout = 2:1, type = "b")
addGrid()
On 7/8/07, icosa atropa <icos.atropa at gmail.com> wrote:
Please provide something reproducible and show what you want to do without zoo.
The following example is contrived, but closely resembles my data. I pull my data from a database using RODBC - it comes in as a data.frame with factors, timestamp, and data all globbed together. This is very convenient for plotting with lattice, but complicates time series
analysis.
I can't turn the object into a zoo directly due to the mixed
factor/data.
I've looked at fCalendar, whose timeSeries object has an recordtID
slot...
# Everything from here down tested with R 2.5.0
experiment.layout = expand.grid( tx=c('control', 'treatment'), rep=c(
'N',
'S'), time=1:25)
# ^^ block design experiment
experiment = data.frame( experiment.layout, meas=rnorm(100) )
# ^^the time-indexed dataframe of observations by
experiment$meas=experiment$meas + c(1,-1,2,-2)
# ^^experimental effects differ by bloco and treatment
summary(experiment)
require(lattice)
#
#First split condition on rep, grouping by tx
#
xyplot(meas ~ time | rep, data=experiment, groups=tx,
panel=function(x, y, subscripts, groups) {
panel.grid()
panel.superpose(x, y, subscripts, groups)
}
)
#
#Next condition on tx, grouping by experiment
#
xyplot(meas ~ time | tx, data=experiment, groups=rep,
panel=function(x, y, subscripts, groups) {
panel.grid()
panel.superpose(x, y, subscripts, groups)
}
)
thanks again for your time!
christian
-- Far better an approximate answer to the right question, which is often vague, than the exact answer to the wrong question, which can always be made precise -- j.w. tukey
One other comment. In addition to the 5 NA handling routines for zoo objects illustrated in the link below there is also a 6th NA handling routine in the stinepack package: library(stinepack) ?na.stinterp
On 7/22/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
Sorry, bad link. Here it is again. https://stat.ethz.ch/pipermail/r-sig-finance/2006q2/000777.html On 7/22/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
There are some example here: https://stat.ethz.ch/pipermail/r-sig-finance/2006q2/ On 7/22/07, icosa atropa <icos.atropa at gmail.com> wrote:
Thank you so much for your previous help with zoo and lattice. Following your guidance, I've found the zoo package very powerful. The rollapply demos were also illuminating! I'm wondering if I've found some inconsistency with zoo documentation r.e. NA handling. Both na.omit and na.contiguous are mentioned as methods in the current CRAN pdf reference documentation. The hyperlink for na.omit on p18 lead to p1, and there are no function defs for either in the docs. Similarly, both ?na.omit and ?na.contiguous lead to function defs. Are these unimplemented as generic functions? I'm a bit confused as to the proper context in which to use na.omit and na.contiguous. best, christian On 7/8/07, Gabor Grothendieck <ggrothendieck at gmail.com > wrote:
Try this (and see ?xyplot.zoo for more info):
# first run code in your post that creates the experiment object
# create a time series from experiment
z <- zoo(matrix(experiment$meas, 25, byrow = TRUE))
colnames(z) <- c("NC", "NT", "SC", "ST")
# add grid to existing plot
addGrid <- function() for(i in 1:2) {
trellis.focus("panel", i, 1); panel.grid(); trellis.unfocus()
}
zz <- z
colnames(zz) <- c("N", "NT", "S", "ST")
xyplot(zz, screens = c(1, 1, 2, 2), col = 1:2, layout = 2:1, type = "b")
addGrid()
zz <- z
colnames(zz) <- c("C", "T", "SC", "ST")
xyplot(zz, screens = 1:2, col = c(1, 1, 2, 2), layout = 2:1, type = "b")
addGrid()
On 7/8/07, icosa atropa <icos.atropa at gmail.com> wrote:
Please provide something reproducible and show what you want to do without zoo.
The following example is contrived, but closely resembles my data. I pull my data from a database using RODBC - it comes in as a data.frame with factors, timestamp, and data all globbed together. This is very convenient for plotting with lattice, but complicates time series
analysis.
I can't turn the object into a zoo directly due to the mixed
factor/data.
I've looked at fCalendar, whose timeSeries object has an recordtID
slot...
# Everything from here down tested with R 2.5.0
experiment.layout = expand.grid( tx=c('control', 'treatment'), rep=c(
'N',
'S'), time=1:25)
# ^^ block design experiment
experiment = data.frame( experiment.layout, meas=rnorm(100) )
# ^^the time-indexed dataframe of observations by
experiment$meas=experiment$meas + c(1,-1,2,-2)
# ^^experimental effects differ by bloco and treatment
summary(experiment)
require(lattice)
#
#First split condition on rep, grouping by tx
#
xyplot(meas ~ time | rep, data=experiment, groups=tx,
panel=function(x, y, subscripts, groups) {
panel.grid()
panel.superpose(x, y, subscripts, groups)
}
)
#
#Next condition on tx, grouping by experiment
#
xyplot(meas ~ time | tx, data=experiment, groups=rep,
panel=function(x, y, subscripts, groups) {
panel.grid()
panel.superpose(x, y, subscripts, groups)
}
)
thanks again for your time!
christian
-- Far better an approximate answer to the right question, which is often vague, than the exact answer to the wrong question, which can always be made precise -- j.w. tukey
Hmm. In fact, there is yet another one. We will have to bring all this together properly but for now here is a summary. (My previous posts did not include na.spline.) Note that there is a help page in zoo for ?na.approx and ?na.spline and in stinepackfor ?na.stinterp . library(zoo) zz <- zoo(c(NA, NA, 1:2, NA, 3:5, NA, 6, NA)) na.approx(zz) # linear interpolation na.contiguous(zz) # longest stretch without NAs na.locf(zz) # last occurence (that is not NA) carried forward na.omit(zz) # omit the NAs na.spline(zz) # spline interpolation na.trim(zz) # remove NAs off beginning and end but not interior ones library(stinepack) na.stinterp(zz) # stineman interpolation
library(zoo) zz <- zoo(c(NA, NA, 1:2, NA, 3:5, NA, 6, NA)) na.approx(zz) # linear interpolation
3 4 5 6 7 8 9 10 1.0 2.0 2.5 3.0 4.0 5.0 5.5 6.0
na.contiguous(zz) # longest stretch without NAs
6 7 8 3 4 5
na.locf(zz) # last occurence (that is not NA) carried forward
3 4 5 6 7 8 9 10 11 1 2 2 3 4 5 5 6 6
na.omit(zz) # omit the NAs
3 4 6 7 8 10 1 2 3 4 5 6
na.spline(zz) # spline interpolation
1 2 3 4 5 6 7
8 9 10 11
-4.676471 -1.058824 1.000000 2.000000 2.463235 3.000000 4.000000
5.000000 5.742647 6.000000 5.522059
na.trim(zz) # remove NAs off beginning and end but not interior ones
3 4 5 6 7 8 9 10 1 2 NA 3 4 5 NA 6
library(stinepack) na.stinterp(zz) # stineman interpolation
3 4 5 6 7 8 9 10 1.000000 2.000000 2.500000 3.000000 4.000000 5.000000 5.600269 6.000000