Dear all;
Is there any way to make this to work?:
.x<-rnorm(50,10,3)
.y<-.x+rnorm(50,0,1)
X<-data.frame(.x,.y)
colnames(X)<-c("Xvar","Yvar")
Ytext<-"Yvar"
lm(Ytext~Xvar,data=X) # doesn't run
lm(Yvar~Xvar,data=X) # does run
The main idea is to use Ytext as input in a function, so you just type
"Yvar" and the model should fit....
Also, I need to avoid the expression X$Yvar~X$Xvar
Thanks for any idea
PM
Why not using: lm(X[[Ytext]]~Xvar,data=X) ThPe Pedro Mardones wrote:> Dear all; > > Is there any way to make this to work?: > > .x<-rnorm(50,10,3) > .y<-.x+rnorm(50,0,1) > > X<-data.frame(.x,.y) > colnames(X)<-c("Xvar","Yvar") > > Ytext<-"Yvar" > > lm(Ytext~Xvar,data=X) # doesn't run > > lm(Yvar~Xvar,data=X) # does run > > The main idea is to use Ytext as input in a function, so you just type > "Yvar" and the model should fit.... > Also, I need to avoid the expression X$Yvar~X$Xvar > > Thanks for any idea > > PM > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.
First check the value of Ytext. Try Ytext <- X$Yvar --- Pedro Mardones <mardones.p at gmail.com> wrote:> Dear all; > > Is there any way to make this to work?: > > .x<-rnorm(50,10,3) > .y<-.x+rnorm(50,0,1) > > X<-data.frame(.x,.y) > colnames(X)<-c("Xvar","Yvar") > > Ytext<-"Yvar" > > lm(Ytext~Xvar,data=X) # doesn't run > > lm(Yvar~Xvar,data=X) # does run > > The main idea is to use Ytext as input in a > function, so you just type > "Yvar" and the model should fit.... > Also, I need to avoid the expression X$Yvar~X$Xvar > > Thanks for any idea > > PM > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, > reproducible code. >
Try:> lm( formula( paste( Ytext, '~ Xvar' ) ), data=X)-- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Pedro Mardones > Sent: Thursday, June 14, 2007 1:14 PM > To: R-help at stat.math.ethz.ch > Subject: [R] question about formula for lm > > Dear all; > > Is there any way to make this to work?: > > .x<-rnorm(50,10,3) > .y<-.x+rnorm(50,0,1) > > X<-data.frame(.x,.y) > colnames(X)<-c("Xvar","Yvar") > > Ytext<-"Yvar" > > lm(Ytext~Xvar,data=X) # doesn't run > > lm(Yvar~Xvar,data=X) # does run > > The main idea is to use Ytext as input in a function, so you > just type "Yvar" and the model should fit.... > Also, I need to avoid the expression X$Yvar~X$Xvar > > Thanks for any idea > > PM > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
A couple of easy ways are to create the calling sequence as a call or
character string and then evaluate it:
eval(bquote(lm(.(as.name(Ytext)) ~ Xvar, X)))
eval(parse(text = paste("lm(", Ytext, "~ Xvar, X)")))
Note that these solutions both have the advantage over some of the prior
solutions that the output from print.lm shows which variable was actually used
after Call:
> eval(bquote(lm(.(as.name(Ytext)) ~ Xvar, X)))
Call:
lm(formula = Yvar ~ Xvar, data = X) <--- This line comes out meaningfully!!!
Coefficients:
(Intercept) Xvar
0.3300 0.9729
On 6/14/07, Pedro Mardones <mardones.p at gmail.com>
wrote:> Dear all;
>
> Is there any way to make this to work?:
>
> .x<-rnorm(50,10,3)
> .y<-.x+rnorm(50,0,1)
>
> X<-data.frame(.x,.y)
> colnames(X)<-c("Xvar","Yvar")
>
> Ytext<-"Yvar"
>
> lm(Ytext~Xvar,data=X) # doesn't run
>
> lm(Yvar~Xvar,data=X) # does run
>
> The main idea is to use Ytext as input in a function, so you just type
> "Yvar" and the model should fit....
> Also, I need to avoid the expression X$Yvar~X$Xvar
>
> Thanks for any idea
>
> PM
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
Apparently Analagous Threads
- by (tapply) and for loop differences
- using eval to handle column names in function calling scatterplot graph function
- Writing a helper function that takes in the dataframe and variable names and then does a subset and plot
- speed up process
- Increasing number of observations worsen the regression model