README.md

shrinkest

A method for Bayesian Shrinkage Estimator on panel data

Installation

You can install shrinkest from github using the devtools package

require(devtools)
install_github('xinzhou1023/shrinkest')

Example

library(shrinkest)

# load datasets
data(test_data) # modeling dataset
data(data_sign) # sign check data format, 1 is pos, 0 is neg, 2 is no preference; is.var=1 means coef varies by market, 0 is not;
data(var_group) # variable group table
data(date_group) # date group table

# convert date format
data=data.table(test_data)
data$date=as.Date(data$date, "%m/%d/%Y") # converte date var to date format
var.group=data.table(var.group)
date.group=data.table(date.group)

# mothed remove
test.restult=lm_remove(
  wols=T,
  dep="chkopens_ext_adji",# dependent in model
  actual="chkopens_ext_adj",#actual dependent
  w="chkopens_ext_adjm",#weight
  date.var="date",#date variable name in your dataset
  date.start="2010-01-26",#don't use other date format
  date.end="2014-12-26",#don't use other date format
  group="market",#cross section variable name in your dataset
  data=data,#dataset for modeling, must be data.table class
  is.int=F, #including intercept or not
  # fixed variable list
  var.fix=c(
    #"dXOther",
    "bransat2",
    #"dbosepwk1",
    #"dbosepwk2",
    "dsepwk1",
    "Seasonality_mthly3_V2",
    "holiday_dda",
    "d15feb10_wasbal",
    "xhsalesmkt",
    "bawboc_attxxi",
    "comptotck",
    "hckttvxx10i",
    "hcktinxx90i",
    "hcktexpxx20i",
    "hcktposxx10i",
    "hcktraxx40i",
    "hhettoxx90i",
    "hhlttoxx80i",
    "hmgttoxx40i",
    "hsvttoxx10i",
    "ckgenttoxx97i",
    "himttoxx10i"
  ),
  # test variable list
  var.test = c(
    "hmucsrttvxxTRPV2ZADBhl4hrf15i",
    "hmucsrtprxxV2ZADBhl0.3hrf45i",
    "hmucsrtinxxV2ZADBhl3hrf60i",
    "hmucsrtothxxV2ZADBhl4hrf15i",
    "hmucbGenttoxxZADBhl0.3hrf15i",
    "hmucbSolunttvxxTRPZADBhl4hrf15i",
    "hmucbSolutsrdpxxZADBhl0.3hrf15i_V2",
    "hmucbSolutothxxZADBhl0.3hrf60i",
    "hmucbASavttoxxZADBhl0.3hrf15i",
    "hmucbAmerttvxxTRPZADBhl0.3hrf15i",
    "hmucbAmertdpxxZADBhl0.3hrf15i",
    "hmucbAmertsrxxZADBhl0.3hrf15i"
  ),
  is.multithread = F, #whether do multi-thread computation
  spec=rep("localhost",4) # device spec for multi-thread
)

test.restult$t.test # testing result

# check fit detail of one model from testing above
test.restult$fit[[2]]$coef # model coef

a=post_calc(data=data,# same dataset for modeling
            fit=test.restult$fit[[2]], # one testing model
            var.group=var.group, # variable group table
            date.group=date.group, # date group table
            date.start="2010-01-26", 
            date.end="2014-12-26",
            group=c("group2"), # dimension column names for aggregation
            is.output=F, # whether output decomp and contribution files
            is.graph = T # whether show AVP
)
runApp(a$app) # open browser
a$R2
a$mape

# pooled lm
fit=lm_pool(
  wols=F,
  dep="chkopens_ext_adji",
  actual="chkopens_ext_adj",
  w="chkopens_ext_adjm",
  ind=c(
    #"dXOther",
    "bransat2",
    #"dbosepwk1",
    #"dbosepwk2",
    "dsepwk1",
    "Seasonality_mthly3_V2",
    "holiday_dda",
    #"d15feb10_wasbal",
    "xhsalesmkt",
    "bawboc_attxxi",
    "comptotck",
    "hckttvxx10i",
    "hcktinxx90i",
    "hcktexpxx20i",
    "hcktposxx10i",
    "hcktraxx40i",
    "hhettoxx90i",
    "hhlttoxx80i",
    "hmgttoxx40i",
    "hsvttoxx10i",
    "ckgenttoxx97i",
    "himttoxx10i",
    "hmucsrttvxxTRPV2ZADBhl4hrf15i",
    "hmucsrtprxxV2ZADBhl0.3hrf45i",
    "hmucsrtinxxV2ZADBhl3hrf60i",
    "hmucsrtothxxV2ZADBhl4hrf15i",
    "hmucbGenttoxxZADBhl0.3hrf15i",
    "hmucbSolunttvxxTRPZADBhl4hrf15i",
    "hmucbSolutsrdpxxZADBhl0.3hrf15i_V2",
    "hmucbSolutothxxZADBhl0.3hrf60i",
    "hmucbASavttoxxZADBhl0.3hrf15i",
    "hmucbAmerttvxxTRPZADBhl0.3hrf15i",
    "hmucbAmertdpxxZADBhl0.3hrf15i",
    "hmucbAmertsrxxZADBhl0.3hrf15i",
    "Card_DM_365i",
    "Checking_Promotion",
    #"hurricane_sandy",
    "Facebook_Paid_ImpressionZADBhl2hrf45"
  ),
  date.var="date",
  date.start="2010-01-26",
  date.end="2014-12-26",
  group="market",
  data=data,
  is.int=F #including intercept or not
)

# check fit detail of one model from testing above
fit$coef # model coef

a=post_calc(data=data,# same dataset for modeling
            fit=fit, # one testing model
            var.group=var.group, # variable group table
            date.group=date.group, # date group table
            date.start="2010-01-26", 
            date.end="2014-12-26",
            group=c("group2"), # dimension column names for aggregation
            is.output=F, # whether output decomp and contribution files
            is.graph = T # whether show AVP
)
runApp(a$app) # open browser
a$R2
a$mape

# lm loop
fit.loop=lm_loop(
  wols=F,
  dep="chkopens_ext_adji",
  actual="chkopens_ext_adj",
  w="chkopens_ext_adjm",
  ind=c(
    #"dXOther",
    "bransat2",
    #"dbosepwk1",
    #"dbosepwk2",
    "dsepwk1",
    "Seasonality_mthly3_V2",
    "holiday_dda",
    #"d15feb10_wasbal",
    "xhsalesmkt",
    "bawboc_attxxi",
    "comptotck",
    "hckttvxx10i",
    "hcktinxx90i",
    "hcktexpxx20i",
    "hcktposxx10i",
    "hcktraxx40i",
    "hhettoxx90i",
    "hhlttoxx80i",
    "hmgttoxx40i",
    "hsvttoxx10i",
    "ckgenttoxx97i",
    "himttoxx10i",
    "hmucsrttvxxTRPV2ZADBhl4hrf15i",
    "hmucsrtprxxV2ZADBhl0.3hrf45i",
    "hmucsrtinxxV2ZADBhl3hrf60i",
    "hmucsrtothxxV2ZADBhl4hrf15i",
    "hmucbGenttoxxZADBhl0.3hrf15i",
    "hmucbSolunttvxxTRPZADBhl4hrf15i",
    "hmucbSolutsrdpxxZADBhl0.3hrf15i_V2",
    "hmucbSolutothxxZADBhl0.3hrf60i",
    "hmucbASavttoxxZADBhl0.3hrf15i",
    "hmucbAmerttvxxTRPZADBhl0.3hrf15i",
    "hmucbAmertdpxxZADBhl0.3hrf15i",
    "hmucbAmertsrxxZADBhl0.3hrf15i",
    "Card_DM_365i",
    "Checking_Promotion",
    #"hurricane_sandy",
    "Facebook_Paid_ImpressionZADBhl2hrf45"
  ),
  date.var="date",
  date.start="2010-01-26",
  date.end="2014-12-26",
  group="market",
  data=data,
  is.int=F, #including intercept or not
  is.multithread=F,
  spec=rep("localhost",4)
)

fit.loop$coef # coef by market

# coef shrinkage
coef.sh=baysian_sh(
  lm.loop=fit.loop,#result from lm_loop
  lm.pool=fit,#result from lm_pool
  is.signcheck=T,#whether do sign check
  data.sign=data.sign,#sign check dataset
  coef.lower=0.5, # upper bound of coef: coef=pool-abs(pool*factor)
  coef.upper=0.5 # lower bound of coef: coef=pool+abs(pool*factor)
)
coef.final=coef.sh$coef

# final decomp after shrinkage
final=post_calc_sh(
  data=data, # this dataset could be different to the modeling one, but same format
  shrink=coef.sh,#result from baysian_sh
  var.group=var.group, # variable group table
  date.group=date.group, # date group table
  date.start="2010-01-26", 
  date.end="2014-12-26",
  group=c("group2"), # dimension column names for aggregation
  is.output=F, # whether output decomp and contribution files
  is.graph = T # whether show AVP
)
runApp(final$app)
final$R2
final$mape


xinzhou1023/shrinkest documentation built on May 4, 2019, 1:07 p.m.