2012-11-05 63 views
2

我正在處理天氣數據和水電費帳單,並試圖估計一個非線性迴歸模型。基於參數總結數據幀並應用於數據幀和nls的調用函數

我想出了一個問題。我調用的函數計算天氣統計數據,加熱和冷卻度天數(HDD和CDD)不能應用於數據框,並且nls不能使用它。顯然,我錯過了一些關於函數參數的非常明顯的東西。

有人能指出我下面的硬盤和CDD功能出錯嗎?

下面是一些生成假天氣和帳單數據的代碼問題的簡單示例。

# Generate Fake Weather Data 
CZ<-c(1,2) 
Date<-c('2001-01-01','2001-01-02','2001-01-03','2001-01-04') 
Weather<-expand.grid(CZ,Date) 
names(Weather)<-c("CZ","Date") 
Weather$AvgTemp<-rnorm(8,mean= 60,sd=20) 

#Generate Fake Billing Data 
ID<-as.numeric(1:10) 
CZ<-c(1,2) 
StartDate<-'2001-01-01' 
EndDate<-'2001-02-01' 
FakeBilling<-data.frame(cbind(ID,CZ,StartDate,EndDate)) 
FakeBilling$KWH<-rnorm(10,mean=1000, sd=200) 

#Heating and cooling degree functions 
HDD<- function(b,CZ,StartDate,EndDate) { 
    Temps<-Weather$AvgTemp[Weather$CZ==CZ&as.Date(Weather$Date) >=as.Date(StartDate) &  as.Date(Weather$Date) < as.Date(EndDate)]; 

    sum((b-Temps)/(1+exp(-5*(b-Temps)))) 
} 


CDD <- function(b,CZ,StartDate,EndDate) { 
Temps<- Weather$AvgTemp[as.character(Weather$CZ)==as.character(CZ) &  as.Date(Weather$Date) >=as.Date(StartDate)& as.Date(Weather$Date) < as.Date(EndDate)] 

    sum((Temps-b)/(1+exp(-5*(Temps-b)))) 
} 

#these work 
HDD(60,1,'2001-01-01','2001-02-01') 
# [1] 29.34333 
CDD(60,1,'2001-01-01','2001-02-01') 
# [1] 53.49393 

# This does not. Lots of warnings about length 
HDD(60,FakeBilling$CZ,FakeBilling$StartDate,FakeBilling$EndDate) 
# [1] NA 
# Warning messages: 
# 1: In is.na(e1) | is.na(e2) : 
# longer object length is not a multiple of shorter object length 
# 2: In `==.default`(Weather$CZ, CZ) : 
# longer object length is not a multiple of shorter object length 
# 3: In `>=.default`(as.Date(Weather$Date), as.Date(StartDate)) : 
# longer object length is not a multiple of shorter object length 
# 4: In `<.default`(as.Date(Weather$Date), as.Date(EndDate)) : 
# longer object length is not a multiple of shorter object length 

# Would like to run this but get similar error. 
nls(KWH~load + heatload*(HDD(base,CZ,StartDate,EndDate)) ,start=c(load=200,  heatload=.1,base=65), data=FakeBilling, na.action=na.omit) 
# Error in numericDeriv(form[[3L]], names(ind), env) : 
# Missing value or an infinity produced when evaluating the model 
# In addition: Warning messages: 
# 1: In is.na(e1) | is.na(e2) : 
# longer object length is not a multiple of shorter object length 
# 2: In `==.default`(Weather$CZ, CZ) : 
# longer object length is not a multiple of shorter object length 
# 3: In `>=.default`(as.Date(Weather$Date), as.Date(StartDate)) : 
# longer object length is not a multiple of shorter object length 
# 4: In `<.default`(as.Date(Weather$Date), as.Date(EndDate)) : 
# longer object length is not a multiple of shorter object length 
# 5: In is.na(e1) | is.na(e2) : 
# longer object length is not a multiple of shorter object length 
# 6: In `==.default`(Weather$CZ, CZ) : 
# longer object length is not a multiple of shorter object length 
# 7: In `>=.default`(as.Date(Weather$Date), as.Date(StartDate)) : 
# longer object length is not a multiple of shorter object length 
# 8: In `<.default`(as.Date(Weather$Date), as.Date(EndDate)) : 
# longer object length is not a multiple of shorter object length 

回答

1

您的函數未設置爲矢量化函數。您可以使用mapply()

with(FakeBilling, mapply(HDD, b = 60, CZ = CZ, StartDate = StartDate, EndDate = EndDate)) 
#---- 
[1] 29.33481 13.39434 29.33481 13.39434 29.33481 13.39434 29.33481 13.39434 29.33481 13.39434 

也有Vectorize()功能賦予相同的結果:

HDDvec <- Vectorize(HDD) 
HDDvec(60,FakeBilling$CZ,FakeBilling$StartDate,FakeBilling$EndDate) 
#---- 
[1] 29.33481 13.39434 29.33481 13.39434 29.33481 13.39434 29.33481 13.39434 29.33481 13.39434 
+0

謝謝。我知道我在某處失去了我的鼻子。 – user1798507