Skip to content

accessing current factor in tapply

5 messages · Bernie McConnell, Sundar Dorai-Raj, Tony Plate +2 more

#
G'Day,

I want to access in a function called from tapply the current factor.  In 
my example below, all I want to do is to write the current factor on each 
histogram.  Needless to say my example does not work. I would be grateful 
for pointers in the right direction.

Many thanks
Bernie McConnell
Sea Mammal Reserach Unit


cc <- 1:10
ff <- rep(c("a","b"),5)

pp<- function(x,f) {
   hist(x, main=as.character(f))
   }


tapply(aa, ff, pp, f=ff)
#
Bernie McConnell wrote:
I think it would be easier and more foolproof to use a for loop:

cc <- 1:10
ff <- rep(c("a", "b"), 5)
par(mfrow = c(1, 2))
for(f in unique(ff))
   hist(cc[ff==f], main = as.character(f))

Regards,
Sundar
#
Here's a way (but as Sundar Dorai-Raj suggests, it might be easier to use a 
for loop.)

 > cc <- 1:10
 > ff <- rep(c("a","b"),5)
 > # a different function, so that we can see what data and what label it gets
 > data.lab <- function(data,lab) paste(paste(data, collapse=" "), 
paste(as.character(lab), collapse=" "), sep="/")
 > # ii is indices of each group of ff
 > ii <- tapply(cc, ff, c)
 > sapply(seq(along=ii), function(j, ii, cc, labs) 
data.lab(data=cc[ii[[j]]],lab=labs[j]), ii=ii, cc=cc, labs=names(ii))
[1] "1 3 5 7 9/a"  "2 4 6 8 10/b"
 >
 >
At Thursday 06:13 PM 4/17/2003 +0100, you wrote:
#
I've just been puzzling through this for my own use.
There are three different R functions which do related
things:  tapply(), by() and aggregate().  In this context,
I think I would use  by()  rather than  tapply().  Define
cc, ff as before, and re-define

pp <- function(dat, name)
	hist(dat$x, main=as.character(unique(dat[[name]])))
then

by(data.frame(cc,ff), ff, pp, "ff")	#  should do what you want

The slightly odd syntax at the end of pp() allows its second
argument "name" to be either a string naming the selection
variable (as here) or else an integer specifying it by position
within the data frame.  If you want to split the data set on
the cross product of two factors, ff x gg, the second argument
to by() would have to be  list(ff,gg).  tapply(), by contrast,
requires the list() syntax even when there's only one factor
to split on.

HTH  -  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Thu, 17 Apr 2003, Bernie McConnell wrote:

            
#
Using the LakeHuron data, fit a simple AR(2) model:

 > data(LakeHuron) 
 > ar.lh <- arima(LakeHuron, order = c(2,0,0))
 > ar.lh

Call:
arima(x = LakeHuron, order = c(2, 0, 0))

Coefficients:
         ar1      ar2  intercept
      1.0436  -0.2495   579.0473
s.e.  0.0983   0.1008     0.3319

sigma^2 estimated as 0.4788:  log likelihood = -103.63,  aic = 215.27

Make a 1-step ahead forecast:

 > predict(ar.lh,1)[[1]]
Time Series:
Start = 1973
End = 1973
Frequency = 1
[1] 579.7896

Compute the forecast manually:

 > sum(ar.lh$coef*c(c(579.96,579.89)-ar.lh$coef[3],1))
[1] 579.7896

This just says that the forecast for the next period (after the end of 
the data) is 579.0473 + 1.0436*(579.96 - 579.0473) - 0.2495*(579.89 - 
579.0473). In other words: the forecast is the intercept plus the AR 
coefficients times the  (previous ts values minus the intercepts).

Now add an exogenous variable (in this case, the (year - 1920):

 > ar.lh <- arima(LakeHuron, order = c(2,0,0), xreg = time(LakeHuron)-1920)
 > ar.lh

Call:
arima(x = LakeHuron, order = c(2, 0, 0), xreg = time(LakeHuron) - 1920)

Coefficients:
         ar1      ar2  intercept  time(LakeHuron) - 1920
      1.0048  -0.2913   579.0993                 -0.0216
s.e.  0.0976   0.1004     0.2370                  0.0081

sigma^2 estimated as 0.4566:  log likelihood = -101.2,  aic = 212.4

The prediction is:

 > predict(ar.lh,1,newxreg=53)[[1]]
Time Series:
Start = 1973
End = 1973
Frequency = 1
[1] 579.3972


Now try to manually forecast where the next time period is 53:

 > sum(ar.lh$coef*c(c(579.96,579.89)-ar.lh$coef[3],1,53))
[1] 578.5907

What am I doing wrong? I've tried this with numerous examples and 
whenever there is an exogenous variable I cannot get the manual forecast 
to agree with predict. Is it not correct to just add (-0.0216 times 53) 
to the rest? I need to know how to write the model correctly. Obviously 
there is something I am overlooking. R's arima function and predict 
function work correctly - at least they agree with SAS for example so 
I'm not doing something right.

I would really appreciate some insight here.

Rick B.