R Markdown

This is an R Markdown document. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

The syntax on line 19 then the subsequent on line 56 tells R Markdown to run R code without the asterisks embeds the subsequent code as embedded inside the R Markdown file

library(ggplot2)
library(plyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(reshape2)
setwd("C:/Users/jlariv/OneDrive/Econ 404/")
oj <- read.csv("oj.csv") 


ggplot(oj, aes(logmove, price)) + geom_point(aes(color = factor(brand)))

#First why to add in lagged weeks
df1 <-oj
df1$week<-df1$week+1  # df1 now has NEXT week and not the current one.  If we merge this by weeks now, this is last week's price (e.g., "lagged price").
myvars <- c("price", "week", "brand","store")
df1 <- df1[myvars]
lagged <- merge(oj, df1, by=c("brand","store","week"))
#NOTE: The number of observations decreased.  Why?  You've just lost (at least) one week's worth of data at each store.
lagged=lagged[order(lagged$week,lagged$store),]
lagged=lagged[order(lagged$store,lagged$week),]
#Comparing this to above, Store two is nowhere to be found.  There was missing data for consecutive weeks.  As a result, it gets dropped.
colnames(lagged)[18] <- "lagged_price"
colnames(lagged)[6] <- "price"


#Explicit code to be super celar about what cross validation is doing.
set.seed(9)
folds<-5
random_lagged <- lagged[sample(nrow(lagged)),]
random_lagged$rand_obs<-seq(1,nrow(random_lagged))
# %% is the modulus operator in R
random_lagged$partition <- random_lagged$rand_obs %% folds +1
MSEs <- c(1:folds)

#For people who've never seen a for loop, in this case its some that helps you iterate through code.  "i" is a count variable that runs from 1 to 5 (e.g., folds <- 5 in like 47) and takes the value of the iteration round of the loop within the loop (e.g., within the brackets)
for (i in 1:folds) {
oj_test1 <- random_lagged[which(random_lagged$partition==i),]
oj_train1 <- anti_join(random_lagged,oj_test1)
reg1 <- lm(logmove ~ log(price) + feat + brand + brand*log(price) + AGE60 + EDUC + ETHNIC + INCOME + HHLARGE + WORKWOM + HVAL150 + SSTRDIST + SSTRVOL + CPDIST5 + CPWVOL5 + EDUC*log(price) + HHLARGE*log(price) + log(lagged_price) , data=oj_train1)
# Predict y
oj_test1$logmove_hat <- predict(reg1, newdata=oj_test1)
MSE <- mean((oj_test1$logmove_hat - oj_test1$logmove)^2)
MSEs[i] <- MSE
}
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
# Printout of vector of MSEs and average of MSEs
MSEs
## [1] 0.4177313 0.4113887 0.4107742 0.4152023 0.4191698
mean(MSEs)
## [1] 0.4148533
for (i in 1:folds) {
oj_test1 <- random_lagged[which(random_lagged$partition==i),]
oj_train1 <- anti_join(random_lagged,oj_test1)
reg1 <- lm(logmove ~ log(price) + feat+brand + AGE60 + EDUC + ETHNIC + INCOME + HHLARGE + WORKWOM + HVAL150 + SSTRDIST + SSTRVOL + CPDIST5 + CPWVOL5, data=oj_train1)
# Predict y
oj_test1$logmove_hat <- predict(reg1, newdata=oj_test1)
MSE <- mean((oj_test1$logmove_hat - oj_test1$logmove)^2)
MSEs[i] <- MSE
}
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
## Joining, by = c("brand", "store", "week", "logmove", "feat", "price", "AGE60", "EDUC", "ETHNIC", "INCOME", "HHLARGE", "WORKWOM", "HVAL150", "SSTRDIST", "SSTRVOL", "CPDIST5", "CPWVOL5", "lagged_price", "rand_obs", "partition")
MSEs
## [1] 0.4537844 0.4457258 0.4452941 0.4500627 0.4509782
mean(MSEs)
## [1] 0.449169
#We see that this simpler model has a HIGHER out of sample MSE.  Not as good at prediction. 

library(reshape2)
library(glmnet)
## Loading required package: Matrix
## Loaded glmnet 3.0-2
###################
# In Class
###################
x <- model.matrix(~ log(price) + feat + brand + brand*log(price) + AGE60 + EDUC + ETHNIC + INCOME + HHLARGE + WORKWOM + HVAL150 + SSTRDIST + SSTRVOL + CPDIST5 + CPWVOL5 + EDUC*log(price) + HHLARGE*log(price) + log(lagged_price) , data=lagged)
y <- as.numeric(as.matrix(lagged$logmove))
set.seed(720)
#lasso_v1 <- cv.glmnet(x, y, alpha=1)
lasso_v1 <- glmnet(x, y, alpha=1)
plot(lasso_v1)

coef(lasso_v1, s=lasso_v1$lambda.min)
## 22 x 73 sparse Matrix of class "dgCMatrix"
##    [[ suppressing 73 column names 's0', 's1', 's2' ... ]]
##                                                                                
## (Intercept)                 9.174759 9.1475029 9.1226678  9.13710438  9.1989982
## (Intercept)                 .        .         .          .           .        
## log(price)                  .        .         .         -0.04473513 -0.1443215
## feat                        .        0.1151777 0.2201233  0.30708480  0.3749318
## brandminute.maid            .        .         .          .           .        
## brandtropicana              .        .         .          .           .        
## AGE60                       .        .         .          .           .        
## EDUC                        .        .         .          .           .        
## ETHNIC                      .        .         .          .           .        
## INCOME                      .        .         .          .           .        
## HHLARGE                     .        .         .          .           .        
## WORKWOM                     .        .         .          .           .        
## HVAL150                     .        .         .          .           .        
## SSTRDIST                    .        .         .          .           .        
## SSTRVOL                     .        .         .          .           .        
## CPDIST5                     .        .         .          .           .        
## CPWVOL5                     .        .         .          .           .        
## log(lagged_price)           .        .         .          .           .        
## log(price):brandminute.maid .        .         .          .           .        
## log(price):brandtropicana   .        .         .          .           .        
## log(price):EDUC             .        .         .          .           .        
## log(price):HHLARGE          .        .         .          .           .        
##                                                                        
## (Intercept)                  9.2553793  9.3067516  9.3535602  9.3968030
## (Intercept)                  .          .          .          .        
## log(price)                  -0.2350437 -0.3177064 -0.3930256 -0.4487721
## feat                         0.4367547  0.4930855  0.5444121  0.5911069
## brandminute.maid             .          .          .          .        
## brandtropicana               .          .          .          .        
## AGE60                        .          .          .          .        
## EDUC                         .          .          .          .        
## ETHNIC                       .          .          .          .        
## INCOME                       .          .          .          .        
## HHLARGE                      .          .          .          .        
## WORKWOM                      .          .          .          .        
## HVAL150                      .          .          .          .        
## SSTRDIST                     .          .          .          .        
## SSTRVOL                      .          .          .          .        
## CPDIST5                      .          .          .          .        
## CPWVOL5                      .          .          .          .        
## log(lagged_price)            .          .          .          .        
## log(price):brandminute.maid  .          .          .          .        
## log(price):brandtropicana    .          .          .          .        
## log(price):EDUC              .          .          .          .        
## log(price):HHLARGE           .          .          .         -0.1181259
##                                                                        
## (Intercept)                  9.4365444  9.4727392  9.5058397  9.5358810
## (Intercept)                  .          .          .          .        
## log(price)                  -0.4766223 -0.5019428 -0.5254306 -0.5464238
## feat                         0.6335646  0.6722511  0.7074957  0.7396143
## brandminute.maid             .          .          .          .        
## brandtropicana               .          .          .          .        
## AGE60                        .          .          .          .        
## EDUC                         .          .          .          .        
## ETHNIC                       .          .          .          .        
## INCOME                       .          .          .          .        
## HHLARGE                      .          .          .          .        
## WORKWOM                      .          .          .          .        
## HVAL150                      .          .          .          .        
## SSTRDIST                     .          .          .          .        
## SSTRVOL                      .          .          .          .        
## CPDIST5                      .          .          .          .        
## CPWVOL5                      .          .          .          .        
## log(lagged_price)            .          .          .          .        
## log(price):brandminute.maid  .          .          .          .        
## log(price):brandtropicana    .          .          .          .        
## log(price):EDUC              .          .          .          .        
## log(price):HHLARGE          -0.4283349 -0.7112909 -0.9668237 -1.2018941
##                                                                          
## (Intercept)                  9.5632509  9.5881892  9.61623575  9.68533198
## (Intercept)                  .          .          .           .         
## log(price)                  -0.5655427 -0.5829629 -0.60899464 -0.68314422
## feat                         0.7688796  0.7955451  0.81906943  0.83659189
## brandminute.maid             .          .          .           .         
## brandtropicana               .          .          0.01169695  0.07750111
## AGE60                        .          .          .           .         
## EDUC                         .          .          .           .         
## ETHNIC                       .          .          .           .         
## INCOME                       .          .          .           .         
## HHLARGE                      .          .          .           .         
## WORKWOM                      .          .          .           .         
## HVAL150                      .          .          .           .         
## SSTRDIST                     .          .          .           .         
## SSTRVOL                      .          .          .           .         
## CPDIST5                      .          .          .           .         
## CPWVOL5                      .          .          .          -0.03522429
## log(lagged_price)            .          .          .           .         
## log(price):brandminute.maid  .          .          .           .         
## log(price):brandtropicana    .          .          .           .         
## log(price):EDUC              .          .          .           .         
## log(price):HHLARGE          -1.4161324 -1.6113395 -1.80126485 -2.04167300
##                                                                           
## (Intercept)                  9.75904921  9.81279679  9.8557881  9.92731970
## (Intercept)                  .           .           .          .         
## log(price)                  -0.74335995 -0.82636180 -0.9214419 -1.00126390
## feat                         0.85257691  0.85936887  0.8610885  0.86295455
## brandminute.maid             .           .           .          .         
## brandtropicana               0.13730999  0.18230482  0.2185578  0.25178928
## AGE60                        .           .           .          .         
## EDUC                         .           .           .          .         
## ETHNIC                       .           .           .          .         
## INCOME                       .           .           .          .         
## HHLARGE                      .           .           .          .         
## WORKWOM                      .           .           .         -0.09882442
## HVAL150                      .           .           .          .         
## SSTRDIST                     .           .           .          .         
## SSTRVOL                      .           .           .          .         
## CPDIST5                      .           .           .          .         
## CPWVOL5                     -0.09312002 -0.14554430 -0.1926849 -0.22795106
## log(lagged_price)            .           0.05131067  0.1255812  0.19172946
## log(price):brandminute.maid  .           .           .          .         
## log(price):brandtropicana    .           .           .          .         
## log(price):EDUC              .           .           .          .         
## log(price):HHLARGE          -2.31780515 -2.56819605 -2.7740235 -3.01158069
##                                                                             
## (Intercept)                 10.0073870 10.09351142 10.17710684 10.2525629092
## (Intercept)                  .          .           .           .           
## log(price)                  -1.0704042 -1.15500118 -1.24375189 -1.3228261663
## feat                         0.8646842  0.86291622  0.85950836  0.8566470896
## brandminute.maid             .          0.03948402  0.09006367  0.1355803956
## brandtropicana               0.2822427  0.34635557  0.41842376  0.4834046537
## AGE60                        .          .           .           .           
## EDUC                         .          .           .           .           
## ETHNIC                       .          .           .           0.0006533839
## INCOME                       .          .           .           .           
## HHLARGE                      .          .           .           .           
## WORKWOM                     -0.2346713 -0.36370906 -0.48279376 -0.5907329848
## HVAL150                      .          .           .           .           
## SSTRDIST                     .          .           .           .           
## SSTRVOL                      .          .           .           .           
## CPDIST5                      .          .           .           .           
## CPWVOL5                     -0.2567441 -0.28340876 -0.30782016 -0.3299203401
## log(lagged_price)            0.2516464  0.28501888  0.30957612  0.3315362221
## log(price):brandminute.maid  .          .           .           .           
## log(price):brandtropicana    .          .           .           .           
## log(price):EDUC              .          .           .           .           
## log(price):HHLARGE          -3.2557762 -3.50103604 -3.72234343 -3.9281089640
##                                                                             
## (Intercept)                 10.31138429 10.374272499 10.43294319 10.48650685
## (Intercept)                  .           .            .           .         
## log(price)                  -1.39301890 -1.455968230 -1.51347365 -1.56582170
## feat                         0.85379926  0.851157168  0.84903703  0.84696981
## brandminute.maid             0.17768538  0.216047975  0.25043141  0.28201053
## brandtropicana               0.54367201  0.598460313  0.64760132  0.69270490
## AGE60                        .           .            .           .         
## EDUC                         .           .            .           .         
## ETHNIC                       0.02632482  0.060759567  0.09407708  0.12442291
## INCOME                       .           .            .           .         
## HHLARGE                      .           .            .           .         
## WORKWOM                     -0.67766804 -0.772134649 -0.85982988 -0.94015413
## HVAL150                      .           .            .           .         
## SSTRDIST                     .           .            .           .         
## SSTRVOL                      .          -0.009913619 -0.02083632 -0.03071054
## CPDIST5                      .           .            .           .         
## CPWVOL5                     -0.34359038 -0.342890420 -0.33945802 -0.33654758
## log(lagged_price)            0.35117072  0.369527661  0.38589101  0.40101820
## log(price):brandminute.maid  .           .            .           .         
## log(price):brandtropicana    .           .            .           .         
## log(price):EDUC              .           .            .           .         
## log(price):HHLARGE          -4.14422886 -4.346203432 -4.52197633 -4.68569029
##                                                                             
## (Intercept)                 10.5353123 10.5842085195 10.61791750 10.62777250
## (Intercept)                  .          .             .           .         
## log(price)                  -1.6134864 -1.6621345444 -1.75572571 -1.85180749
## feat                         0.8450895  0.8434152080  0.84166411  0.83986805
## brandminute.maid             0.3107810  0.3370975670  0.36188699  0.38490888
## brandtropicana               0.7337989  0.7715316721  0.80754657  0.84093275
## AGE60                        .          .             0.07554039  0.18546955
## EDUC                         .          .             .           .         
## ETHNIC                       0.1520772  0.1787484557  0.21345749  0.24731861
## INCOME                       .          .             .           .         
## HHLARGE                      .          .             .           .         
## WORKWOM                     -1.0133586 -1.0948078169 -1.17482658 -1.20942250
## HVAL150                      .          0.0009283278  .           .         
## SSTRDIST                     .          .             .           .         
## SSTRVOL                     -0.0397081 -0.0475039933 -0.05230015 -0.05586669
## CPDIST5                      .          .             .           .         
## CPWVOL5                     -0.3338955 -0.3319953659 -0.33495796 -0.33921115
## log(lagged_price)            0.4147921  0.4268177796  0.43617898  0.44479752
## log(price):brandminute.maid  .          .             .           .         
## log(price):brandtropicana    .          .             .           .         
## log(price):EDUC              .          0.0193012553  0.15409097  0.28308794
## log(price):HHLARGE          -4.8349904 -4.9598911573 -4.89151018 -4.76198773
##                                                                               
## (Intercept)                 10.6392020781 10.65486840 10.67123908 10.668200679
## (Intercept)                  .             .           .           .          
## log(price)                  -1.9373519842 -2.04227916 -2.13062179 -2.216868439
## feat                         0.8382831118  0.83670585  0.83586452  0.835042635
## brandminute.maid             0.4057702199  0.43050895  0.45179589  0.470969985
## brandtropicana               0.8712155426  0.85160644  0.82938439  0.810297848
## AGE60                        0.2807911087  0.38024787  0.47616538  0.588598889
## EDUC                         .             .           .           .          
## ETHNIC                       0.2779715798  0.30670592  0.34493307  0.385174034
## INCOME                       .             .           .           .          
## HHLARGE                      .             .           .           .          
## WORKWOM                     -1.2453908398 -1.26950393 -1.28390568 -1.270124189
## HVAL150                      .             .           .           .          
## SSTRDIST                     .             .          -0.00163307 -0.003361582
## SSTRVOL                     -0.0592425556 -0.06205185 -0.06247905 -0.061891569
## CPDIST5                      .             .           .           0.001980196
## CPWVOL5                     -0.3428422754 -0.34674225 -0.35957158 -0.373960160
## log(lagged_price)            0.4525850506  0.46013027  0.46727941  0.474366146
## log(price):brandminute.maid  .             .           .           .          
## log(price):brandtropicana    0.0003583555  0.05645411  0.10974044  0.156768399
## log(price):EDUC              0.3984581049  0.51088984  0.61375951  0.716297012
## log(price):HHLARGE          -4.6564147670 -4.52265020 -4.43169854 -4.317060818
##                                                                                
## (Intercept)                 10.662373400 10.657418048 10.652890227 10.648756787
## (Intercept)                  .            .            .            .          
## log(price)                  -2.291144573 -2.359313379 -2.421323717 -2.477872562
## feat                         0.834368599  0.833764902  0.833211406  0.832714224
## brandminute.maid             0.487978801  0.503657568  0.517923552  0.531066245
## brandtropicana               0.793630286  0.777151182  0.762411921  0.748198734
## AGE60                        0.681484496  0.765743887  0.842458010  0.912376440
## EDUC                         .            .            .            .          
## ETHNIC                       0.425124236  0.461218165  0.494105534  0.524042138
## INCOME                       .            .            .            .          
## HHLARGE                      .            .            .            .          
## WORKWOM                     -1.264350852 -1.258868638 -1.253950471 -1.248978508
## HVAL150                      .            .            .            .          
## SSTRDIST                    -0.004930196 -0.006352005 -0.007647303 -0.008823047
## SSTRVOL                     -0.061590974 -0.061322826 -0.061079301 -0.060890733
## CPDIST5                      0.006473598  0.010533609  0.014233497  0.017589512
## CPWVOL5                     -0.386566213 -0.398026240 -0.408465202 -0.417901992
## log(lagged_price)            0.481208646  0.487358980  0.492968919  0.497980982
## log(price):brandminute.maid  .            .            .            .          
## log(price):brandtropicana    0.198194226  0.237439631  0.272900983  0.306168963
## log(price):EDUC              0.811820415  0.898556878  0.977573205  1.049132025
## log(price):HHLARGE          -4.242496218 -4.173846074 -4.111473932 -4.055487129
##                                                                             
## (Intercept)                 10.698134901 11.01720329 11.26205411 11.48504886
## (Intercept)                  .            .           .           .         
## log(price)                  -2.529771237 -2.56161332 -2.58540450 -2.60659809
## feat                         0.832245117  0.83166938  0.83125168  0.83084903
## brandminute.maid             0.542923254  0.55396118  0.56398792  0.57301129
## brandtropicana               0.735161036  0.72540483  0.71566423  0.70747096
## AGE60                        0.980601347  1.10814090  1.21580885  1.31362139
## EDUC                         .            .           .           .         
## ETHNIC                       0.546933324  0.54534973  0.54659018  0.54776949
## INCOME                      -0.005680632 -0.04424726 -0.07463384 -0.10233000
## HHLARGE                      0.032096628  0.48015163  0.87224710  1.23212310
## WORKWOM                     -1.236265691 -1.16270243 -1.09947853 -1.04250735
## HVAL150                      .            .           .           .         
## SSTRDIST                    -0.009891778 -0.01098034 -0.01192976 -0.01279971
## SSTRVOL                     -0.061149328 -0.06239502 -0.06359079 -0.06466157
## CPDIST5                      0.020842959  0.02532395  0.02903610  0.03243559
## CPWVOL5                     -0.425494405 -0.43016760 -0.43424444 -0.43800789
## log(lagged_price)            0.502759696  0.50738988  0.51131651  0.51503470
## log(price):brandminute.maid  .            .           .           .         
## log(price):brandtropicana    0.336411621  0.36229022  0.38662196  0.40797923
## log(price):EDUC              1.122132692  1.25396997  1.36093477  1.45867543
## log(price):HHLARGE          -4.016568095 -4.25295220 -4.48230648 -4.69445926
##                                                                            
## (Intercept)                 11.68728928 11.87174695 12.02721516 12.06205567
## (Intercept)                  .           .           .           .         
## log(price)                  -2.62554142 -2.64288623 -2.65904705 -2.69952177
## feat                         0.83049200  0.83015727  0.82988132  0.82985528
## brandminute.maid             0.58124157  0.58871758  0.59541746  0.59317741
## brandtropicana               0.69970273  0.69306830  0.68680579  0.68040542
## AGE60                        1.40274487  1.48384571  1.55555662  1.59697970
## EDUC                         .           .          -0.01071934 -0.12677751
## ETHNIC                       0.54888183  0.54988869  0.55034578  0.55885688
## INCOME                      -0.12750157 -0.15043908 -0.16989712 -0.17358530
## HHLARGE                      1.56237184  1.86185436  2.12951617  2.28855135
## WORKWOM                     -0.99052252 -0.94328160 -0.89817526 -0.86347351
## HVAL150                      .           .           .           .         
## SSTRDIST                    -0.01359227 -0.01431390 -0.01492263 -0.01538417
## SSTRVOL                     -0.06563698 -0.06652630 -0.06737881 -0.06796463
## CPDIST5                      0.03552741  0.03834498  0.04066573  0.04166356
## CPWVOL5                     -0.44144035 -0.44456296 -0.44725916 -0.44970153
## log(lagged_price)            0.51840757  0.52149557  0.52434456  0.52678222
## log(price):brandminute.maid  .           .           .           0.01202270
## log(price):brandtropicana    0.42774723  0.44529272  0.46132053  0.47851461
## log(price):EDUC              1.54752357  1.62849531  1.70794417  1.86602744
## log(price):HHLARGE          -4.89083606 -5.06824569 -5.23411063 -5.35915989
##                                                                            
## (Intercept)                 12.14685868 12.22908341 12.30551063 12.37330408
## (Intercept)                  .           .           .           .         
## log(price)                  -2.75012303 -2.79773039 -2.84122104 -2.88268536
## feat                         0.82996560  0.83001748  0.83003469  0.83006447
## brandminute.maid             0.57941617  0.56958385  0.56127094  0.55337288
## brandtropicana               0.67185388  0.66295560  0.65562883  0.64815059
## AGE60                        1.63340822  1.66417822  1.69238213  1.71707634
## EDUC                        -0.25946511 -0.39372432 -0.51577897 -0.62991502
## ETHNIC                       0.56587608  0.57279096  0.57897701  0.58486967
## INCOME                      -0.18211542 -0.19036232 -0.19801239 -0.20460532
## HHLARGE                      2.42098877  2.53496864  2.63530500  2.71888427
## WORKWOM                     -0.82315491 -0.78469138 -0.74961118 -0.71823970
## HVAL150                      0.02355404  0.05278751  0.07937156  0.10364399
## SSTRDIST                    -0.01586244 -0.01630790 -0.01671330 -0.01708063
## SSTRVOL                     -0.06702032 -0.06563168 -0.06436671 -0.06321177
## CPDIST5                      0.04355582  0.04551341  0.04730215  0.04891286
## CPWVOL5                     -0.45377778 -0.45816468 -0.46215646 -0.46578882
## log(lagged_price)            0.52871121  0.53025107  0.53167357  0.53296535
## log(price):brandminute.maid  0.04001360  0.06217480  0.08141326  0.09948929
## log(price):brandtropicana    0.50060607  0.52161438  0.53978181  0.55729630
## log(price):EDUC              2.00534260  2.13282695  2.24911528  2.35755991
## log(price):HHLARGE          -5.41111199 -5.44262656 -5.46661454 -5.48121123
##                                                                            
## (Intercept)                 12.43594928 12.49327950 12.54849750 12.59427098
## (Intercept)                  .           .           .           .         
## log(price)                  -2.92041174 -2.95461803 -2.98440425 -3.01358479
## feat                         0.83008678  0.83010993  0.83011783  0.83014102
## brandminute.maid             0.54627138  0.53982713  0.53424091  0.52865658
## brandtropicana               0.64124248  0.63495858  0.63013876  0.62487911
## AGE60                        1.73953598  1.76007794  1.78008582  1.79686106
## EDUC                        -0.73265725 -0.82568029 -0.90778356 -0.98835012
## ETHNIC                       0.59012125  0.59487010  0.59891193  0.60311613
## INCOME                      -0.21069242 -0.21627774 -0.22184321 -0.22624165
## HHLARGE                      2.79414288  2.86297663  2.93144584  2.98754837
## WORKWOM                     -0.68993536 -0.66414588 -0.63971679 -0.61804138
## HVAL150                      0.12553650  0.14542717  0.16361450  0.18054600
## SSTRDIST                    -0.01741497 -0.01771965 -0.01800058 -0.01825366
## SSTRVOL                     -0.06217196 -0.06122856 -0.06036342 -0.05955436
## CPDIST5                      0.05038194  0.05172205  0.05297054  0.05407551
## CPWVOL5                     -0.46908949 -0.47209416 -0.47484491 -0.47735778
## log(lagged_price)            0.53415588  0.53523094  0.53620438  0.53709522
## log(price):brandminute.maid  0.11584326  0.13070366  0.14373875  0.15640616
## log(price):brandtropicana    0.57333871  0.58793291  0.60016790  0.61239290
## log(price):EDUC              2.45560073  2.54444957  2.62323644  2.69942441
## log(price):HHLARGE          -5.49351754 -5.50496111 -5.51929082 -5.52794173
##                                                                            
## (Intercept)                 12.64125964 12.68028713 12.71553397 12.74863959
## (Intercept)                  .           .           .           .         
## log(price)                  -3.03849633 -3.06204121 -3.08395395 -3.10334368
## feat                         0.83015021  0.83016216  0.83017983  0.83019287
## brandminute.maid             0.52399166  0.51955002  0.51539169  0.51177240
## brandtropicana               0.62090398  0.61681637  0.61277068  0.60915451
## AGE60                        1.81374448  1.82792141  1.84058970  1.85223138
## EDUC                        -1.05612673 -1.12095975 -1.18042300 -1.23209116
## ETHNIC                       0.60641350  0.60974757  0.61283126  0.61547820
## INCOME                      -0.23097914 -0.23478723 -0.23817664 -0.24139706
## HHLARGE                      3.04329190  3.09126615  3.13287212  3.17099147
## WORKWOM                     -0.59753465 -0.57962719 -0.56349783 -0.54910305
## HVAL150                      0.19568507  0.20951093  0.22208034  0.23319992
## SSTRDIST                    -0.01848744 -0.01869739 -0.01888833 -0.01906172
## SSTRVOL                     -0.05883843 -0.05818197 -0.05758547 -0.05706462
## CPDIST5                      0.05511988  0.05604233  0.05687875  0.05764383
## CPWVOL5                     -0.47963386 -0.48170706 -0.48359478 -0.48529460
## log(lagged_price)            0.53789145  0.53863662  0.53931073  0.53992654
## log(price):brandminute.maid  0.16726139  0.17747599  0.18697350  0.19538225
## log(price):brandtropicana    0.62252700  0.63232256  0.64162367  0.64997515
## log(price):EDUC              2.76457512  2.82629431  2.88287934  2.93254934
## log(price):HHLARGE          -5.53777656 -5.54625786 -5.55201686 -5.55757177
##                                                                            
## (Intercept)                 12.78157400 12.80810907 12.82762813 12.86089275
## (Intercept)                  .           .           .           .         
## log(price)                  -3.12082467 -3.13720760 -3.14854579 -3.16439162
## feat                         0.83020878  0.83021681  0.83023099  0.83026586
## brandminute.maid             0.50841414  0.50533205  0.50348587  0.49986906
## brandtropicana               0.60637117  0.60355343  0.60032582  0.59838138
## AGE60                        1.86444289  1.87406691  1.87989866  1.89407436
## EDUC                        -1.28039657 -1.32540748 -1.34834439 -1.40133742
## ETHNIC                       0.61782961  0.62016815  0.62204554  0.62381791
## INCOME                      -0.24474771 -0.24731531 -0.24911793 -0.25278022
## HHLARGE                      3.21063182  3.24277038  3.26143410  3.31101067
## WORKWOM                     -0.53407898 -0.52183293 -0.51650262 -0.49726451
## HVAL150                      0.24410369  0.25364133  0.25871044  0.27102640
## SSTRDIST                    -0.01922301 -0.01936736 -0.01947186 -0.01962014
## SSTRVOL                     -0.05656320 -0.05611055 -0.05604008 -0.05533928
## CPDIST5                      0.05837388  0.05900672  0.05950327  0.06017110
## CPWVOL5                     -0.48684241 -0.48826978 -0.48910950 -0.49067416
## log(lagged_price)            0.54044231  0.54095792  0.54143334  0.54173852
## log(price):brandminute.maid  0.20306913  0.21013829  0.21529590  0.22259818
## log(price):brandtropicana    0.65704637  0.66380377  0.67037988  0.67586509
## log(price):EDUC              2.97842680  3.02120317  3.04583393  3.09313418
## log(price):HHLARGE          -5.56416421 -5.56925338 -5.57323017 -5.58479171
##                                                                            
## (Intercept)                 12.87642103 12.89780747 12.90493231 12.92990292
## (Intercept)                  .           .           .           .         
## log(price)                  -3.17402217 -3.18682042 -3.19406687 -3.20705783
## feat                         0.83024707  0.83026862  0.83026343  0.83029950
## brandminute.maid             0.49842194  0.49569086  0.49477851  0.49166543
## brandtropicana               0.59689660  0.59450838  0.59270772  0.59023814
## AGE60                        1.89925100  1.90740702  1.90854572  1.91908793
## EDUC                        -1.42460115 -1.46196823 -1.47676288 -1.51719391
## ETHNIC                       0.62553941  0.62713798  0.62873558  0.62997489
## INCOME                      -0.25424091 -0.25636804 -0.25679775 -0.25945782
## HHLARGE                      3.32867054  3.35682261  3.36273272  3.39635380
## WORKWOM                     -0.49183437 -0.48087490 -0.48027429 -0.46582037
## HVAL150                      0.27611748  0.28397038  0.28666848  0.29561875
## SSTRDIST                    -0.01971012 -0.01982105 -0.01989514 -0.01999645
## SSTRVOL                     -0.05520987 -0.05477336 -0.05470068 -0.05421182
## CPDIST5                      0.06058898  0.06106453  0.06135875  0.06182893
## CPWVOL5                     -0.49144710 -0.49260961 -0.49329967 -0.49435346
## log(lagged_price)            0.54217152  0.54250147  0.54289820  0.54309983
## log(price):brandminute.maid  0.22663720  0.23240972  0.23536744  0.24144209
## log(price):brandtropicana    0.68002851  0.68534164  0.68925678  0.69449204
## log(price):EDUC              3.11648337  3.15135602  3.16719347  3.20366536
## log(price):HHLARGE          -5.58899801 -5.59406327 -5.59624087 -5.60063467
##                                                                            
## (Intercept)                 12.93034523 12.94801276 12.96802895 12.96787456
## (Intercept)                  .           .           .           .         
## log(price)                  -3.21226319 -3.21985852 -3.23105939 -3.23286850
## feat                         0.83029924  0.83029154  0.83033707  0.83039810
## brandminute.maid             0.49118469  0.48973216  0.48692400  0.48690926
## brandtropicana               0.58872115  0.58698302  0.58488008  0.58456673
## AGE60                        1.91680594  1.92398442  1.93268012  1.93145922
## EDUC                        -1.52591176 -1.54373529 -1.58086944 -1.58361715
## ETHNIC                       0.63126794  0.63225496  0.63330449  0.63386590
## INCOME                      -0.25907449 -0.26097419 -0.26309920 -0.26289539
## HHLARGE                      3.39234182  3.41241783  3.44096555  3.43851247
## WORKWOM                     -0.46877921 -0.46139236 -0.44854951 -0.45037061
## HVAL150                      0.29678922  0.30100062  0.30907916  0.30958271
## SSTRDIST                    -0.02006615 -0.02011196 -0.02020211 -0.02024367
## SSTRVOL                     -0.05405857 -0.05410951 -0.05358492 -0.05352505
## CPDIST5                      0.06202568  0.06234668  0.06273134  0.06285726
## CPWVOL5                     -0.49523546 -0.49533130 -0.49637185 -0.49697970
## log(lagged_price)            0.54348487  0.54369181  0.54381876  0.54401387
## log(price):brandminute.maid  0.24341629  0.24693312  0.25215856  0.25279030
## log(price):brandtropicana    0.69756299  0.70136782  0.70567211  0.70663819
## log(price):EDUC              3.21344666  3.23175451  3.26435632  3.26765846
## log(price):HHLARGE          -5.60018711 -5.60321941 -5.60671487 -5.60769037
# Now ready for cross validation version of the object
cvfit <- cv.glmnet(x, y, alpha=1)
#Results
plot(cvfit)

cvfit$lambda.min
## [1] 0.0006793529
log(cvfit$lambda.min)
## [1] -7.29437
coef(cvfit, s = "lambda.min")
## 22 x 1 sparse Matrix of class "dgCMatrix"
##                                       1
## (Intercept)                 12.96787456
## (Intercept)                  .         
## log(price)                  -3.23286850
## feat                         0.83039810
## brandminute.maid             0.48690926
## brandtropicana               0.58456673
## AGE60                        1.93145922
## EDUC                        -1.58361715
## ETHNIC                       0.63386590
## INCOME                      -0.26289539
## HHLARGE                      3.43851247
## WORKWOM                     -0.45037061
## HVAL150                      0.30958271
## SSTRDIST                    -0.02024367
## SSTRVOL                     -0.05352505
## CPDIST5                      0.06285726
## CPWVOL5                     -0.49697970
## log(lagged_price)            0.54401387
## log(price):brandminute.maid  0.25279030
## log(price):brandtropicana    0.70663819
## log(price):EDUC              3.26765846
## log(price):HHLARGE          -5.60769037
#LASSO is nice because it is transparent and algorithmic rather than up to the discretion of the econometrician.  NOTE: the coefficients of LASSO are different from the coefficients in OLS.  They are biased downward. 


#dcast is a function in the reshape2 library that turns "long data" into "wide data""
oj_prices <-lagged[,1:6]
oj_wide <- dcast(oj_prices, store + week ~ brand)
## Using price as value column: use value.var to override.
#New tidyverse package
#oj %>% select(store, week, brand, price) %>% spread(brand, price)
# gather is going wide to long !!!!gather("variable","value", -store, -week) or gather("variable","value", 3:5)
colnames(oj_wide)[3] <- "P_Dom"
colnames(oj_wide)[4] <- "P_MM"
colnames(oj_wide)[5] <- "P_Trop"
oj_cross <- merge(oj, oj_wide, by=c("week","store"))

#Merge the wide data back in then only look at the cross price elasticity matrix for tropicana.
trop_cross <- subset(oj_cross, brand=="tropicana") 
regcrosst = glm(logmove ~ log(P_Dom)*feat+log(P_MM)*feat+log(P_Trop)*feat, data=trop_cross)
summary(regcrosst)
## 
## Call:
## glm(formula = logmove ~ log(P_Dom) * feat + log(P_MM) * feat + 
##     log(P_Trop) * feat, data = trop_cross)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.3152  -0.3802   0.0019   0.3660   2.6480  
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      10.95288    0.04430 247.244  < 2e-16 ***
## log(P_Dom)        0.15027    0.02958   5.081 3.83e-07 ***
## feat              1.48611    0.10089  14.731  < 2e-16 ***
## log(P_MM)         0.27827    0.03813   7.298 3.17e-13 ***
## log(P_Trop)      -2.16685    0.03782 -57.288  < 2e-16 ***
## log(P_Dom):feat   0.14505    0.10327   1.405     0.16    
## feat:log(P_MM)    0.66526    0.11312   5.881 4.21e-09 ***
## feat:log(P_Trop) -1.73819    0.09673 -17.969  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.3516469)
## 
##     Null deviance: 6683.9  on 9335  degrees of freedom
## Residual deviance: 3280.2  on 9328  degrees of freedom
## AIC: 16747
## 
## Number of Fisher Scoring iterations: 2
MM_cross <- subset(oj_cross, brand=="minute.maid") 
regcrossm = glm(logmove ~ log(P_Dom)*feat+log(P_MM)*feat+log(P_Trop)*feat, data=MM_cross)
summary(regcrossm)
## 
## Call:
## glm(formula = logmove ~ log(P_Dom) * feat + log(P_MM) * feat + 
##     log(P_Trop) * feat, data = MM_cross)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.68138  -0.36949  -0.01947   0.34312   2.72247  
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      10.12742    0.04149 244.096   <2e-16 ***
## log(P_Dom)        0.56164    0.03277  17.141   <2e-16 ***
## feat              1.12989    0.09446  11.962   <2e-16 ***
## log(P_MM)        -2.34574    0.04625 -50.722   <2e-16 ***
## log(P_Trop)       0.32894    0.03732   8.813   <2e-16 ***
## log(P_Dom):feat   0.11417    0.06375   1.791   0.0733 .  
## feat:log(P_MM)   -1.36205    0.08536 -15.956   <2e-16 ***
## feat:log(P_Trop)  0.76155    0.07742   9.837   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.3341122)
## 
##     Null deviance: 9046.9  on 9335  degrees of freedom
## Residual deviance: 3116.6  on 9328  degrees of freedom
## AIC: 16270
## 
## Number of Fisher Scoring iterations: 2
dom_cross <- subset(oj_cross, brand=="dominicks") 
regcrossd = glm(logmove ~ log(P_Dom)*feat+log(P_MM)*feat+log(P_Trop)*feat, data=dom_cross)
summary(regcrossd)
## 
## Call:
## glm(formula = logmove ~ log(P_Dom) * feat + log(P_MM) * feat + 
##     log(P_Trop) * feat, data = dom_cross)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -4.8345  -0.5141  -0.0078   0.4968   3.0611  
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      10.10162    0.06169 163.754  < 2e-16 ***
## log(P_Dom)       -2.86900    0.04790 -59.893  < 2e-16 ***
## feat             -0.56275    0.11615  -4.845 1.29e-06 ***
## log(P_MM)         0.80506    0.05481  14.689  < 2e-16 ***
## log(P_Trop)      -0.26275    0.05159  -5.093 3.59e-07 ***
## log(P_Dom):feat  -0.56809    0.08929  -6.362 2.08e-10 ***
## feat:log(P_MM)    1.21816    0.12356   9.859  < 2e-16 ***
## feat:log(P_Trop)  0.71789    0.09660   7.432 1.16e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.6610932)
## 
##     Null deviance: 13359.9  on 9335  degrees of freedom
## Residual deviance:  6166.7  on 9328  degrees of freedom
## AIC: 22641
## 
## Number of Fisher Scoring iterations: 2
rownames = c("Q Trop", "Q MM", "Q Dom")
colnames = c("P Trop", "P MM", "P Dom")
Elast_matrix <- matrix(,3,3, dimnames = list(rownames, colnames))

#This code is a hack; it would be much better to do this assignment as a loop; that will show up in the next homework or two
Elast_matrix[1,1] <- coef(regcrosst)["log(P_Trop)"] 
Elast_matrix[1,2] <- coef(regcrosst)["log(P_MM)"] 
Elast_matrix[1,3] <- coef(regcrosst)["log(P_Dom)"] 
Elast_matrix[2,1] <- coef(regcrossm)["log(P_Trop)"] 
Elast_matrix[2,2] <- coef(regcrossm)["log(P_MM)"] 
Elast_matrix[2,3] <- coef(regcrossm)["log(P_Dom)"] 
Elast_matrix[3,1] <- coef(regcrossd)["log(P_Trop)"] 
Elast_matrix[3,2] <- coef(regcrossd)["log(P_MM)"] 
Elast_matrix[3,3] <- coef(regcrossd)["log(P_Dom)"] 

Elast_matrix
##            P Trop       P MM      P Dom
## Q Trop -2.1668512  0.2782743  0.1502673
## Q MM    0.3289361 -2.3457404  0.5616439
## Q Dom  -0.2627461  0.8050605 -2.8689953
# We see that orange juice is a substitute for other orange juice. Minute Maid has twice the cross price elasticity as Dominicks, which makes sense.



###################
## LASSO implementation
###################
# Useful links to tutorials
# https://web.stanford.edu/~hastie/Papers/Glmnet_Vignette.pdf
# http://www4.stat.ncsu.edu/~post/josh/LASSO_Ridge_Elastic_Net_-_Examples.html
#


trop_cross$price <- log(trop_cross$price)
trop_cross$P_MM <- log(trop_cross$P_MM)
trop_cross$P_Dom <- log(trop_cross$P_Dom)
trop_cross$P_Trop <- log(trop_cross$P_Trop)
# NOTE: Must change to reading in the log price directly 
x <- as.matrix(trop_cross[ ,5:20])
#x2 <- as.matrix(df[ ,c(2:235, 237)])
y <- as.numeric(as.matrix(trop_cross[ ,4]))
#ydh <- as.double(as.matrix(df[ ,237]))

set.seed(720)
#lasso_v1 <- cv.glmnet(x, y, alpha=1)
lasso_v1 <- glmnet(x, y, alpha=1)

#Results
plot(lasso_v1)

coef(lasso_v1, s=lasso_v1$lambda.min)
## 17 x 71 sparse Matrix of class "dgCMatrix"
##    [[ suppressing 71 column names 's0', 's1', 's2' ... ]]
##                                                                           
## (Intercept) 9.113095  9.3636565  9.5919585  9.7999789  9.9895192 10.162221
## feat        .         .          .          .          .          .       
## price       .        -0.2424893 -0.4634365 -0.6647553 -0.8481895 -1.015328
## AGE60       .         .          .          .          .          .       
## EDUC        .         .          .          .          .          .       
## ETHNIC      .         .          .          .          .          .       
## INCOME      .         .          .          .          .          .       
## HHLARGE     .         .          .          .          .          .       
## WORKWOM     .         .          .          .          .          .       
## HVAL150     .         .          .          .          .          .       
## SSTRDIST    .         .          .          .          .          .       
## SSTRVOL     .         .          .          .          .          .       
## CPDIST5     .         .          .          .          .          .       
## CPWVOL5     .         .          .          .          .          .       
## P_Dom       .         .          .          .          .          .       
## P_MM        .         .          .          .          .          .       
## P_Trop      .         .          .          .          .          .       
##                                                                                
## (Intercept) 10.319581 10.4438187 10.52920524 10.598464158 10.6455464 10.6884154
## feat         .         0.0209238  0.07039071  0.114879653  0.1541166  0.1899014
## price       -1.167618 -1.2912176 -1.38180665 -1.464394936 -1.5498808 -1.6253536
## AGE60        .         .          .           .            .          .        
## EDUC         .         .          .           .            .          .        
## ETHNIC       .         .          .           .            .          .        
## INCOME       .         .          .           .            .          .        
## HHLARGE      .         .          .           .            .          .        
## WORKWOM      .         .          .           .            .          .        
## HVAL150      .         .          .           0.033189385  0.1263768  0.2112843
## SSTRDIST     .         .          .           .            .          .        
## SSTRVOL      .         .          .           .            .          .        
## CPDIST5      .         .          .           .            .          .        
## CPWVOL5      .         .          .           .            .          .        
## P_Dom        .         .          .           .            .          .        
## P_MM         .         .          .           .            .          .        
## P_Trop       .         .          .          -0.002627611  .          .        
##                                                                           
## (Intercept) 10.7274761 10.7703971457 10.8512702 10.862234685 10.8634444942
## feat         0.2225071  0.2521991085  0.2790193  0.302733492  0.3241982639
## price       -1.6941215 -1.7568665063 -1.8155678 -1.868905229 -1.9252424074
## AGE60        .          .             .          0.211324555  0.4322210396
## EDUC         .          .             .          .            .           
## ETHNIC       .          .             .          .            .           
## INCOME       .          .             .          .            .           
## HHLARGE      .         -0.0521163663 -0.3955450 -0.501861755 -0.5680003552
## WORKWOM      .          .             .          .            .           
## HVAL150      0.2886489  0.3559837608  0.3994133  0.457631866  0.5133662568
## SSTRDIST     .          .             .          .            .           
## SSTRVOL      .          .             .          .            .           
## CPDIST5      .          .             .          .            .           
## CPWVOL5      .          .             .          .            .           
## P_Dom        .          .             .          .            .           
## P_MM         .          .             .          .            .           
## P_Trop       .         -0.0001221817  .         -0.003945815 -0.0003646148
##                                                                    
## (Intercept) 10.8973415361 10.9406370745 10.9791950998 11.0143109284
## feat         0.3436817426  0.3611243888  0.3770009743  0.3914664373
## price       -1.9734939612 -2.0180946512 -2.0593292606 -2.0969189669
## AGE60        0.6118646442  0.7659346798  0.9075682969  1.0366413682
## EDUC         .             .             .             .           
## ETHNIC       .             .             .             .           
## INCOME       .             .             .             .           
## HHLARGE     -0.6841265782 -0.8185312209 -0.9367821813 -1.0444478405
## WORKWOM      .             .             .             .           
## HVAL150      0.5669965833  0.6216868723  0.6719642704  0.7177849925
## SSTRDIST     .             .             .             .           
## SSTRVOL     -0.0053368361 -0.0075416240 -0.0094620485 -0.0112096999
## CPDIST5      .             .             .             .           
## CPWVOL5     -0.0388784844 -0.0999882498 -0.1557268431 -0.2065157405
## P_Dom        .             .             .             .           
## P_MM         .             .             .             .           
## P_Trop      -0.0002528143 -0.0008009768 -0.0007472381 -0.0006812219
##                                                                    
## (Intercept) 11.0463068322 11.0754603015 11.1020238568 11.1262275790
## feat         0.4046468145  0.4166562830  0.4275988635  0.4375693352
## price       -2.1311696906 -2.1623776821 -2.1908132418 -2.2167226647
## AGE60        1.1542483722  1.2614075027  1.3590469115  1.4480123043
## EDUC         .             .             .             .           
## ETHNIC       .             .             .             .           
## INCOME       .             .             .             .           
## HHLARGE     -1.1425471973 -1.2319316463 -1.3133754266 -1.3875839660
## WORKWOM      .             .             .             .           
## HVAL150      0.7595353227  0.7975766724  0.8322385336  0.8638211306
## SSTRDIST     .             .             .             .           
## SSTRVOL     -0.0128020518 -0.0142529426 -0.0155749403 -0.0167794954
## CPDIST5      .             .             .             .           
## CPWVOL5     -0.2527927315 -0.2949586030 -0.3333785747 -0.3683854220
## P_Dom        .             .             .             .           
## P_MM         .             .             .             .           
## P_Trop      -0.0006207113 -0.0005655692 -0.0005153256 -0.0004695455
##                                                                               
## (Intercept) 11.1486714683 11.160758506 11.165655309 11.176423863 11.2486224286
## feat         0.4466778546  0.454531781  0.461423830  0.467709868  0.4734829122
## price       -2.2403248009 -2.261990760 -2.285098674 -2.305487267 -2.3237907457
## AGE60        1.5286161326  1.615033438  1.704338548  1.780191473  1.7739203488
## EDUC         .             .            .            .            .           
## ETHNIC       .             0.027321572  0.075077360  0.118079216  0.1529456727
## INCOME       .             .            .            .            .           
## HHLARGE     -1.4570823575 -1.515792556 -1.568209246 -1.622083356 -1.7589420880
## WORKWOM      .             .            .           -0.013546381 -0.1361551644
## HVAL150      0.8923374274  0.924654118  0.957961690  0.988716788  1.0170123570
## SSTRDIST     .             .            .            .            .           
## SSTRVOL     -0.0179388731 -0.021746326 -0.028025516 -0.033847942 -0.0404600705
## CPDIST5      .             .            .            .            .           
## CPWVOL5     -0.4002164697 -0.419383209 -0.427839318 -0.434859834 -0.4362636377
## P_Dom        .             .            .            .            .           
## P_MM         .             .            .            .            .           
## P_Trop      -0.0003967452 -0.001891497 -0.001096458 -0.001056376 -0.0009323071
##                                                                               
## (Intercept) 11.3148574974 11.373497007 11.4150686289 11.44897422  1.165731e+01
## feat         0.4787682213  0.483327510  0.4867086852  0.48928712  4.916038e-01
## price       -2.3404204707 -2.355644774 -2.3754685088 -2.39361099 -2.410036e+00
## AGE60        1.7675367884  1.760480539  1.7728719117  1.78649037  1.801561e+00
## EDUC         .             0.002981127  0.0748659767  0.13811059  2.192971e-01
## ETHNIC       0.1843194965  0.213038521  0.2379624604  0.26049955  2.676830e-01
## INCOME       .             .            .             .          -1.751407e-02
## HHLARGE     -1.8870904075 -2.003412042 -2.1061171023 -2.19362384 -2.223849e+00
## WORKWOM     -0.2470504829 -0.351004220 -0.4556988489 -0.54723985 -6.316530e-01
## HVAL150      1.0423489056  1.064135879  1.0554242249  1.04833764  1.042858e+00
## SSTRDIST     .             .            .             .           .           
## SSTRVOL     -0.0464789492 -0.052171640 -0.0588458519 -0.06487980 -7.155204e-02
## CPDIST5      .             .            .             .           .           
## CPWVOL5     -0.4377308566 -0.438632884 -0.4383352481 -0.43806239 -4.351136e-01
## P_Dom        .             .            0.0019998538  0.01187433  2.069156e-02
## P_MM         .             0.005269569  0.0225912128  0.03714275  4.993092e-02
## P_Trop      -0.0008135327 -0.001965341 -0.0002098574  .          -9.207667e-05
##                                                                    
## (Intercept)  1.199013e+01  1.228517e+01 12.6202096559 12.9967141694
## feat         4.938124e-01  4.959368e-01  0.4980080642  0.4999311078
## price       -2.424691e+00 -2.437406e+00 -2.4481870288 -2.4578514304
## AGE60        1.853239e+00  1.919592e+00  1.9803534889  2.0425141356
## EDUC         3.311907e-01  4.269284e-01  0.5285200121  0.6275256799
## ETHNIC       2.688311e-01  2.791452e-01  0.2904205046  0.2973114041
## INCOME      -4.948575e-02 -7.890468e-02 -0.1137908121 -0.1537724245
## HHLARGE     -2.175077e+00 -2.110002e+00 -2.0483096488 -1.9626339224
## WORKWOM     -6.812585e-01 -7.060447e-01 -0.7230822137 -0.7300521630
## HVAL150      1.035151e+00  1.033847e+00  1.0365664623  1.0441736485
## SSTRDIST    -9.758202e-04 -2.792227e-03 -0.0045537782 -0.0061786858
## SSTRVOL     -7.665066e-02 -7.953011e-02 -0.0820556840 -0.0843084609
## CPDIST5      .             .             0.0054395009  0.0128226043
## CPWVOL5     -4.376095e-01 -4.456494e-01 -0.4533300325 -0.4601496206
## P_Dom        2.905829e-02  3.703585e-02  0.0445745836  0.0515095088
## P_MM         6.212493e-02  7.395574e-02  0.0855454361  0.0962340765
## P_Trop      -8.974930e-06 -3.144655e-05 -0.0002591993 -0.0005213386
##                                                                   
## (Intercept) 13.321678401 13.6169373957 13.8856011617 14.1331401527
## feat         0.501685143  0.5032834030  0.5047395697  0.5060668619
## price       -2.466980443 -2.4752093795 -2.4827167038 -2.4895941148
## AGE60        2.097322348  2.1468616544  2.1919180028  2.2341084029
## EDUC         0.716870319  0.7976826727  0.8711565455  0.9400555036
## ETHNIC       0.305344057  0.3127612488  0.3195630781  0.3254208861
## INCOME      -0.188262345 -0.2195852767 -0.2480854815 -0.2743793267
## HHLARGE     -1.894323084 -1.8326672547 -1.7766685346 -1.7239771882
## WORKWOM     -0.738000445 -0.7454240550 -0.7522132532 -0.7579757350
## HVAL150      1.050026925  1.0555314583  1.0605866662  1.0646060551
## SSTRDIST    -0.007672848 -0.0090337864 -0.0102740200 -0.0114046140
## SSTRVOL     -0.086335586 -0.0881771783 -0.0898526141 -0.0914021799
## CPDIST5      0.019459596  0.0255046605  0.0310117103  0.0360353442
## CPWVOL5     -0.466528268 -0.4723350686 -0.4776280024 -0.4824571838
## P_Dom        0.057846222  0.0636195333  0.0688799156  0.0736748605
## P_MM         0.106048169  0.1149894751  0.1231367472  0.1305637901
## P_Trop      -0.000396378 -0.0003721749 -0.0003408628 -0.0002716806
##                                                                   
## (Intercept) 14.3558768086 14.558312128 14.7422364587 14.9134867448
## feat         0.5072756906  0.508377028  0.5093804497  0.5102954534
## price       -2.4957947788 -2.501469907 -2.5066460405 -2.5113922015
## AGE60        2.2715920914  2.305576859  2.3364692082  2.3656657437
## EDUC         1.0011849867  1.056554498  1.1068395233  1.1545403224
## ETHNIC       0.3310984129  0.336333938  0.3411627297  0.3451275272
## INCOME      -0.2980124263 -0.319487611 -0.3389993805 -0.3571912397
## HHLARGE     -1.6775812240 -1.635576435 -1.5975345946 -1.5609053639
## WORKWOM     -0.7635885235 -0.768748430 -0.7734606334 -0.7773900311
## HVAL150      1.0687297507  1.072585912  1.0761322199  1.0788612574
## SSTRDIST    -0.0124349256 -0.013373827 -0.0142297795 -0.0150086639
## SSTRVOL     -0.0927937893 -0.094056441 -0.0952027680 -0.0962742031
## CPDIST5      0.0406056368  0.044768878  0.0485606114  0.0520252380
## CPWVOL5     -0.4868574125 -0.490869499 -0.4945320492 -0.4978569263
## P_Dom        0.0780426887  0.082022200  0.0856482774  0.0889531607
## P_MM         0.1373301595  0.143494964  0.1491129649  0.1542311111
## P_Trop      -0.0002767043 -0.000256639 -0.0002331071 -0.0001796895
##                                                                    
## (Intercept) 15.0609994437 15.2031842042 15.3284239716 15.4470835588
## feat         0.5111280114  0.5118877030  0.5125799799  0.5132106604
## price       -2.5156413614 -2.5195917434 -2.5231527872 -2.5264171640
## AGE60        2.3904233101  2.4143583095  2.4357099973  2.4558722379
## EDUC         1.1945505214  1.2336650488  1.2682942445  1.3012859026
## ETHNIC       0.3497104754  0.3530136634  0.3564762450  0.3591444113
## INCOME      -0.3728328614 -0.3879234181 -0.4012229133 -0.4138254918
## HHLARGE     -1.5317805991 -1.5015250539 -1.4758432026 -1.4503450420
## WORKWOM     -0.7815454589 -0.7849298117 -0.7880773182 -0.7907931210
## HVAL150      1.0821541912  1.0846115983  1.0869706401  1.0888459766
## SSTRDIST    -0.0157228649 -0.0163684045 -0.0169605290 -0.0174966893
## SSTRVOL     -0.0971896720 -0.0980759543 -0.0988533321 -0.0995964176
## CPDIST5      0.0551562902  0.0580341408  0.0606405776  0.0630309173
## CPWVOL5     -0.5009501650 -0.5036981504 -0.5062648313 -0.5085455802
## P_Dom        0.0919644135  0.0947071099  0.0972078640  0.0994854797
## P_MM         0.1589031528  0.1631485530  0.1670263819  0.1705525053
## P_Trop      -0.0002071088 -0.0001546598 -0.0001420728 -0.0001127566
##                                                                    
## (Intercept) 15.5503143078  1.564898e+01  1.573379e+01  1.581577e+01
## feat         0.5137863924  5.143098e-01  5.147896e-01  5.152238e-01
## price       -2.5293659899 -2.532071e+00 -2.534519e+00 -2.536762e+00
## AGE60        2.4736321609  2.490368e+00  2.505048e+00  2.518905e+00
## EDUC         1.3300547428  1.357463e+00  1.381200e+00  1.403903e+00
## ETHNIC       0.3620688483  3.642723e-01  3.667546e-01  3.685876e-01
## INCOME      -0.4247943525 -4.352724e-01 -4.442866e-01 -4.529901e-01
## HHLARGE     -1.4292973230 -1.408077e+00 -1.390986e+00 -1.373367e+00
## WORKWOM     -0.7933511443 -7.956222e-01 -7.977132e-01 -7.996270e-01
## HVAL150      1.0907448800  1.092304e+00  1.093872e+00  1.095192e+00
## SSTRDIST    -0.0179891357 -1.843405e-02 -1.884333e-02 -1.921256e-02
## SSTRVOL     -0.1002347542 -1.008538e-01 -1.013736e-01 -1.018890e-01
## CPDIST5      0.0651905204  6.717575e-02  6.896339e-02  7.061231e-02
## CPWVOL5     -0.5107017298 -5.125893e-01 -5.144077e-01 -5.159680e-01
## P_Dom        0.1015625236  1.034533e-01  1.051784e-01  1.067480e-01
## P_MM         0.1737745799  1.767016e-01  1.793781e-01  1.818078e-01
## P_Trop      -0.0001075201 -8.833642e-05 -8.167192e-05 -6.906215e-05
##                                                                    
## (Intercept) 15.8915878237  1.595499e+01  1.601698e+01  1.607474e+01
## feat         0.5156196854  5.159854e-01  5.163134e-01  5.166124e-01
## price       -2.5388116170 -2.540655e+00 -2.542347e+00 -2.543897e+00
## AGE60        2.5319006243  2.543050e+00  2.553550e+00  2.563413e+00
## EDUC         1.4252341670  1.443243e+00  1.460452e+00  1.476671e+00
## ETHNIC       0.3701227702  3.720190e-01  3.734181e-01  3.745463e-01
## INCOME      -0.4610491533 -4.677952e-01 -4.743776e-01 -4.805154e-01
## HHLARGE     -1.3566812965 -1.344023e+00 -1.330700e+00 -1.317911e+00
## WORKWOM     -0.8012359345 -8.027412e-01 -8.041976e-01 -8.054245e-01
## HVAL150      1.0962146721  1.097317e+00  1.098301e+00  1.099070e+00
## SSTRDIST    -0.0195488833 -1.985869e-02 -2.013820e-02 -2.039232e-02
## SSTRVOL     -0.1023668736 -1.027522e-01 -1.031441e-01 -1.035095e-01
## CPDIST5      0.0721175767  7.346348e-02  7.471112e-02  7.585172e-02
## CPWVOL5     -0.5173887479 -5.187953e-01 -5.199714e-01 -5.210365e-01
## P_Dom        0.1081785247  1.094846e-01  1.106719e-01  1.117538e-01
## P_MM         0.1840218620  1.860481e-01  1.878867e-01  1.895609e-01
## P_Trop      -0.0000514194 -5.007404e-05 -4.591394e-05 -3.369295e-05
##                                                                    
## (Intercept)  1.612173e+01  1.616841e+01  1.621241e+01  1.624680e+01
## feat         5.168933e-01  5.171406e-01  5.173661e-01  5.175834e-01
## price       -2.545286e+00 -2.546565e+00 -2.547738e+00 -2.548780e+00
## AGE60        2.571745e+00  2.579649e+00  2.587101e+00  2.593239e+00
## EDUC         1.490100e+00  1.503045e+00  1.515321e+00  1.525161e+00
## ETHNIC       3.760091e-01  3.771045e-01  3.779426e-01  3.790799e-01
## INCOME      -4.855168e-01 -4.904727e-01 -4.951455e-01 -4.988045e-01
## HHLARGE     -1.308759e+00 -1.298788e+00 -1.289005e+00 -1.282587e+00
## WORKWOM     -8.065421e-01 -8.076654e-01 -8.086195e-01 -8.094736e-01
## HVAL150      1.099892e+00  1.100654e+00  1.101256e+00  1.101886e+00
## SSTRDIST    -2.062589e-02 -2.083770e-02 -2.102977e-02 -2.120441e-02
## SSTRVOL     -1.037925e-01 -1.040885e-01 -1.043679e-01 -1.045771e-01
## CPDIST5      7.686156e-02  7.780537e-02  7.867027e-02  7.942291e-02
## CPWVOL5     -5.221256e-01 -5.230141e-01 -5.238093e-01 -5.246465e-01
## P_Dom        1.127425e-01  1.136407e-01  1.144588e-01  1.152076e-01
## P_MM         1.910930e-01  1.924847e-01  1.937505e-01  1.949066e-01
## P_Trop      -3.258248e-05 -3.169255e-05 -2.295239e-05 -2.485327e-05
##                                                                    
## (Intercept) 16.2816298426  1.631503e+01  1.634621e+01 16.3688276262
## feat         0.5177699447  5.179394e-01  5.180938e-01  0.5182506694
## price       -2.5497471114 -2.550636e+00 -2.551446e+00 -2.5521512305
## AGE60        2.5991363611  2.604728e+00  2.610005e+00  2.6141752937
## EDUC         1.5348037722  1.544028e+00  1.552753e+00  1.5593354532
## ETHNIC       0.3799701021  3.806160e-01  3.811191e-01  0.3819074572
## INCOME      -0.5025019027 -5.060467e-01 -5.093592e-01 -0.5117671572
## HHLARGE     -1.2752834201 -1.267867e+00 -1.260719e+00 -1.2566130681
## WORKWOM     -0.8103509916 -8.111122e-01 -8.117416e-01 -0.8123433814
## HVAL150      1.1024937020  1.102988e+00  1.103361e+00  1.1037438639
## SSTRDIST    -0.0213652851 -2.151073e-02 -2.164298e-02 -0.0217597718
## SSTRVOL     -0.1047973837 -1.050102e-01 -1.052089e-01 -0.1053619950
## CPDIST5      0.0801359886  8.079229e-02  8.139255e-02  0.0818968018
## CPWVOL5     -0.5253241112 -5.259170e-01 -5.264537e-01 -0.5270167756
## P_Dom        0.1158871038  1.165056e-01  1.170692e-01  0.1175837818
## P_MM         0.1959607971  1.969183e-01  1.977901e-01  0.1985833188
## P_Trop      -0.0000247842 -1.846476e-05 -1.201702e-05 -0.0000186862
##                                                                    
## (Intercept)  1.639844e+01  1.641180e+01  1.643490e+01  1.644905e+01
## feat         5.183638e-01  5.185106e-01  5.186044e-01  5.187169e-01
## price       -2.552846e+00 -2.553388e+00 -2.553980e+00 -2.554451e+00
## AGE60        2.618941e+00  2.621724e+00  2.625286e+00  2.627899e+00
## EDUC         1.567467e+00  1.571209e+00  1.577604e+00  1.581407e+00
## ETHNIC       3.821948e-01  3.830722e-01  3.835637e-01  3.841904e-01
## INCOME      -5.149091e-01 -5.163267e-01 -5.187766e-01 -5.202743e-01
## HHLARGE     -1.249228e+00 -1.247940e+00 -1.242796e+00 -1.240542e+00
## WORKWOM     -8.128909e-01 -8.135300e-01 -8.140436e-01 -8.146006e-01
## HVAL150      1.104057e+00  1.104511e+00  1.104884e+00  1.105280e+00
## SSTRDIST    -2.187456e-02 -2.195903e-02 -2.206326e-02 -2.213763e-02
## SSTRVOL     -1.055415e-01 -1.056759e-01 -1.057813e-01 -1.059035e-01
## CPDIST5      8.242900e-02  8.279338e-02  8.324512e-02  8.357585e-02
## CPWVOL5     -5.274176e-01 -5.278571e-01 -5.282899e-01 -5.286192e-01
## P_Dom        1.180516e-01  1.184842e-01  1.188679e-01  1.192270e-01
## P_MM         1.993123e-01  1.999527e-01  2.005743e-01  2.011089e-01
## P_Trop      -9.897414e-06 -4.332179e-05 -1.586943e-05 -3.519192e-05
# Now ready for cross validation version of the object
cvfit <- cv.glmnet(x, y, alpha=1)
#Results
plot(cvfit)

cvfit$lambda.min
## [1] 0.0008242127
log(cvfit$lambda.min)
## [1] -7.101082
coef(cvfit, s = "lambda.min")
## 17 x 1 sparse Matrix of class "dgCMatrix"
##                         1
## (Intercept)  1.644905e+01
## feat         5.187169e-01
## price       -2.554451e+00
## AGE60        2.627899e+00
## EDUC         1.581407e+00
## ETHNIC       3.841904e-01
## INCOME      -5.202743e-01
## HHLARGE     -1.240542e+00
## WORKWOM     -8.146006e-01
## HVAL150      1.105280e+00
## SSTRDIST    -2.213763e-02
## SSTRVOL     -1.059035e-01
## CPDIST5      8.357585e-02
## CPWVOL5     -5.286192e-01
## P_Dom        1.192270e-01
## P_MM         2.011089e-01
## P_Trop      -3.519192e-05
# Check relative to OLS
reg_lasso <- glm(logmove ~ feat + price + AGE60 + EDUC + ETHNIC + INCOME+ HHLARGE + WORKWOM + HVAL150 + SSTRDIST + SSTRVOL + CPDIST5 + CPWVOL5 + P_MM + P_Dom + P_Trop, data=trop_cross)
summary(reg_lasso)
## 
## Call:
## glm(formula = logmove ~ feat + price + AGE60 + EDUC + ETHNIC + 
##     INCOME + HHLARGE + WORKWOM + HVAL150 + SSTRDIST + SSTRVOL + 
##     CPDIST5 + CPWVOL5 + P_MM + P_Dom + P_Trop, data = trop_cross)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.59850  -0.30183  -0.01105   0.28559   2.81404  
## 
## Coefficients: (1 not defined because of singularities)
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 16.672684   0.419348  39.759  < 2e-16 ***
## feat         0.519680   0.014537  35.748  < 2e-16 ***
## price       -2.559681   0.027930 -91.645  < 2e-16 ***
## AGE60        2.668487   0.161997  16.472  < 2e-16 ***
## EDUC         1.649849   0.129046  12.785  < 2e-16 ***
## ETHNIC       0.385353   0.047286   8.149 4.13e-16 ***
## INCOME      -0.544194   0.042310 -12.862  < 2e-16 ***
## HHLARGE     -1.182697   0.293007  -4.036 5.47e-05 ***
## WORKWOM     -0.816628   0.186575  -4.377 1.22e-05 ***
## HVAL150      1.104607   0.053199  20.764  < 2e-16 ***
## SSTRDIST    -0.023005   0.001868 -12.317  < 2e-16 ***
## SSTRVOL     -0.107334   0.012439  -8.629  < 2e-16 ***
## CPDIST5      0.087552   0.007996  10.950  < 2e-16 ***
## CPWVOL5     -0.532020   0.032704 -16.268  < 2e-16 ***
## P_MM         0.206762   0.028859   7.165 8.40e-13 ***
## P_Dom        0.122861   0.022711   5.410 6.47e-08 ***
## P_Trop             NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.224841)
## 
##     Null deviance: 6683.9  on 9335  degrees of freedom
## Residual deviance: 2095.5  on 9320  degrees of freedom
## AIC: 12580
## 
## Number of Fisher Scoring iterations: 2
plot(cvfit, xvar = "lambda", label = TRUE)
## Warning in plot.window(...): "xvar" is not a graphical parameter
## Warning in plot.window(...): "label" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "xvar" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "label" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "xvar" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "label" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "xvar" is not a
## graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "label" is not a
## graphical parameter
## Warning in box(...): "xvar" is not a graphical parameter
## Warning in box(...): "label" is not a graphical parameter
## Warning in title(...): "xvar" is not a graphical parameter
## Warning in title(...): "label" is not a graphical parameter

plot(lasso_v1, xvar = "dev", label = TRUE)

#plot(lasso_v1$glmnet.fit, xvar="lambda", label=TRUE) # There is a term 
#lasso_v1$lambda.min
#lasso_v1$lambda.1se
#coef(lasso_v1, s=cv.lasso$lambda.min)
coef(lasso_v1, s=lasso_v1$lambda.min)
## 17 x 71 sparse Matrix of class "dgCMatrix"
##    [[ suppressing 71 column names 's0', 's1', 's2' ... ]]
##                                                                           
## (Intercept) 9.113095  9.3636565  9.5919585  9.7999789  9.9895192 10.162221
## feat        .         .          .          .          .          .       
## price       .        -0.2424893 -0.4634365 -0.6647553 -0.8481895 -1.015328
## AGE60       .         .          .          .          .          .       
## EDUC        .         .          .          .          .          .       
## ETHNIC      .         .          .          .          .          .       
## INCOME      .         .          .          .          .          .       
## HHLARGE     .         .          .          .          .          .       
## WORKWOM     .         .          .          .          .          .       
## HVAL150     .         .          .          .          .          .       
## SSTRDIST    .         .          .          .          .          .       
## SSTRVOL     .         .          .          .          .          .       
## CPDIST5     .         .          .          .          .          .       
## CPWVOL5     .         .          .          .          .          .       
## P_Dom       .         .          .          .          .          .       
## P_MM        .         .          .          .          .          .       
## P_Trop      .         .          .          .          .          .       
##                                                                                
## (Intercept) 10.319581 10.4438187 10.52920524 10.598464158 10.6455464 10.6884154
## feat         .         0.0209238  0.07039071  0.114879653  0.1541166  0.1899014
## price       -1.167618 -1.2912176 -1.38180665 -1.464394936 -1.5498808 -1.6253536
## AGE60        .         .          .           .            .          .        
## EDUC         .         .          .           .            .          .        
## ETHNIC       .         .          .           .            .          .        
## INCOME       .         .          .           .            .          .        
## HHLARGE      .         .          .           .            .          .        
## WORKWOM      .         .          .           .            .          .        
## HVAL150      .         .          .           0.033189385  0.1263768  0.2112843
## SSTRDIST     .         .          .           .            .          .        
## SSTRVOL      .         .          .           .            .          .        
## CPDIST5      .         .          .           .            .          .        
## CPWVOL5      .         .          .           .            .          .        
## P_Dom        .         .          .           .            .          .        
## P_MM         .         .          .           .            .          .        
## P_Trop       .         .          .          -0.002627611  .          .        
##                                                                           
## (Intercept) 10.7274761 10.7703971457 10.8512702 10.862234685 10.8634444942
## feat         0.2225071  0.2521991085  0.2790193  0.302733492  0.3241982639
## price       -1.6941215 -1.7568665063 -1.8155678 -1.868905229 -1.9252424074
## AGE60        .          .             .          0.211324555  0.4322210396
## EDUC         .          .             .          .            .           
## ETHNIC       .          .             .          .            .           
## INCOME       .          .             .          .            .           
## HHLARGE      .         -0.0521163663 -0.3955450 -0.501861755 -0.5680003552
## WORKWOM      .          .             .          .            .           
## HVAL150      0.2886489  0.3559837608  0.3994133  0.457631866  0.5133662568
## SSTRDIST     .          .             .          .            .           
## SSTRVOL      .          .             .          .            .           
## CPDIST5      .          .             .          .            .           
## CPWVOL5      .          .             .          .            .           
## P_Dom        .          .             .          .            .           
## P_MM         .          .             .          .            .           
## P_Trop       .         -0.0001221817  .         -0.003945815 -0.0003646148
##                                                                    
## (Intercept) 10.8973415361 10.9406370745 10.9791950998 11.0143109284
## feat         0.3436817426  0.3611243888  0.3770009743  0.3914664373
## price       -1.9734939612 -2.0180946512 -2.0593292606 -2.0969189669
## AGE60        0.6118646442  0.7659346798  0.9075682969  1.0366413682
## EDUC         .             .             .             .           
## ETHNIC       .             .             .             .           
## INCOME       .             .             .             .           
## HHLARGE     -0.6841265782 -0.8185312209 -0.9367821813 -1.0444478405
## WORKWOM      .             .             .             .           
## HVAL150      0.5669965833  0.6216868723  0.6719642704  0.7177849925
## SSTRDIST     .             .             .             .           
## SSTRVOL     -0.0053368361 -0.0075416240 -0.0094620485 -0.0112096999
## CPDIST5      .             .             .             .           
## CPWVOL5     -0.0388784844 -0.0999882498 -0.1557268431 -0.2065157405
## P_Dom        .             .             .             .           
## P_MM         .             .             .             .           
## P_Trop      -0.0002528143 -0.0008009768 -0.0007472381 -0.0006812219
##                                                                    
## (Intercept) 11.0463068322 11.0754603015 11.1020238568 11.1262275790
## feat         0.4046468145  0.4166562830  0.4275988635  0.4375693352
## price       -2.1311696906 -2.1623776821 -2.1908132418 -2.2167226647
## AGE60        1.1542483722  1.2614075027  1.3590469115  1.4480123043
## EDUC         .             .             .             .           
## ETHNIC       .             .             .             .           
## INCOME       .             .             .             .           
## HHLARGE     -1.1425471973 -1.2319316463 -1.3133754266 -1.3875839660
## WORKWOM      .             .             .             .           
## HVAL150      0.7595353227  0.7975766724  0.8322385336  0.8638211306
## SSTRDIST     .             .             .             .           
## SSTRVOL     -0.0128020518 -0.0142529426 -0.0155749403 -0.0167794954
## CPDIST5      .             .             .             .           
## CPWVOL5     -0.2527927315 -0.2949586030 -0.3333785747 -0.3683854220
## P_Dom        .             .             .             .           
## P_MM         .             .             .             .           
## P_Trop      -0.0006207113 -0.0005655692 -0.0005153256 -0.0004695455
##                                                                               
## (Intercept) 11.1486714683 11.160758506 11.165655309 11.176423863 11.2486224286
## feat         0.4466778546  0.454531781  0.461423830  0.467709868  0.4734829122
## price       -2.2403248009 -2.261990760 -2.285098674 -2.305487267 -2.3237907457
## AGE60        1.5286161326  1.615033438  1.704338548  1.780191473  1.7739203488
## EDUC         .             .            .            .            .           
## ETHNIC       .             0.027321572  0.075077360  0.118079216  0.1529456727
## INCOME       .             .            .            .            .           
## HHLARGE     -1.4570823575 -1.515792556 -1.568209246 -1.622083356 -1.7589420880
## WORKWOM      .             .            .           -0.013546381 -0.1361551644
## HVAL150      0.8923374274  0.924654118  0.957961690  0.988716788  1.0170123570
## SSTRDIST     .             .            .            .            .           
## SSTRVOL     -0.0179388731 -0.021746326 -0.028025516 -0.033847942 -0.0404600705
## CPDIST5      .             .            .            .            .           
## CPWVOL5     -0.4002164697 -0.419383209 -0.427839318 -0.434859834 -0.4362636377
## P_Dom        .             .            .            .            .           
## P_MM         .             .            .            .            .           
## P_Trop      -0.0003967452 -0.001891497 -0.001096458 -0.001056376 -0.0009323071
##                                                                               
## (Intercept) 11.3148574974 11.373497007 11.4150686289 11.44897422  1.165731e+01
## feat         0.4787682213  0.483327510  0.4867086852  0.48928712  4.916038e-01
## price       -2.3404204707 -2.355644774 -2.3754685088 -2.39361099 -2.410036e+00
## AGE60        1.7675367884  1.760480539  1.7728719117  1.78649037  1.801561e+00
## EDUC         .             0.002981127  0.0748659767  0.13811059  2.192971e-01
## ETHNIC       0.1843194965  0.213038521  0.2379624604  0.26049955  2.676830e-01
## INCOME       .             .            .             .          -1.751407e-02
## HHLARGE     -1.8870904075 -2.003412042 -2.1061171023 -2.19362384 -2.223849e+00
## WORKWOM     -0.2470504829 -0.351004220 -0.4556988489 -0.54723985 -6.316530e-01
## HVAL150      1.0423489056  1.064135879  1.0554242249  1.04833764  1.042858e+00
## SSTRDIST     .             .            .             .           .           
## SSTRVOL     -0.0464789492 -0.052171640 -0.0588458519 -0.06487980 -7.155204e-02
## CPDIST5      .             .            .             .           .           
## CPWVOL5     -0.4377308566 -0.438632884 -0.4383352481 -0.43806239 -4.351136e-01
## P_Dom        .             .            0.0019998538  0.01187433  2.069156e-02
## P_MM         .             0.005269569  0.0225912128  0.03714275  4.993092e-02
## P_Trop      -0.0008135327 -0.001965341 -0.0002098574  .          -9.207667e-05
##                                                                    
## (Intercept)  1.199013e+01  1.228517e+01 12.6202096559 12.9967141694
## feat         4.938124e-01  4.959368e-01  0.4980080642  0.4999311078
## price       -2.424691e+00 -2.437406e+00 -2.4481870288 -2.4578514304
## AGE60        1.853239e+00  1.919592e+00  1.9803534889  2.0425141356
## EDUC         3.311907e-01  4.269284e-01  0.5285200121  0.6275256799
## ETHNIC       2.688311e-01  2.791452e-01  0.2904205046  0.2973114041
## INCOME      -4.948575e-02 -7.890468e-02 -0.1137908121 -0.1537724245
## HHLARGE     -2.175077e+00 -2.110002e+00 -2.0483096488 -1.9626339224
## WORKWOM     -6.812585e-01 -7.060447e-01 -0.7230822137 -0.7300521630
## HVAL150      1.035151e+00  1.033847e+00  1.0365664623  1.0441736485
## SSTRDIST    -9.758202e-04 -2.792227e-03 -0.0045537782 -0.0061786858
## SSTRVOL     -7.665066e-02 -7.953011e-02 -0.0820556840 -0.0843084609
## CPDIST5      .             .             0.0054395009  0.0128226043
## CPWVOL5     -4.376095e-01 -4.456494e-01 -0.4533300325 -0.4601496206
## P_Dom        2.905829e-02  3.703585e-02  0.0445745836  0.0515095088
## P_MM         6.212493e-02  7.395574e-02  0.0855454361  0.0962340765
## P_Trop      -8.974930e-06 -3.144655e-05 -0.0002591993 -0.0005213386
##                                                                   
## (Intercept) 13.321678401 13.6169373957 13.8856011617 14.1331401527
## feat         0.501685143  0.5032834030  0.5047395697  0.5060668619
## price       -2.466980443 -2.4752093795 -2.4827167038 -2.4895941148
## AGE60        2.097322348  2.1468616544  2.1919180028  2.2341084029
## EDUC         0.716870319  0.7976826727  0.8711565455  0.9400555036
## ETHNIC       0.305344057  0.3127612488  0.3195630781  0.3254208861
## INCOME      -0.188262345 -0.2195852767 -0.2480854815 -0.2743793267
## HHLARGE     -1.894323084 -1.8326672547 -1.7766685346 -1.7239771882
## WORKWOM     -0.738000445 -0.7454240550 -0.7522132532 -0.7579757350
## HVAL150      1.050026925  1.0555314583  1.0605866662  1.0646060551
## SSTRDIST    -0.007672848 -0.0090337864 -0.0102740200 -0.0114046140
## SSTRVOL     -0.086335586 -0.0881771783 -0.0898526141 -0.0914021799
## CPDIST5      0.019459596  0.0255046605  0.0310117103  0.0360353442
## CPWVOL5     -0.466528268 -0.4723350686 -0.4776280024 -0.4824571838
## P_Dom        0.057846222  0.0636195333  0.0688799156  0.0736748605
## P_MM         0.106048169  0.1149894751  0.1231367472  0.1305637901
## P_Trop      -0.000396378 -0.0003721749 -0.0003408628 -0.0002716806
##                                                                   
## (Intercept) 14.3558768086 14.558312128 14.7422364587 14.9134867448
## feat         0.5072756906  0.508377028  0.5093804497  0.5102954534
## price       -2.4957947788 -2.501469907 -2.5066460405 -2.5113922015
## AGE60        2.2715920914  2.305576859  2.3364692082  2.3656657437
## EDUC         1.0011849867  1.056554498  1.1068395233  1.1545403224
## ETHNIC       0.3310984129  0.336333938  0.3411627297  0.3451275272
## INCOME      -0.2980124263 -0.319487611 -0.3389993805 -0.3571912397
## HHLARGE     -1.6775812240 -1.635576435 -1.5975345946 -1.5609053639
## WORKWOM     -0.7635885235 -0.768748430 -0.7734606334 -0.7773900311
## HVAL150      1.0687297507  1.072585912  1.0761322199  1.0788612574
## SSTRDIST    -0.0124349256 -0.013373827 -0.0142297795 -0.0150086639
## SSTRVOL     -0.0927937893 -0.094056441 -0.0952027680 -0.0962742031
## CPDIST5      0.0406056368  0.044768878  0.0485606114  0.0520252380
## CPWVOL5     -0.4868574125 -0.490869499 -0.4945320492 -0.4978569263
## P_Dom        0.0780426887  0.082022200  0.0856482774  0.0889531607
## P_MM         0.1373301595  0.143494964  0.1491129649  0.1542311111
## P_Trop      -0.0002767043 -0.000256639 -0.0002331071 -0.0001796895
##                                                                    
## (Intercept) 15.0609994437 15.2031842042 15.3284239716 15.4470835588
## feat         0.5111280114  0.5118877030  0.5125799799  0.5132106604
## price       -2.5156413614 -2.5195917434 -2.5231527872 -2.5264171640
## AGE60        2.3904233101  2.4143583095  2.4357099973  2.4558722379
## EDUC         1.1945505214  1.2336650488  1.2682942445  1.3012859026
## ETHNIC       0.3497104754  0.3530136634  0.3564762450  0.3591444113
## INCOME      -0.3728328614 -0.3879234181 -0.4012229133 -0.4138254918
## HHLARGE     -1.5317805991 -1.5015250539 -1.4758432026 -1.4503450420
## WORKWOM     -0.7815454589 -0.7849298117 -0.7880773182 -0.7907931210
## HVAL150      1.0821541912  1.0846115983  1.0869706401  1.0888459766
## SSTRDIST    -0.0157228649 -0.0163684045 -0.0169605290 -0.0174966893
## SSTRVOL     -0.0971896720 -0.0980759543 -0.0988533321 -0.0995964176
## CPDIST5      0.0551562902  0.0580341408  0.0606405776  0.0630309173
## CPWVOL5     -0.5009501650 -0.5036981504 -0.5062648313 -0.5085455802
## P_Dom        0.0919644135  0.0947071099  0.0972078640  0.0994854797
## P_MM         0.1589031528  0.1631485530  0.1670263819  0.1705525053
## P_Trop      -0.0002071088 -0.0001546598 -0.0001420728 -0.0001127566
##                                                                    
## (Intercept) 15.5503143078  1.564898e+01  1.573379e+01  1.581577e+01
## feat         0.5137863924  5.143098e-01  5.147896e-01  5.152238e-01
## price       -2.5293659899 -2.532071e+00 -2.534519e+00 -2.536762e+00
## AGE60        2.4736321609  2.490368e+00  2.505048e+00  2.518905e+00
## EDUC         1.3300547428  1.357463e+00  1.381200e+00  1.403903e+00
## ETHNIC       0.3620688483  3.642723e-01  3.667546e-01  3.685876e-01
## INCOME      -0.4247943525 -4.352724e-01 -4.442866e-01 -4.529901e-01
## HHLARGE     -1.4292973230 -1.408077e+00 -1.390986e+00 -1.373367e+00
## WORKWOM     -0.7933511443 -7.956222e-01 -7.977132e-01 -7.996270e-01
## HVAL150      1.0907448800  1.092304e+00  1.093872e+00  1.095192e+00
## SSTRDIST    -0.0179891357 -1.843405e-02 -1.884333e-02 -1.921256e-02
## SSTRVOL     -0.1002347542 -1.008538e-01 -1.013736e-01 -1.018890e-01
## CPDIST5      0.0651905204  6.717575e-02  6.896339e-02  7.061231e-02
## CPWVOL5     -0.5107017298 -5.125893e-01 -5.144077e-01 -5.159680e-01
## P_Dom        0.1015625236  1.034533e-01  1.051784e-01  1.067480e-01
## P_MM         0.1737745799  1.767016e-01  1.793781e-01  1.818078e-01
## P_Trop      -0.0001075201 -8.833642e-05 -8.167192e-05 -6.906215e-05
##                                                                    
## (Intercept) 15.8915878237  1.595499e+01  1.601698e+01  1.607474e+01
## feat         0.5156196854  5.159854e-01  5.163134e-01  5.166124e-01
## price       -2.5388116170 -2.540655e+00 -2.542347e+00 -2.543897e+00
## AGE60        2.5319006243  2.543050e+00  2.553550e+00  2.563413e+00
## EDUC         1.4252341670  1.443243e+00  1.460452e+00  1.476671e+00
## ETHNIC       0.3701227702  3.720190e-01  3.734181e-01  3.745463e-01
## INCOME      -0.4610491533 -4.677952e-01 -4.743776e-01 -4.805154e-01
## HHLARGE     -1.3566812965 -1.344023e+00 -1.330700e+00 -1.317911e+00
## WORKWOM     -0.8012359345 -8.027412e-01 -8.041976e-01 -8.054245e-01
## HVAL150      1.0962146721  1.097317e+00  1.098301e+00  1.099070e+00
## SSTRDIST    -0.0195488833 -1.985869e-02 -2.013820e-02 -2.039232e-02
## SSTRVOL     -0.1023668736 -1.027522e-01 -1.031441e-01 -1.035095e-01
## CPDIST5      0.0721175767  7.346348e-02  7.471112e-02  7.585172e-02
## CPWVOL5     -0.5173887479 -5.187953e-01 -5.199714e-01 -5.210365e-01
## P_Dom        0.1081785247  1.094846e-01  1.106719e-01  1.117538e-01
## P_MM         0.1840218620  1.860481e-01  1.878867e-01  1.895609e-01
## P_Trop      -0.0000514194 -5.007404e-05 -4.591394e-05 -3.369295e-05
##                                                                    
## (Intercept)  1.612173e+01  1.616841e+01  1.621241e+01  1.624680e+01
## feat         5.168933e-01  5.171406e-01  5.173661e-01  5.175834e-01
## price       -2.545286e+00 -2.546565e+00 -2.547738e+00 -2.548780e+00
## AGE60        2.571745e+00  2.579649e+00  2.587101e+00  2.593239e+00
## EDUC         1.490100e+00  1.503045e+00  1.515321e+00  1.525161e+00
## ETHNIC       3.760091e-01  3.771045e-01  3.779426e-01  3.790799e-01
## INCOME      -4.855168e-01 -4.904727e-01 -4.951455e-01 -4.988045e-01
## HHLARGE     -1.308759e+00 -1.298788e+00 -1.289005e+00 -1.282587e+00
## WORKWOM     -8.065421e-01 -8.076654e-01 -8.086195e-01 -8.094736e-01
## HVAL150      1.099892e+00  1.100654e+00  1.101256e+00  1.101886e+00
## SSTRDIST    -2.062589e-02 -2.083770e-02 -2.102977e-02 -2.120441e-02
## SSTRVOL     -1.037925e-01 -1.040885e-01 -1.043679e-01 -1.045771e-01
## CPDIST5      7.686156e-02  7.780537e-02  7.867027e-02  7.942291e-02
## CPWVOL5     -5.221256e-01 -5.230141e-01 -5.238093e-01 -5.246465e-01
## P_Dom        1.127425e-01  1.136407e-01  1.144588e-01  1.152076e-01
## P_MM         1.910930e-01  1.924847e-01  1.937505e-01  1.949066e-01
## P_Trop      -3.258248e-05 -3.169255e-05 -2.295239e-05 -2.485327e-05
##                                                                    
## (Intercept) 16.2816298426  1.631503e+01  1.634621e+01 16.3688276262
## feat         0.5177699447  5.179394e-01  5.180938e-01  0.5182506694
## price       -2.5497471114 -2.550636e+00 -2.551446e+00 -2.5521512305
## AGE60        2.5991363611  2.604728e+00  2.610005e+00  2.6141752937
## EDUC         1.5348037722  1.544028e+00  1.552753e+00  1.5593354532
## ETHNIC       0.3799701021  3.806160e-01  3.811191e-01  0.3819074572
## INCOME      -0.5025019027 -5.060467e-01 -5.093592e-01 -0.5117671572
## HHLARGE     -1.2752834201 -1.267867e+00 -1.260719e+00 -1.2566130681
## WORKWOM     -0.8103509916 -8.111122e-01 -8.117416e-01 -0.8123433814
## HVAL150      1.1024937020  1.102988e+00  1.103361e+00  1.1037438639
## SSTRDIST    -0.0213652851 -2.151073e-02 -2.164298e-02 -0.0217597718
## SSTRVOL     -0.1047973837 -1.050102e-01 -1.052089e-01 -0.1053619950
## CPDIST5      0.0801359886  8.079229e-02  8.139255e-02  0.0818968018
## CPWVOL5     -0.5253241112 -5.259170e-01 -5.264537e-01 -0.5270167756
## P_Dom        0.1158871038  1.165056e-01  1.170692e-01  0.1175837818
## P_MM         0.1959607971  1.969183e-01  1.977901e-01  0.1985833188
## P_Trop      -0.0000247842 -1.846476e-05 -1.201702e-05 -0.0000186862
##                                                                    
## (Intercept)  1.639844e+01  1.641180e+01  1.643490e+01  1.644905e+01
## feat         5.183638e-01  5.185106e-01  5.186044e-01  5.187169e-01
## price       -2.552846e+00 -2.553388e+00 -2.553980e+00 -2.554451e+00
## AGE60        2.618941e+00  2.621724e+00  2.625286e+00  2.627899e+00
## EDUC         1.567467e+00  1.571209e+00  1.577604e+00  1.581407e+00
## ETHNIC       3.821948e-01  3.830722e-01  3.835637e-01  3.841904e-01
## INCOME      -5.149091e-01 -5.163267e-01 -5.187766e-01 -5.202743e-01
## HHLARGE     -1.249228e+00 -1.247940e+00 -1.242796e+00 -1.240542e+00
## WORKWOM     -8.128909e-01 -8.135300e-01 -8.140436e-01 -8.146006e-01
## HVAL150      1.104057e+00  1.104511e+00  1.104884e+00  1.105280e+00
## SSTRDIST    -2.187456e-02 -2.195903e-02 -2.206326e-02 -2.213763e-02
## SSTRVOL     -1.055415e-01 -1.056759e-01 -1.057813e-01 -1.059035e-01
## CPDIST5      8.242900e-02  8.279338e-02  8.324512e-02  8.357585e-02
## CPWVOL5     -5.274176e-01 -5.278571e-01 -5.282899e-01 -5.286192e-01
## P_Dom        1.180516e-01  1.184842e-01  1.188679e-01  1.192270e-01
## P_MM         1.993123e-01  1.999527e-01  2.005743e-01  2.011089e-01
## P_Trop      -9.897414e-06 -4.332179e-05 -1.586943e-05 -3.519192e-05
# The key this here is that the week variable is formatted as a date variable.  This provides R with some information that it is a panel dataset


 #create a date and sequence accompanying the dates within the dataframe then use lag operaters to make progress on it.

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.