0
$\begingroup$

I'm trying to create a regression model for a set of data that includes time and temperature, among others, for 30 minutes intervals throughout the course of a month. I want to build a model that shows how these factors affect air conditioning energy usage. Basically, given time and temperature, I want to predict AC energy usage. This sounds easy enough, and the model for one household would be easily found with:

model = lm(ACEnergy ~ Time + Temperature, data)

One wrinkle that I wanted to add in is a conditional coefficient on the time and temperature data to differentiate between weekends and weekdays (because household behavior is very different based on whether it is a weekend/weekday) and another conditional coefficient for temperature above and below 70 degrees (because people may use energy very differently at this cutoff). If this isn't clear, here is the basis for the model I want to create:

$$Y = \beta_0 + \beta_1 \times \rm{WeekendTime} + \beta_2 \times \rm{WeekdayTime} + \beta_3 \times \rm{LowTemp} + \beta_4 \times \rm{HighTemp} + \beta_5 \times \rm{LowTemp} + \epsilon$$

Where Y is AC usage for a given household and time. If the given time stamp is during a weekday, $\beta_1$ is equal to 0 and vice versa. If the temperature is below 70 degrees, $\beta_4$ is equal to 0 and vice versa.

I'm having trouble figuring out a way to do this in R. Given a chunk of data for a particular household, I can easily separate the chunk into separate lists where one list has all the records with temperature above 70 degrees, another with all the records with timestamps indicating weekends, etc. I can then find the regression models for each of these lists. The problem is that each sub-list will not just have a different coefficient for the differing time or temperature category, but for all variables and intercepts. This makes it impossible to build the model in the way that I want. Or is there a better way to do this altogether?

I would appreciate any help you can offer. I am new to both R and the finer points of regression.

For clarity, here is an example of some of my data:

Record    TimeStamp            Energy  Temp

1         2009-08-17 16:45:00   0.19    75    
2         2009-08-17 17:15:00   0.28    76   
3         2009-08-17 17:45:00   0.20    76   
4         2009-08-17 18:15:00   0.32    76   
5         2009-08-17 18:45:00   0.27    66   
$\endgroup$

1 Answer 1

2
$\begingroup$

If your data.frame (df) has usage data (useage), time (a), temp (b) and indicator variable for weekend (c), then use is model:

myModel <- lm(useage ~ a*c + b*c,data = df)
summary(myModel)
#> [some text omitted]
#> Coefficients:
#>          Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  24.7409     2.5085   9.863 1.02e-12 ***
#> a            -0.5857     2.0452  -0.286    0.776    
#> c            -0.5604     2.4569  -0.228    0.821    
#> b             0.6263     2.0539   0.305    0.762    
#> a:c          -2.1584     1.8035  -1.197    0.238    
#> c:b          -0.8074     1.8861  -0.428    0.671    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The estimate for the parameter c is the expected change in usage betweeen weekends and weekdays, a:c is the difference between the coefficient for time between weekdays and weekends, and b:c is the same for temperature. (yes, the summary says c:b and I said b:c; they're symetric, just like multiplication...)

$\endgroup$
9
  • 1
    $\begingroup$ Thanks a lot for your answer. It looks pretty straightforward. I tried to implement this method. I manipulated my data so I know have a column for the hour of the day, a binary variable for whether or not is is a weekend entry, and two columns showing either the temp or 0 depending on whether it is more or less than 70. My only question is why the $\endgroup$
    – Ore M
    Jan 30, 2015 at 19:12
  • $\begingroup$ I think you ran out of characters in your comment. Can you finish the thought? $\endgroup$
    – Jthorpe
    Jan 30, 2015 at 19:20
  • $\begingroup$ whoops, sorry. Continued ... the weekend indicator is combined with the temperature data? Shouldn't the temperature data be combined with a binary indicator for above and below 70 degrees? Thanks $\endgroup$
    – Ore M
    Jan 30, 2015 at 19:26
  • $\begingroup$ Probably not a good idea to use zeros to indicate records that you think are not relevant, because zero is a valid numeric value an will affect your regression (notice that if you choose a different number to indicate irrelevant records, like -1, you get different coeffecients and p-values.). Better to subset your data (i.e. lm(useage ~ a*c + b*c,data=df[df$temp > 70,])). Replacing temperatures with 'NA' has the same effect b/c lm() will ignore those rows. $\endgroup$
    – Jthorpe
    Jan 30, 2015 at 19:26
  • $\begingroup$ Yes, I see. so then your model would be useage ~ time*weekend + temp * I(temp>70). BTW. the function I() just lets you calculate a value within the formula without having to create a new variable in your dataset. $\endgroup$
    – Jthorpe
    Jan 30, 2015 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.