简体   繁体   English

R时间序列ddply

[英]R time-series ddply

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 (eg lm(date ~ temp)) and a call for the coefficients of the fit, but don't know how to do this.... 我认为我必须以某种方式将ddply与lm函数(例如lm(date〜temp))和对拟合系数的调用结合起来,但是不知道该怎么做。

There might be a much simpler solution - many thanks for helping me out; 可能有一个更简单的解决方案-非常感谢您的帮助;

W 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: 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))

Use x instead of tempdata inside funcreg . x代替tempdata内部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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM