Dear S+ users,
Under
S-PLUS 2000 Professional Edition Release 2 for Microsoft Windows : 1999,
with Windows 95, I wrote the following function
to compare two models according BIC criteria.
myfun _ function(){
set.seed(1)
x _ arima.sim(n=100, model=list(ar=.9))
model.1 _ lm( x[3:100] ~ x[2:99] )
model.2 _ lm( x[3:100] ~ x[2:99] + x[1:98] )
BIC(model.1,model.2)
}
When I run the function, I have the following error
message:
> myfun()
myfun()
Error in function(object, ...): Object "x" not found
>
So I looked in the code of the function logLik.lm(),
and I found that there is an update() operation.
# ==== part of the code of lokLik.lm() ==============
if(is.null(object$qr))
object <- update(object, qr = T)
# ===================================================
So if I modify my original code in the following way:
myfun.qr _ function(){
set.seed(1)
x _ arima.sim(n=100, model=list(ar=.9))
model.1 _ lm( x[3:100] ~ x[2:99], qr=T )
model.2 _ lm( x[3:100] ~ x[2:99] + x[1:98], qr=T )
BIC(model.1,model.2)
}
all is now fine. But I avoid the error message since there is no
update step! It seems that in the update step, the function
is not able to find the data. Is it related to a problem
of data.frame? Is there a way to tell to logLik.lm()
where are the data in my original function myfun()?
I tried the following but that does not work:
myfun2 _ function(){
set.seed(1)
x _ arima.sim(n=100, model=list(ar=.9))
mydata _ cbind(x[3:100],x[2:99],x[1:98])
mydata _ as.data.frame(mydata)
model.1 _ lm( x[3:100] ~ x[2:99], data=mydata )
model.2 _ lm( x[3:100] ~ x[2:99] + x[1:98] , data=mydata)
BIC(model.1,model.2)
}
I presume that I do not understand well the data.frame in that
situation.
Thanks for your help,
P.S. The drastic function myfun3() works, but I prefer to avoid
the 'terrible' operator "<<-".
myfun3 _ function(){
set.seed(1)
x <<- arima.sim(n=100, model=list(ar=.9))
model.1 _ lm( x[3:100] ~ x[2:99] )
model.2 _ lm( x[3:100] ~ x[2:99] + x[1:98] )
BIC(model.1,model.2)
}
--
Au plaisir, Pierre.
_____________________________________________________________________
Pierre Duchesne, professeur adjoint,
Service de l'enseignement des methodes quantitatives de gestion (MQG)
Ecole des Hautes Etudes Commerciales (http://www.hec.ca/mqg)
3000, chemin de la Cote-Sainte-Catherine, Montreal(QC) Canada H3T 2A7
Bureau: 4.821 Tel:(514)340-6503 Fax:(514)340-5634
Page WEB: http://www.hec.ca/profs/pierre.duchesne.html
=====================================================================
|