Any R experts/geeks...
 

MegaSack DRAW - This year's winner is user - rgwb
We will be in touch

[Closed] Any R experts/geeks in da house?

9 Posts
5 Users
0 Reactions
148 Views
 Kit
Posts: 24
Free Member
Topic starter
 

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? 😀


 
Posted : 22/06/2012 3:02 pm
Posts: 17274
Full Member
 

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.


 
Posted : 22/06/2012 4:25 pm
 Kit
Posts: 24
Free Member
Topic starter
 

Thanks very much, will give those methods a shot!


 
Posted : 22/06/2012 4:40 pm
Posts: 17274
Full Member
 

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 🙂


 
Posted : 22/06/2012 4:53 pm
Posts: 2882
Full Member
 

Oh my god!

People actually do this?!?!

😉


 
Posted : 22/06/2012 5:22 pm
Posts: 9
Free Member
 

Wtf!!!

I mastered ABC easy enough, but on the basis of above, I think I'll leave 'R' for a bit....


 
Posted : 22/06/2012 10:42 pm
Posts: 0
Free Member
 


 
Posted : 23/06/2012 8:20 am
 Kit
Posts: 24
Free Member
Topic starter
 

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


 
Posted : 24/06/2012 10:47 am
Posts: 17274
Full Member
 

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!


 
Posted : 24/06/2012 5:36 pm
Posts: 2882
Full Member
 

You mis-spelt resistivity... 😉


 
Posted : 24/06/2012 5:37 pm