我有一个数据库(以下简称格式),包含城市,不同日期和这些日期的温度。我想计算每个城市随时间变化的趋势,以及这种趋势是否显着。
我认为我必须以某种方式将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
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))
答案 0 :(得分: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