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)
## Warning: package 'dplyr' was built under R version 3.3.3
## 
## 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)
## Warning: package 'reshape2' was built under R version 3.3.3
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"

##########
# Here is some extra code from a previous assignment which might be useful.  It plotting the relationship between quantity and price and showing one way (actually incorrect) to also think about the standard errors of the estimated model.  If you want to know how to handle functions of parameter estimates, check out the "delta method".  I hope you find it useful. 

lagged\(Q <-exp(lagged\)logmove) reglag = glm(Q ~ price*brand + feat + lagged_price, data=lagged) summary(reglag)

int<-coef(reglag)[“(Intercept)”]/coef(reglag)[“price”] slope <- 1/coef(reglag)[“price”] Quant <-c(1:coef(reglag)[“(Intercept)”]) inv_demand <- 1/coef(reglag)[“price”]*Quant - int plot(Quant,inv_demand, type = “l”)

inv_demand_lagged1 <- 1/coef(reglag)[“price”]Quant - ((coef(reglag)[“(Intercept)”]+coef(reglag)[“lagged_price”])/coef(reglag)[“price”]) inv_demand_lagged3 <- 1/coef(reglag)[“price”]Quant - ((coef(reglag)[“(Intercept)”]+3*coef(reglag)[“lagged_price”])/coef(reglag)[“price”])

Standard error on price coefficient: Upper bound is 1.96 times this plus and minus

vcov(reglag)[2,2]^(.5)

Lower_CI <- 1/((-1.96(vcov(reglag)[2,2](.5)))+coef(reglag)[“price”])Quant - int Upper_CI <- 1/((1.96(vcov(reglag)[2,2](.5)))+coef(reglag)[“price”])Quant - int plot_df <- data.frame(Quant,inv_demand,inv_demand_lagged1,inv_demand_lagged3,Lower_CI,Upper_CI)

d <- melt(plot_df, id.vars=“Quant”) ggplot(d, aes(Quant,value, col=variable)) + geom_line() + stat_smooth() #Higher lagged prices imply higher demand in the current period.
##################

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.
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")
mean(MSEs)
## [1] 0.4150361
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")
mean(MSEs)
## [1] 0.4493511
#We see that this simpler model has a HIGHER out of sample MSE.  Not as good at prediction. 

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

cvfit$lambda.min
## [1] 0.0007455889
log(cvfit$lambda.min)
## [1] -7.201336
coef(cvfit, s = "lambda.min")
## 22 x 1 sparse Matrix of class "dgCMatrix"
##                                       1
## (Intercept)                 12.96802895
## (Intercept)                  .         
## log(price)                  -3.23105939
## feat                         0.83033707
## brandminute.maid             0.48692400
## brandtropicana               0.58488008
## AGE60                        1.93268012
## EDUC                        -1.58086944
## ETHNIC                       0.63330449
## INCOME                      -0.26309920
## HHLARGE                      3.44096555
## WORKWOM                     -0.44854951
## HVAL150                      0.30907916
## SSTRDIST                    -0.02020211
## SSTRVOL                     -0.05358492
## CPDIST5                      0.06273134
## CPWVOL5                     -0.49637185
## log(lagged_price)            0.54381876
## log(price):brandminute.maid  0.25215856
## log(price):brandtropicana    0.70567211
## log(price):EDUC              3.26435632
## log(price):HHLARGE          -5.60671487
#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.
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") 
regcross = glm(logmove ~ log(P_Dom)*feat+log(P_MM)*feat+log(P_Trop)*feat, data=trop_cross)
summary(regcross)
## 
## 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") 
regcross = glm(logmove ~ log(P_Dom)*feat+log(P_MM)*feat+log(P_Trop)*feat, data=MM_cross)
summary(regcross)
## 
## 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") 
regcross = glm(logmove ~ log(P_Dom)*feat+log(P_MM)*feat+log(P_Trop)*feat, data=dom_cross)
summary(regcross)
## 
## 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
# 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
#


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