I am using R to evaluate climate data and I have a data set that looks like the following miniaturized version... please forgive my crude posting etiquette, I hope this post is understandable.
[0][STA.NAME] [YEAR] [SUM.CDD]
1 NAME1 1967 760
2 NAME1 1985 800
3 NAME1 1996 740
4 NAME1 2003 810
5 NAME1 2011 790
6 NAME2 1967 700
7 NAME2 1985 690
8 NAME2 1996 850
9 NAME2 2003 790
10 NAME3 1967 760
11 NAME3 1985 800
12 NAME3 1990 740
13 NAME3 1996 810
14 NAME3 2003 790
15 NAME3 2011 800
I am trying to return a new DF with this
[STA.NAME] [Eq'n of trend]
NAME1 (y = mx + b)
NAME2 (y = mx + b)
etc...
Eventually I will need to calculate variance of the trends, as well as total variance of data and would like to eventually append those to this resulting data set for something like...
[STA.NAME] [TREND] [VAR.TREND] [VAR.DATA]
with values in rows, 1 for each STA.NAME...
Any help is greatly appreciated, If there is a better way than lm(), with which I am currently stumped, I would be interested as well.
Thank you very much,
Jesse
Here is a simple solution using
ddply()
fromplyr
to return the coefficients for each group:First replicate the data:
Now do the modelling:
Now, depending on what you want to do, it may be simpler (and perhaps more meaningful) to create a single model of your data:
Get a summary:
Extract only the coefficients:
Finally, you perhaps wanted to fit a model with interaction terms. This model gives you effectively the same results as the original
plyr
solution. Depending on your data and your objectives, this might be the way to do it: