[R] using step function in functions
Barry Feldman
bfeldman at ibbotson.com
Mon Sep 30 22:54:54 CEST 2002
Help!
I am a new R user. It has been slow getting up to speed, but definitely
rewarding. I have come up against a problem I can't handle. I would
very much appreciate any help.
I am writing a vector auto regression (VAR) function that utilizes existing
R statistical functions. I would like to use the step function to do
step-wise elimination on each univariate time series model. No matter
what I do, I can't get step() to work inside a function, although it seems
to work perfectly well through the GUI.
I get an error such as the following when I try to run the function:
Error in eval(expr, envir, enclos) : Object "SP500" not found
In this case the object is the dependent variable. In other cases it can
be one of the independent variables. I've tried everying I can think of.
The code for the function follows. It should work with any numeric
variables
and specified number of lags.
Thanks in advance,
Barry Feldman
Barry Feldman (p) 312-616-9213
Ibbotson Associates (f) 312-616-0404
225 North Michigan Ave.
Chicago, IL 60601 U.S.A.
# Vector Auto Regression
VAR <- function(rData0,lagz) {
# build regression datasets
rData<-na.omit(rData0)
dimz<-dim(rData)
depDat<-rData[(lagz+1):dimz[1],]
print((dim(depDat))[1])
for (i in 1:lagz) {
cDat<-rData[(lagz-i+1):(dimz[1]-i),]
for (j in 1:dimz[2]) {
names(cDat)[j]<-paste(names(cDat)[j],"xL", i,
sep="",collapse=NULL)
}
if (i==1) indDat<-cDat else indDat<-cbind(indDat,cDat)
}
# build regression models
for (i in 1:dimz[2]) {
subData<-cbind(depDat[[i]],indDat)
names(subData)[1]<-names(rData)[i]
# make model where first variable in frame is the dependent variable
regOut00<-eval(substitute(lm( dV ~ . , data=subData),
list(dV = as.name(names(subData)[1]))
))
regOut0<-step(regOut00)
#regOut0<-eval(step(regOut00),envir=subData,enclos=parent.frame())
regOut0<-list(regOut0)
if (i>1) regOut<-c(regOut,regOut0) else regOut<-regOut0
}
#subData
regOut
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list