Justin,
Thank you for taking time out on a Sunday to help me at such short
notice! Your solution works and is very much appreciated :-)
I’ll keep you posted on any new developments.
Zebedee
2009/9/7 Justin Grimmer <jgrimmer(a)fas.harvard.edu>du>:
Dear Zebedee,
Thanks for pointing out that my previous email was less than helpful--I
misunderstood the question. After spending some time with your code, I've
been able to replicate the error and I have a solution, but please let me
know if this doesn't solve the problem.
After you load your data, you have a few lines of code that create time
series
#######
CH.ts <- ts(Data[,2], start = c(2002, 1), end = c(2008, 22), freq = 52)
PK.ts <- ts(Data[,3], start = c(2002, 1), end = c(2008, 22), freq = 52)
BF.ts <- ts(Data[,4], start = c(2002, 1), end = c(2008, 22), freq = 52)
CHPK.ts<- ts(Data[,5], start = c(2002, 1), end = c(2008, 22), freq = 52)
CHBF.ts<- ts(Data[,6], start = c(2002, 1), end = c(2008, 22), freq = 52)
#######
Let's focus on CHPK.ts and check its length quickly:
#########
length(CHPK.ts)
[1] 334
##########
But if we check your original data frame, we'll see the dimensions don't
match:
###########
dim(Data)
[1] 339 6
###########
To append a column to a data frame, you'll need the length of the vector
(CHPK.ts) to have the same number of rows as the data frame (Data). Since
they are different in this case, you get an error:
#################
cbind(Data, as.numeric(CHPK.ts))
Error in
data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 339, 334
#################
One solution is to create a new data frame that just includes the
information you'll use for a particular time series (this process can be
repeated to append additional time series if you want to model several).
From your code, it looks like you're interested in modeling
``dtrdseas.log.CHPK.ts" which you create using this code:
################
dtrdseas.log.CHPK.ts<- log(CHPK.ts) -
(lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit) #* I need help with this line
###############
Let's coerce this ``ts" object into a data frame. To do this, we run the
following code:
##################
dtrdseas.log.CHPK.ts<- as.data.frame(as.numeric(dtrdseas.log.CHPK.ts))
###############
one more thing. we need to make sure zelig knows that the one column in
this data frame is named ``dtrdseas.log.CHPK.ts" (which you use to call it
in your Zelig code). So, let's give the column a name,
###############
colnames(dtrdseas.log.CHPK.ts)<- 'dtrdseas.log.CHPK.ts'
################
Let's use this data frame to rerun the model
###############
z.out <- zelig(Diff(dtrdseas.log.CHPK.ts, 0) ~ lag.y(3) + lag.eps(3), model
= "arima", data = dtrdseas.log.CHPK.ts)
How to cite this model in Zelig:
Justin Grimmer. 2007. "arima: Arima models for Time Series Data" in Kosuke
Imai, Gary King, and Olivia Lau, "Zelig: Everyone's Statistical
Software,"
http://gking.harvard.edu/zelig
##############
One more point as you're moving this forward. on the x.out line, you need to
specify how many time periods ahead you want to forecast. to do this, add a
``pred.ahead" option. So, when setting the x values, you can add the
command (to predict 10 units of time in the future)
##########
x.out <- setx(z.out, pred.ahead=10)
###########
you can then use this in the sim function
##########
s.out <- sim(z.out, x=x.out)
##########
Thanks for posting this question to the list and please let me know if you
need some more help,
Justin
On Sun, Sep 6, 2009 at 10:38 PM, Zebedee Nii-Naate
<zebedeeniinaate(a)gmail.com> wrote:
>
> Dear Justin,
>
> Thank you for your advice.
>
> Removing "Data&" from line 48 does resolve the issues with that syntax
> but it takes me back to my initial problem - that I'm unable to run
> line 85: "z.out <- zelig(Diff(dtrdseas.log.CHPK.ts, 0) ~ lag.y(3) +
> lag.eps(3), model = "arima", data = Data)" because the series
> dtrdseas.log.CHPK.ts is not held in "Data".
>
> I need to somehow add my new detrended and deseasonalised series
> "dtrdseas.log.CHPK.ts" to the data.frame and then run your zelig
> command.
>
> Regards,
>
> Zebedee
>
> 2009/9/7 Justin Grimmer <jgrimmer(a)fas.harvard.edu>du>:
> > Hi Zebedee,
> > If you replace the command
> > Data$dtrdseas.log.CHPK.ts<- log(CHPK.ts) -
> > (lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit)
> > with the following
> > dtrdseas.log.CHPK.ts<- log(CHPK.ts) -
> > (lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit)
> > the code should run without error.
> > Thanks for using our software and please don't hesitate to answer
> > questions
> > in the future--
> > Cheers
> > Justin Grimmer
> > PhD Candidate
> > Harvard University
> > On Sat, Sep 5, 2009 at 12:05 PM, Zebedee Nii-Naate
> > <zebedeeniinaate(a)gmail.com> wrote:
> >>
> >> Thank you very much! I'm definately heading in the right direct :-)
> >>
> >> After running:
> >>
> >> > Data$dtrdseas.log.CHPK.ts<- log(CHPK.ts) -
> >> > (lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit)
> >>
> >> I get:
> >>
> >> Error in `$<-.data.frame`(`*tmp*`, "dtrdseas.log.CHPK.ts",
value =
> >> c(0.0702719580951204, :
> >> replacement has 334 rows, data has 339
> >>
> >> My arima model has 5 parameters so it looks like I've lost 5 degrees
> >> of freedom, hence the number of rows do not match the original data.
> >> I'll
> >> have a look at your manuals and archives for a solution. I'd be very
> >> grateful for any advice, if you have the time.
> >>
> >> Regards,
> >>
> >> Zebedee
> >>
> >> 2009/9/5 Olivia Lau <olivia.lau(a)post.harvard.edu>
> >>>
> >>> Hi, Zebedee,
> >>>
> >>> It seems that you're missing some important information in the
zelig
> >>> call. The most important thing for the zelig function is that you
> >>> have all the variables in one data frame, which is specified using the
> >>> data argument in the zelig call.
> >>>
> >>> To insert variables in your data frame Data, the line
> >>> dtrdseas.log.CHPK.ts <-
> >>> log(CHPK.ts)-(lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit)
> >>> should be
> >>> Data$dtrdseas.log.CHPK.ts<- log(CHPK.ts) -
> >>> (lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit)
> >>> where the $ inserts this variable in the Data dataframe.
> >>>
> >>> Next, the zelig call should be:
> >>> z.out <- zelig(Diff(dtrdseas.log.CHPK.ts, 0) ~ lag.y(3) +
> >>> lag.eps(3),
> >>> model = "arima", data = Data)
> >>> where Data is the data frame into which you have inserted your
> >>> dtrdseas.log.CHPK.ts variable.
> >>>
> >>> Please let us know whether this fixes the problem.
> >>>
> >>> Yours,
> >>> Olivia Lau
> >>> Zelig[[3]]
> >>>
> >>>
> >>>
> >>>
> >>> On Sat, Sep 5, 2009 at 10:37 AM, Zebedee
> >>> Nii-Naate<zebedeeniinaate(a)gmail.com> wrote:
> >>> > Thank you Olivia.
> >>> >
> >>> > I'm using R version 2.9.2 and Zelig version 3.4-5 (built:
> >>> > 2009-03-13).
> >>> > I'm
> >>> > praying your colleague is able to have a look early week, as
I'm due
> >>> > to
> >>> > hand
> >>> > in my dissertation on Wed of next week.
> >>> >
> >>> > btw - I think you forgot to cc in colleague.
> >>> >
> >>> > Kind regards,
> >>> >
> >>> > Zebedee
> >>> >
> >>> > 2009/9/5 Olivia Lau <olivia.lau(a)post.harvard.edu>
> >>> >>
> >>> >> Dear Zebedee,
> >>> >>
> >>> >> The student who worked on ARIMA is currently at APSA.
I've cc'ed
> >>> >> him
> >>> >> above, and hopefully he'll have a chance to take a look
when he
> >>> >> gets
> >>> >> back next week.
> >>> >>
> >>> >> Which version of Zelig and which version of R are you using?
> >>> >>
> >>> >> Yours,
> >>> >> Olivia
> >>> >>
> >>> >> On Sat, Sep 5, 2009 at 3:42 AM, Zebedee
> >>> >> Nii-Naate<zebedeeniinaate(a)gmail.com> wrote:
> >>> >> > Dear all,
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > Good morning all. I’m trying to us the Zelig::zelig
function to
> >>> >> > estimate an
> >>> >> > ARIMA model after I have removed the seasonal and trend
> >>> >> > components
> >>> >> > of a
> >>> >> > time
> >>> >> > series data (CHPH.ts) that I have imported from a CV
file.
> >>> >> > Please
> >>> >> > see
> >>> >> > syntax below.
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > All seems to be ok apart from when I run: z.out <-
> >>> >> > zelig(Diff(dtrdseas.log.CHPK.ts, 0) ~ lag.y(3) +
lag.eps(3),
> >>> >> > model =
> >>> >> > "arima"
> >>> >> > , ,)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > I get the following error message:
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > Error in inherits(x, "factor") : element 1 is
empty;
> >>> >> >
> >>> >> > the part of the args list of 'class' being
evaluated was:
> >>> >> >
> >>> >> > (data)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > I’ve tried changing the syntax to include: data = Data and
data =
> >>> >> > dtrdseas.log.CHPK.ts plus many alternatives. Nothing
seems to
> >>> >> > work.
> >>> >> > I’d be
> >>> >> > very grateful for advice on the error message and how
force the
> >>> >> > zelig
> >>> >> > function to read dtrdseas.log.CHPK.ts in the zelig
function.
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > I’ve read material on the zelig website
> >>> >> > (
http://gking.harvard.edu/zelig)
> >>> >> > but
> >>> >> > I’m still lost.
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > Syntax
> >>> >> >
> >>> >> > #Import Data from csv file
> >>> >> >
> >>> >> > Data
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> >
<-read.csv("C:/Users/Zebedee/Documents/Dissertation/Data/Germany.csv",header=T)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > #Name the first column of csv file as Data and format the
Data
> >>> >> >
> >>> >> > Datecol<-Data[,1]
> >>> >> >
> >>> >> > as.Date(as.character(Datecol),format="%Y%m%d")
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > #Report the dimensions of Data and list
> >>> >> >
> >>> >>
dim(Data)
>
>>> >> >
> >>> >> > names (Data)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > ## DESCRIPTIVE STATISTICS
> >>> >> >
> >>> >> > # Plots in levels
> >>> >> >
> >>> >> > CH.ts <- ts(Data[,2], start = c(2002, 1), end = c(2008,
22), freq
> >>> >> > =
> >>> >> > 52)
> >>> >> >
> >>> >> > PK.ts <- ts(Data[,3], start = c(2002, 1), end = c(2008,
22), freq
> >>> >> > =
> >>> >> > 52)
> >>> >> >
> >>> >> > BF.ts <- ts(Data[,4], start = c(2002, 1), end = c(2008,
22), freq
> >>> >> > =
> >>> >> > 52)
> >>> >> >
> >>> >> > CHPK.ts<- ts(Data[,5], start = c(2002, 1), end =
c(2008, 22),
> >>> >> > freq =
> >>> >> > 52)
> >>> >> >
> >>> >> > CHBF.ts<- ts(Data[,6], start = c(2002, 1), end =
c(2008, 22),
> >>> >> > freq =
> >>> >> > 52)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > #Deseasonalise and detrend data
> >>> >> >
> >>> >> > t<-1: length(log(CHPK.ts))
> >>> >> >
> >>> >> > t2<-t^2
> >>> >> >
> >>> >> > t3<-t^3
> >>> >> >
> >>> >> > sin.t<-sin(2*pi*t/52)
> >>> >> >
> >>> >> > cos.t<-cos(2*pi*t/52)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > par(mfrow=c(2,1))
> >>> >> >
> >>> >> > plot(log(CHPK.ts))
> >>> >> >
> >>> >> > trd.log.CHPK.ts<-lm(log(CHPK.ts)~t+t2+t3+sin.t+cos.t)
> >>> >> >
> >>> >> >
lines(lm(log(CHPK.ts)~t+t2+t3+sin.t+cos.t)$fit,col=2,lwd=2)
> >>> >> >
> >>> >> > summary(lm(log(CHPK.ts)~t+t2+t3+sin.t+cos.t))
> >>> >> >
> >>> >> > AIC(trd.log.CHPK.ts)
> >>> >> >
> >>> >> > tsDyn::BIC(trd.log.CHPK.ts)
> >>> >> >
> >>> >> > lmtest::bptest(trd.log.CHPK.ts)
> >>> >> >
> >>> >> > lmtest::resettest(trd.log.CHPK.ts)
> >>> >> >
> >>> >> > lmtest::bgtest(trd.log.CHPK.ts, order = 52)
> >>> >> >
> >>> >> > lmtest::dwtest(trd.log.CHPK.ts)
> >>> >> >
> >>> >> > dtrdseas.log.CHPK.ts<- log(CHPK.ts) -
> >>> >> > (lm(log(CHPK.ts)~t+t2+t3+t3+sin.t+cos.t)$fit)
> >>> >> >
> >>> >> > plot(dtrdseas.log.CHPK.ts)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > #Estimate ARIMA model
> >>> >> >
> >>> >> > library(Zelig)
> >>> >> >
> >>> >> > z.out <- zelig(Diff(dtrdseas.log.CHPK.ts, 0) ~ lag.y(3)
+
> >>> >> > lag.eps(3),
> >>> >> > model
> >>> >> > = "arima" , ,)
> >>> >> >
> >>> >> > summary(z.out)
> >>> >> >
> >>> >> > x.out <- setx(z.out)
> >>> >> >
> >>> >> > s.out <- sim(z.out)
> >>> >> >
> >>> >> > s.out <- sim(z.out, x = x.out)
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > Kind regards,
> >>> >> >
> >>> >> >
> >>> >> >
> >>> >> > Zebedee
> >>> >> >
> >>> >> >
> >>> >
> >>> >
> >>
> >
> >
-
Zelig Mailing List, served by Harvard-MIT Data Center
Send messages: zelig(a)lists.gking.harvard.edu
[un]subscribe Options: