[R] [GGplot] Geom_smooth with formula "power"?
David Winsemius
dwinsemius at comcast.net
Tue Nov 10 18:47:58 CET 2015
> On Nov 9, 2015, at 7:19 AM, Catarina Silva <bolseiro.raiz.csilva at portucelsoporcel.com> wrote:
>
> I've tried others initial solutions and the adjustement was done to power model in ggplot - geom_smooth.
> But, with "nls" I can't do the confidence interval with ggplot - geom_smooth? I read that with "nls" we have to force "se=FALSE". Is this true?
Well, sort of. Setting `se = FALSE` prevents the ggplot2 functions from trying to force nls and nls.predict to do something that is not in their design.
> How can I draw confidence interval in the plot?
>
> I've done this:
>> ggplot(data,aes(x = idade,y = v_mt)) +
> + geom_point(alpha=2/10, shape=21,fill="darkgray", colour="black", size=3) +
> + geom_smooth(method = 'nls', formula = y ~ a * x^b, start = list(a=1,b=2),se=FALSE)
>>
> And then I don't have the confidence interval.
>
> If I do:
>> ggplot(data,aes(x = idade,y = v_mt)) +
> + geom_point(alpha=2/10, shape=21,fill="darkgray", colour="black", size=3) +
> + geom_smooth(method = 'nls', formula = y ~ a * x^b, start = list(a=1,b=2))
> Error in pred$fit : $ operator is invalid for atomic vectors
>>
>
> Return error…
>
Read the help page for nls. The ‘se.fit' parameter is set to FALSE and efforts to make it TRUE will be ignored. So `predict.nls` simply does not return std-error estimates in the typical manner of other predict.* functions. I believe this is because the authors of `nls` did not think there was a clear answer to the question of what confidence bounds should be returned.
If you want to add confidence bounds to an nls, then you need to decide what bounds to add, and then use the ggplot2 line-drawing functions to overlay them on your own. I found posts in Rhelp that pointed me to the ‘nls2' package, but when I tried to run the code I got messages saying that the `as.lm` function could not be found.
http://markmail.org/message/7kvolf5zzpqyb7l2?q=list:org%2Er-project%2Er-help+as%2Elm+is+a+linear+model+between+the+response+variable+and+the+gradient
> require(nls2)
Loading required package: nls2
Loading required package: proto
> fm <- nls(demand ~ SSasympOrig(Time, A, lrc), data = BOD)
> predict(as.lm(fm), interval = "confidence")
Error in predict(as.lm(fm), interval = "confidence") :
could not find function "as.lm"
> getAnywhere(as.lm)
no object named ‘as.lm’ was found
I also found a couple of posts on R-bloggers pointing me to the ‘propagate' package which has two different methods for constructing confidence intervals.
http://rmazing.wordpress.com/2013/08/14/predictnls-part-1-monte-carlo-simulation-confidence-intervals-for-nls-models/
http://rmazing.wordpress.com/2013/08/26/predictnls-part-2-taylor-approximation-confidence-intervals-for-nls-models/
—
David.
> Ty,
> Catarina Silva
>
> -----Original Message-----
> From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.CA.us]
> Sent: sábado, 7 de Novembro de 2015 01:09
> To: bolseiro.raiz.csilva at portucelsoporcel.com; R mailling list
> Subject: Re: [R] [GGplot] Geom_smooth with formula "power"?
>
> Does [1] help?
>
> [1] http://stackoverflow.com/questions/10528631/add-exp-power-trend-line-to-a-ggplot
> ---------------------------------------------------------------------------
> Jeff Newmiller The ..... ..... Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
> Live: OO#.. Dead: OO#.. Playing
> Research Engineer (Solar/Batteries O.O#. #.O#. with
> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> On November 6, 2015 2:41:18 AM PST, Catarina Silva <bolseiro.raiz.csilva at portucelsoporcel.com> wrote:
>> Hi,
>>
>> It's possible to use ggplot and geom_smooth to adjust a power curve to
>> the data?
>>
>> Initially i have done the adjustement with nls and the formula 'a*x^b',
>> but resulted the singular matrix error for start solution.
>> Alternatively I used the log transformation and i had correct results,
>> but I can't draw a power curve on the graphic.
>>
>> Someone know how to solve this problem?
>>
>>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list