Reputation: 1797
I have a database (simplied format per below) with cities, different dates, and the temperatures on these dates. I want to calculate for each city the trend over time, and whether this trend is significant.
I think i have to somehow combine ddply together with the lm function (e.g. lm(date ~ temp)) and a call for the coefficients of the fit, but don't know how to do this....
There might be a much simpler solution - many thanks for helping me out;
W
City Date Temp (Celcius)
Amsterdam Jan-01 21
Amsterdam Mar-01 23
Amsterdam May-01 25
Barcelona Feb-01 20
Barcelona Mar-01 19
Barcelona May-01 25
Copenhagen Jan-01 19
Copenhagen Feb-01 23
Copenhagen May-01 22
I tried:
This is what I tried:
tempdata=read.csv("tempfile.csv", header=TRUE, sep=",", as.is=TRUE)
tempdata$Date <- as.Date(tempdata$Date, "%d/%m/%Y")
funcreg = function(x) {regmodel=lm(tempdata$Date ~ tempdata$Temperature)
return(data.frame(regmodel$coefficients[2]))
}
ddply(tempdata, .(City), funcreg)
Gives output of:
City regmodel.coefficients.2.
1 Amsterdam 14.71244
2 Barcelona 14.71244
3 Copenhagen 14.71244
Dput:
structure(list(City = c("Amsterdam", "Amsterdam", "Amsterdam",
"Barcelona", "Barcelona", "Barcelona", "Copenhagen", "Copenhagen",
"Copenhagen"), Date = c("01/01/2001", "01/03/2001", "01/05/2001",
"01/02/2001", "01/03/2001", "01/05/2001", "01/01/2001", "01/02/2001",
"01/05/2001"), Temperature = c(21L, 23L, 25L, 20L, 19L, 25L,
19L, 23L, 22L), X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("City",
"Date", "Temperature", "X"), class = "data.frame", row.names = c(NA,
-9L))
Upvotes: 2
Views: 819
Reputation: 132999
Use x
instead of tempdata
inside funcreg
. You should also switch your variables in the regression. Temperature is clearly the dependent here.
tempdata$Date <- as.Date(tempdata$Date,'%d/%m/%Y')
funcreg = function(x) {
regmodel <- lm(Temperature ~ Date, data=x)
data.frame(trend = regmodel$coefficients[2],
p = summary(regmodel)$coef["Date","Pr(>|t|)"])
}
library(plyr)
ddply(tempdata, .(City), funcreg)
City trend p
1 Amsterdam 0.03333025 0.006125688
2 Barcelona 0.06301304 0.298501483
3 Copenhagen 0.01696590 0.660997625
Upvotes: 1