[R] avoiding expression evaluation when calling a function
Rui Barradas
rui1174 at sapo.pt
Sat Mar 31 03:13:18 CEST 2012
Hello,
btc1 wrote
>
> Hey thanks, that worked. So you're right, I'd like to run it for multiple
> values of i. As it's written, I'm doing it in a for loop, as
>
> plotter<-function(i,fram,framvec,obj,form1,form2){
> temp.i<-fram[framvec <=(i*.10),]
> plot(form1, data=temp.i, xlim=c(0,1500), ylim=c(0,35), main=(i*.10))
> mod<-lm(form2,data=temp.i)
> r2<-summary(mod)$adj.r.squared
> rsqrd[i]<-r2
> legend("bottomright", legend=signif(r2), col="black")
> abline(mod)
> rsqrd
> }
> for(i in 0:10){
> rsqrd<-plotter(i,fram,framvec,obj,form1,form2 )
> }
> plot(rsqrd)
> abline(lm(rsqrd~c(1:10)))
>
> i tried using your suggestion for lapply, but don't fully understand that
> function (even after reading the help file).
>
> rrsqrd <- lapply(1:10, function(my.i)
> plotter(i=my.i,fram=rwb,framvec=rwb$prcnt.char.depth,obj=prcnt.char.depth,form1=
> post.f.crwn.length~leaf.area,form2=post.f.crwn.length~leaf.area-1))
>
> This must be doing something in the background I can't see, though,
> because
> it took my computer a few seconds to run the first time, and locked it up
> for a couple minutes the second.
>
> If anyone cares to explain, thanks - otherwise I'll limp along with my old
> for loop.
>
> Best
>
> On Fri, Mar 30, 2012 at 12:42 PM, Joshua Wiley <jwiley.psych@>wrote:
>
>> Hi Benjamin,
>>
>> See inline
>>
>> On Fri, Mar 30, 2012 at 12:31 PM, Benjamin Caldwell
>> <btcaldwell@> wrote:
>> > Another question on functions - I have something that looks like
>> >
>> > plotter<-function(i){
>> > temp.i<-rwb[rwb$vector1 <=(i*.10),]
>> > with(temp.i, plot(vector2, vector3, main=(i*.10),))
>> > mod<-lm(vector3~vector3-1,data=temp.i)
>> > r2<-summary(mod)$adj.r.squared
>> > rsqrd[i]<-r2
>> > legend("bottomright", legend=signif(r2), col="black")
>> > abline(mod)
>> > rsqrd<<-rsqrd
>> > }
>> >
>> >
>> > I'd rather not have to go into the function and re-type each vector and
>> > dataframe as I apply it to different vectors. My idea is something
>> like:
>> >
>>
>> Close, but pass a formula object. Something like (untested)
>>
>>
>> plotter <- function(i, frm, obj, form){
>> temp.i<-frm[frm$obj <=(i*.10),]
>> plot(form, main=(i*.10), data = temp.i)
>> mod<-lm(form, data=temp.i)
>> r2<-summary(mod)$adj.r.squared
>> legend("bottomright", legend=signif(r2), col="black")
>> abline(mod)
>> return(rsqrd)
>> }
>>
>> and I guess you are doing this for different values of i? so maybe
>>
>>
>> rsqrd <- lapply(1:100, function(my.i) plotter(i = my.i, frm =
>> your_frm, obj = your_obj, form = y ~ x))
>>
>> Hope this helps,
>>
>> Josh
>>
>>
>>
>> > plotter<-function(i,frm,obj,x,y){
>> > temp.i<-frm[frm$obj <=(i*.10),]
>> > with(temp.i, plot(x, y, main=(i*.10),))
>> > mod<-lm(y~x-1,data=temp.i)
>> > r2<-summary(mod)$adj.r.squared
>> > rsqrd[i]<-r2
>> > legend("bottomright", legend=signif(r2), col="black")
>> > abline(mod)
>> > rsqrd<<-rsqrd
>> > }
>> >
>> > However, it seems that when I do this the function is calling x, y from
>> the
>> > global environment rather than simply passing the text and using the
>> frame
>> > temp.i I create in the function. One idea I had was to go from read.csv
>> to
>> > frm, obj entirely within the function, but that's about 110 lines of
>> code
>> > each time.
>> >
>> > I'm hoping there's a way to have some of the expressions passed within
>> the
>> > function without evaluation, in this case without calling data from the
>> > global environment.
>> >
>> > I've tried substitute(),
>> >
>> > plotter<-function(i,frm,obj,x,y){
>> > temp.i<-frm[frm$obj <=(i*.10),]
>> > with(temp.i, plot(substitute(x), substitute(y), xlim=c(0,250),
>> > ylim=c(0,35), main=(i*.10),))
>> > mod<-lm(substitute(y)~substitute(x)-1,data=temp.i)
>> > r2<-summary(mod)$adj.r.squared
>> > rsqrd[i]<-r2
>> > legend("bottomright", legend=signif(r2), col="black")
>> > abline(mod)
>> > rsqrd<<-rsqrd
>> > }
>> > but am told "cannot coerce type 'symbol' to vector of type 'double'"
>> >
>> >
>> > Any suggestions appreciated.
>> >
>> > Thanks
>> >
>> > Ben
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help@ 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.
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> Programmer Analyst II, Statistical Consulting Group
>> University of California, Los Angeles
>> https://joshuawiley.com/
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@ 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.
>
I don't know why it takes minutes, but
1. Your function doesn't use the parameter 'obj'. If the function does what
you expect it to do,
get rid of it.
2. In the lapply, look at the values you pass for 'framvec' and 'obj'.
Are 'rwb$prcnt.char.depth' and 'prcnt.char.depth' the same? It's at best
confusing. Have attached
the data.frame 'rwb'?
3. Your loop is for i in 0:10 and the lapply for i (my.i) in 1:10.
4. The loop overwrites 'rsqrd' every time through, the lapply returns a list
of all 10 vectors.
I believe this last point is where the problem is. In your function you have
an assignment
rsqrd[i] <- r2
but for i == 2 or greater, this should give an error,
Error in rsqrd[i] <- r2 : object 'rsqrd' not found
Unless you're using the double arrow assignment, '<<-'
As for not understanding lapply, it seems you do understand it, but there's
more going on with your code
that's making it work in a strange way.
Hope this helps,
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/avoiding-expression-evaluation-when-calling-a-function-tp4520151p4520876.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list