[R] step error

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Wed Apr 6 11:05:16 CEST 2005


an indirect solution is to use the following:

pro <- function(indep, dep){
    d <- data.frame(indep)
    form <- formula(lm(dep~., data=d))
    assign("d", d, envir=.GlobalEnv)
    res <- step(lm(dep~X1, data=d), scope=form, trace=0, 
direction="f")
    rm("d", envir=.GlobalEnv)
    res
}
#########
m <- rnorm(100*5); dim(m) <- c(100, 5); colnames(m) <- paste("X", 1:5, 
sep="")
Y <- as.vector(1 + m[, sample(5, 2)]%*%rnorm(2))
pro(m, Y)
step(lm(Y~X1, data=data.frame(m)), scope=Y~X1+X2+X3+X4+X5, trace=0, 
direction="f")

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "vasilis pappas" <vasileios_p at yahoo.gr>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, April 01, 2005 9:29 PM
Subject: [R] step error


> Could anyone tell me what am I doing wrong?
>
>> pro<-function(indep,dep){
> +  d<-data.frame(indep)
> +  form<-formula(lm(dep~.,data=d))
> +
> forward<-step(lm(dep~X1,data=d),scope=form,trace=0,direction='f')
> +  return(forward)
> + }
>> pro(m,q)
> Error in inherits(x, "data.frame") : Object "d" not
> found
>
> Where q is a vector with the dependent variable's
> values
> and m is a matrix containing the values of the
> independent variables.
>
> While writing the above without a function form has no
> problem, that is :
>
>> d<-data.frame(m)
>> form<-formula(lm(q~.,data=d))
>>
> forward<-step(lm(q~X1,data=d),scope=form,trace=0,direction='f')
>> forward
>
> Call:
> lm(formula = q ~ X1 + X2 + X5, data = d)
>
> Coefficients:
> (Intercept)           X1           X2           X5
>    -15.798        8.765        6.774       -4.245
>
>
> Thank you in advance!
> Vasilis
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list