MegaSack DRAW - This year's winner is user - rgwb
We will be in touch
Welcome! I wonder if anyone can help me on this one...
I'm currently trying to learn R, and am stumped. I want to produce a scatter plot with a power regression line plotted through my data points. Easy-peasy in Excel, I was able to knock up the following in about 30 secs:
[url= http://farm8.staticflickr.com/7119/7420139690_9ba4464aa6.jp g" target="_blank">http://farm8.staticflickr.com/7119/7420139690_9ba4464aa6.jp g"/> [/img][/url]
[url= http://www.flickr.com/photos/gingerfox/7420139690/ ]Power regression plot[/url] by [url= http://www.flickr.com/people/gingerfox/ ]Ginger F0x[/url], on Flickr
I can plot the points fine in R, using both the inbuilt plot function and the ggplot2 package. But I can't find anything online that lets me plot the power regression through the points. I can do linear, loess, polynomial, but that's not what I'm after. A web search turned up nothing useful...
Any takers? 😀
Why don't you just plot the data as log(Rw) vs. log(Sal) with a linear regression line through the data? Non-linear fitting is fine, but log-log is easier.
y = ax^b so log(y) = log(a) + b log(x)
find b from your regression.
Alternatively, looking at your data, it's pretty obvious that the product of Sal x Rw is a constant (power is minus unity). So you could also plot 1/Rw vs. Sal. That would give a nice regression line but tell you about the value of a only (because b is fixed to -1).
EDIT: if you STILL want to model the data as is, then I suggest reading [url= http://www.apsnet.org/EDCENTER/ADVANCED/TOPICS/ECOLOGYANDEPIDEMIOLOGYINR/DISEASEPROGRESS/Pages/NonlinearRegression.aspx ]this[/url]. Personally, I'd log the data. A search on "Non-linear Regression R example" turned this up.
Thanks very much, will give those methods a shot!
If you really want to fit with the power law then you will require the nls function:
ModelFit <- nls(Sal ~ a*Rw^b),
data = YourDataSet,
start = list(a = 8000, b = -1)
)
summary(ModelFit)
The log-log fitting will be the best method for the simple case you have. I'm more of a SAS lover, so graphics will require overlaying as plot of the function with obtained parameters a,b. that is left to the interested reader 🙂
Oh my god!
People actually do this?!?!
😉
Wtf!!!
I mastered ABC easy enough, but on the basis of above, I think I'll leave 'R' for a bit....
Seeing as you're all so interested, this is the linear regression of the logs of my data, as djaustin suggested. Good, eh? 😉
[url= http://farm6.staticflickr.com/5332/7431239568_a1784fbd0d.jp g" target="_blank">http://farm6.staticflickr.com/5332/7431239568_a1784fbd0d.jp g"/> [/img][/url]
[url= http://www.flickr.com/photos/gingerfox/7431239568/ ]Salinity Rw plot[/url] by [url= http://www.flickr.com/people/gingerfox/ ]Ginger F0x[/url], on Flickr
Now try it with nls and not linearizing. It will be good for your R skills but won't look as nice as the above. And your graphics are miles better than mine!
You mis-spelt resistivity... 😉
