[R] How to use a function inside a regression formula

Duncan Murdoch murdoch.duncan at gmail.com
Fri Sep 30 21:02:30 CEST 2011


On 30/09/2011 2:07 PM, Jim Cheng wrote:
> Hi,
>
> How do I use a function inside a regression formula? When I tried to use
> min() function inside a regression formula,  I ran into errors.   Here are
> the commands to reproduce the error:
>
>
> mydata<- read.csv(url("http://www.ats.ucla.edu/stat/r/dae/binary.csv"))
>
> attach(mydata)
>
> names(mydata)
>
> mylm2 = lm(admit~gre+I(max(gpa,rank)))
>
> *Error in model.frame.default(formula = admit ~ gre + I(max(gpa, rank)),  :
>    variable lengths differ (found for 'I(min(gpa, rank))')*

min(gpa, rank) will be a length 1 vector.  You want pmin(gpa, rank).  (I 
haven't tried it in the regression, but it should work.)

Duncan Murdoch
> *
> *
>
> *
> *
>
> Regards,
>
> David
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list