time series - How to make Dummies with R -
i'm trying forecast daily electricity market price, dataset of 3 years before of daily prices, , want correct seasonal effect because cannot predict prices, want dummy diferentiate everyday of week, everymonday, everytuesday , on.
can explain me how make dummies in r in order diferentiate days on week? dataset daily price, dates. thank much.
you don't have that, in r, can use as.factor()
treat variable categorical:
set.seed(784) week <- sample(1:7, 50, rep=t) y <- rnorm(50) # treat week factor: m1 <- lm(y ~ as.factor(week)) summary(m1)
results:
coefficients: estimate std. error t value pr(>|t|) (intercept) 0.3148 0.3417 0.921 0.362 as.factor(week)2 0.2059 0.5029 0.410 0.684 as.factor(week)3 0.3623 0.5293 0.684 0.497 as.factor(week)4 -0.1308 0.4678 -0.280 0.781 as.factor(week)5 0.6916 0.4678 1.478 0.147 as.factor(week)6 -0.3285 0.4832 -0.680 0.500 as.factor(week)7 -0.6165 0.4555 -1.353 0.183
however, if insist, can use command make dummies:
# manually make dummies: sun <- (week==1) mon <- (week==2) tue <- (week==3) wed <- (week==4) thu <- (week==5) fri <- (week==6) sat <- (week==7) m2 <- lm(y ~ mon + tue + wed + thu + fri + sat) summary(m2)
the results same:
coefficients: estimate std. error t value pr(>|t|) (intercept) 0.3148 0.3417 0.921 0.362 montrue 0.2059 0.5029 0.410 0.684 tuetrue 0.3623 0.5293 0.684 0.497 wedtrue -0.1308 0.4678 -0.280 0.781 thutrue 0.6916 0.4678 1.478 0.147 fritrue -0.3285 0.4832 -0.680 0.500 sattrue -0.6165 0.4555 -1.353 0.183
you may want check how treat dates in r, instance, can extract day of week using weekdays()
:
x <- as.date('3/20/2014',format='%m/%d/%y') weekdays(x)
results:
"thursday"
Comments
Post a Comment