2013-06-23 27 views
0

我有以下代碼可以使用外部空氣溫度和TOD(96分類變量,一天中的時間)在15分鐘的時間間隔內獲得負載消耗量的日提前預測。當我運行下面的代碼時,出現以下錯誤。在R中使用GLM模型的日期提前

i = 97:192 
    formula = as.formula(load[i] ~ load[i-96] + oat[i]) 
    model = glm(formula, data = train.set, family=Gamma(link=vlog())) 

我得到以下錯誤的最後一行後使用GLM(),

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
    contrasts can be applied only to factors with 2 or more levels 

而且使用預測()的最後一行後,下面的錯誤出現時,

Warning messages: 
1: In if (!se.fit) { : 
    the condition has length > 1 and only the first element will be used 
2: 'newdata' had 96 rows but variable(s) found have 1 rows 
3: In predict.lm(object, newdata, se.fit, scale = residual.scale, type = ifelse(type == : 
    prediction from a rank-deficient fit may be misleading 
4: In if (se.fit) list(fit = predictor, se.fit = se, df = df, residual.scale = sqrt(res.var)) else predictor : 
    the condition has length > 1 and only the first element will be used 
+0

'我'代表什麼?你是想要適合一個模型,還是(192-97 + 1)= 96模型? –

+0

我代表15分鐘數據的間隔數。 train.set = dt.new [1:192,] test.set = dt.new [193:288,] –

+0

因此,您想使用數據框中行的子集來擬合模型? 'link = vlog'從哪裏來? –

回答

1

你以相當迂迴的方式做事,而且不能很好地做出樣本預測。如果要對行的子集建模,則可以直接使用data參數或使用subset參數。

train.set$load_lag <- c(rep(NA, 96), train.set$load[1:96]) 
mod <- glm(load ~ load_lag*TOD, data=train.set[97:192, ], ...) 

您還需要重新考慮您正在做什麼與TOD。如果它有96個等級,那麼你對96個觀測值的擬合(至少)96個自由度不會給你一個明智的結果。

+0

我正在想方法來精確地解釋'formula = as.formula(load [i]〜load [i-96] * TOD [i] + oat [i]')是如何嚴重誤解公式的工作方式,但我能想到的最好的結果就是指出,這導致將模型擬合到單個數據點。 – joran

+0

@紅Ooi謝謝你的幫助。我會稍微嘗試一下,如果它適用於我,我會讓你知道。 –

+0

@joran你有什麼建議,你必須正確地適應模型? –