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 <[email protected]>wrote:
> Hi Benjamin,
>
> See inline
>
> On Fri, Mar 30, 2012 at 12:31 PM, Benjamin Caldwell
> <[email protected]> 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]]
> >
> > ______________________________________________
> > [email protected] 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]]
______________________________________________
[email protected] 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.