-1

I have a data sets that has potential of being enormous with each Meter data i add. For now, i am only using 5 meters for example purpose. Here is what my data structure looks like:

str(Data1)
'data.frame':   43800 obs. of  7 variables:
 $ METER: Factor w/ 5 levels "10443720001539305",..: 1 1 1 1 1 1 1 1 1 1 
 $ LOAD : num  87.7 101.5 96.5 92 185.6 ...
 $ TEMP : num  30.5 34 39 36.5 24.5 31.5 32.5 18.5 26.5 25.5 ...
 $ DAY  : chr  "WD" "WD" "WE" "WE" ...
 $ HDD  : num  34.5 31 26 28.5 40.5 33.5 32.5 46.5 38.5 39.5 ...
 $ CDD  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ HOUR : Factor w/ 24 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...

For each Meter, i am trying to get the 24 different hourly regression models total of 120 models.

Creating Model List & storing Coefficients.

cat1<-dlply(Data1, c("METER", "HOUR"), function(z) lm(LOAD ~ HDD + CDD +  DAY))
cat_COF<-ldply(cat1, coef)

PREDICTING FOR SAME DATA

Predicting for the same data was fairly straight forward.

cat_Pred<-ldply(cat1, predict, type = "response")   

PROBLEM WHILE PREDICTING WITH NEW DATA

The real issue is while i try to predict with the new data set that has all the necessary information.

doesn't work

cat_Pred1<-ldply(cat1, predict, type = "response", newdata)   

Is there a quick way for me to do the predictions from these list of models?

This is the closest example i came across but this only has one "state" level. It is little confusing to me & I have tried adding c("METER", "HOUR") but never seems to work for me.

Any help will be highly appreciated!!

using predict with a list of lm() objects

Community
  • 1
  • 1
Gyve
  • 57
  • 1
  • 7

1 Answers1

0

I am not sure if this is a good answer I have a solution which seems like some manual work.

I noticed that you have an object called cat_COF I assume this is a 4 X 120 matrix. (4 because of the three regression coefficients + intercept) Assuming your newx object has an intercept column,

Could you try to multiply newx with cat_coef If your newx has dimensions n X 4 matrix multiplication will be possible,

Basically the output will be a n X 120 matrix of predicted values. I just tried thinking about it as Yhat = X beta-hat.

This might give you your predicted values.

Let me know if this helps.

Ajay Kumar
  • 71
  • 1
  • 3
  • 11