2

我有一个包含城市、不同日期以及这些日期的温度的数据库(以下为简化格式)。我想为每个城市计算一段时间内的趋势,以及这种趋势是否显着。

我想我必须以某种方式将 ddply 与 lm 函数(例如 lm(date ~ temp))结合起来并调用拟合系数,但不知道该怎么做......

可能有一个更简单的解决方案 - 非常感谢您帮助我;

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

我试过:

这是我尝试过的:

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)

给出以下输出:

        City regmodel.coefficients.2.
1  Amsterdam                 14.71244
2  Barcelona                 14.71244
3 Copenhagen                 14.71244

输入:

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))
4

1 回答 1

1

使用x而不是tempdata里面funcreg。您还应该在回归中切换变量。温度显然是这里的依赖项。

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
于 2012-12-18T07:53:12.597 回答