Donnerstag, 31. Oktober 2013

Will Small Retailers Survive? - A Demand Analysis using Multiple Stages

 

I have just updated the draft of my second thesis. Feedback is welcome.

Abstract. Will price competition with efficient big retailers drive out small apparel retailers? To find out I estimate price elasticities of in-store demand for 91 stores of 22 independent small fashion retailers in Austria. Demand is less price elastic than I expect from the level at which prices are set. For the majority of stores an increase in price seems to be profitible. I also estimate demand for 19 apparel categories. A price increase in any category has negative cross effects on almost all categories. The optimal category price is constraint by its effect on other categories.

https://dl.dropboxusercontent.com/u/19155359/DA2.pdf

Mittwoch, 23. Oktober 2013

Scatterplot with histograms in R

What I want

What I type in R

What I get (and source)

I often want to visualizing 2 dimensional results.

require(stats)

plot(cars)

image

Scatter plots are nice, but it is nicer with histograms

library(psych)

data(sat.act)

scatter.hist(sat.act[5:6],pch=(19+sat.act$gender),col=c("blue","red")[sat.act$gender])

image

http://www.personality-project.org/r/psych/help/scatter.hist.html

R has serveral implementation sof that scatterplot /histogram combination

library(ade4)

data(rpjdl)

coa1 <- dudi.coa(rpjdl$fau, scannf = FALSE, nf = 4)

s.hist(coa1$li)

s.hist(coa1$li, cgrid = 2, cbr = 3, adj = 0.5, clab = 0)

s.hist(coa1$co, cgrid = 2, cbr = 3, adj = 0.5, clab = 0)

image

http://pbil.univ-lyon1.fr/ade4/ade4-html/s.hist.html

Or this implementation

http://www.r-bloggers.com/example-10-3-enhanced-scatterplot-with-marginal-histograms/

graph copied from http://www.r-bloggers.com/example-10-3-enhanced-scatterplot-with-marginal-histograms/


 

I have not yet made up my mind which to use…

Dienstag, 10. September 2013

Estimating consumer demand for Austria

 

I just finished my first thesis. You can download it here: https://dl.dropboxusercontent.com/u/19155359/DA1.pdf 

 

Estimating consumer demand for Austria

Abstract Prices and income impact consumption patterns. I estimate an Almost Ideal Demand System (AIDS) for 12 broadly defined consumer goods (COICOP) for the countries Austria, Finland, Denmark, Norway, and the Netherlands. Focusing on Austria, I find expenditure elasticities vary between 0 and 3, which shows how diverse the effects of income on consumption are. Own price elasticities vary between -2.6 and 0, revealing the different opportunities of consumers to substitute goods when prices change. I also find that a simple log-log model yields similar own price elasticities than the AIDS model. Whenever only own price elasticities and expenditure elasticities are of interest a simple log-log model is an easier to estimate alternative. The AIDS model gives additional insights into cross price elasticities and allows to evaluate how elasticities have changed over time.

Freitag, 2. August 2013

Political competition and macroeconomic performance–What does the data say?

 

This paper summarizes empirical evidence from public choice theories and presents new evidence on the influence of unemployment and partisan bias in Austria. Download the paper from here http://sdrv.ms/18WvKgN 

The dataset comes from the European Election Database and Eurostat.

Using the eurostat_r library (get it here: https://github.com/toprach/eurostat_r)  and the following r script you can replicate the table in the paper.

The data sources allow for easy repetition of the analysis for other countries.

 

library(foreign)
library(data.table)
library(texreg)
library(estout)
library(stargazer)
library(plm)

#http://129.177.90.166/nesstar/temp/download69627.tmp/BEEP2009_Download.zip
#http://129.177.90.166/nesstar/temp/download69627.tmp/BEEP2004_Download.zip

election=data.table(read.csv("election.csv"))
setnames(election,"NUTSID","GEO")
setnames(election,"Year","time")
        
election[,SPO:=SPO/Validvotes*100]
election[,OVP:=OVP/Validvotes*100]
election[,FPO:=FPO/Validvotes*100]
election[,BZO:=BZO/Validvotes*100]
election[,Grune:=Grune/Validvotes*100]
election[,other:=100-(SPO+OVP+FPO+BZO+Grune)]
election[,KPO:=KPO/Validvotes*100]
election[,left:=SPO+Grune]
election[,right:=OVP+FPO+BZO]

election[time==1999 | time==2002, BZO:=NA]

source("C:/git/eurostat_r/eurostat_r.r")
unemp=read.eurostat("lfst_r_lfu3rt")
bip=read.eurostat("nama_r_e3gdp")
setnames(unemp,"value","unemp")
setnames(bip,"value","gdp")
unemp$flag=NULL

bip=bip[UNIT=="EUR_HAB_EU",list(GEO,time,gdp)]

j=join(election,unemp)
j=join(j,bip)
j=j[SEX=="T" & AGE=="Y_GE15",]
level2=j[NUTSlevel==2,]
level1=j[NUTSlevel==1,]

j=j[ NUTSlevel==3,]
j$GEO=as.factor(as.character(j$GEO)) # remove unused factors of level 1,2

table(j[,list(time,GEO)])

plmdata=plm.data(j, index=c("GEO","time"), model="twoways")

p_s=plm(SPO ~ unemp ,data=plmdata)
p_o=plm(OVP ~ unemp ,data=plmdata)
p_f=plm(FPO ~ unemp ,data=plmdata)
p_b=plm(BZO ~ unemp ,data=plmdata)

p_g=plm(Grune ~ unemp ,data=plmdata)
p_l=plm(left ~ unemp ,data=plmdata)
p_r=plm(right ~ unemp ,data=plmdata)


sink("unempvotesplm.tex")
s=stargazer(p_s,p_g,p_o,p_f,p_l,omit=levels(j$GEO),
            title="How unemployment influences elections",
            intercept.top=TRUE,covariate.labels="unemployment")

sink()


p_s=plm(SPO ~ gdp ,data=plmdata)
p_o=plm(OVP ~ gdp ,data=plmdata)

 

#the level of gdp does not change the votes
summary(lm(SPO ~ gdp + GEO ,j))
summary(lm(OVP ~ log(gdp) + GEO ,j))
summary(lm(FPO ~ log(gdp) + GEO ,j)) # when unemp high is then FPO gets - other things equal -less votes


######################## graph for median voters

windows(width=7,height=4)
x=seq(-2,2,length=200)
y=dnorm(x)
plot(x,y,type="l", lwd=2, col="blue", axes=F,xlab="",ylab="",
      main="Distribution of voter preferences\n and party positions")
x=seq(-1,1,length=100)
y=dnorm(x)

polygon(c(-1,x,1),c(0,y,0),col="gray")
axis(1,labels=F)
axis(1,at=c(-1,0,1),labels=c("Left","Median","Right"))
savePlot(filename="distrvoters.png",type="png")
dev.off()
######################################################################
##election denmark

fetchfile=function(x){
  data.table(read.csv(x))
}
files=c("DKPA1994_Download_F1.csv",
        "DKPA1998_Download_F1.csv",
        "DKPA2001_Download_F1.csv",
        "DKPA2005_Download_F1.csv",
        "DKPA2007_Download_F1.csv")
library(plyr)
election=data.table(ldply(files,fetchfile))
unemp=read.eurostat("lfst_r_lfu3rt")

setnames(election,"NUTSID","GEO")
setnames(election,"Year","time")

j=join(election,unemp)
j=j[SEX=="T" & AGE=="Y_GE15",]
j=j[GEO!="DK" & GEO!="DK0" ,]
j=j[ NUTSlevel==3,]
j$GEO=as.factor(as.character(j$GEO)) # remove unused factors of level 1,2

Dienstag, 2. Juli 2013

Estimating consumer demand for Austria

 

I have just finished two drafts of my thesis, which I’d be happy to get some feedback on

Estimating consumer demand for Austria

Abstract Price and income impact consumption patterns. An Almost Ideal Demand System is estimated for Austria to find expenditure, own and cross price elasticities for 12 broadly defined consumption groups. Results are compared to results from Finland, Denkmark, Norway and the Netherlands. A simple log-linear model yields similar own price elasticities for most categories, except for Clothing and Furnishings which show significant cross elasticities. The micEconAids package for R was extended to facilitate estimation of AIDS in first difference and calculate standard errors of elasticities with respect demand shifters.

https://dl.dropboxusercontent.com/u/19155359/thesis1ConsumerDemandAnalaysis_JohannesKutsam_draft20130702.pdf

Dienstag, 14. Mai 2013

holiday dataset for R

 

I work with sales datasets from retail companies. It is important for me to know if a day was a holiday, and to estimate the effect of that holiday on sales.

But also the days before and after a holiday may be informative, that is why I wrote this little routine: It generates an R dataset for the specified years with all the holidays in Austria. It can be easily adjusted to other nations by adopting the “national” vector.

Demetra+ Manual http://www.cros-portal.eu/sites/default/files//Demetra%2B%20User%20Manual%20November%202012.pdf  was the source for the holidays. Demetra+ is a free software for seasonal adjustment, but its finest aggregate is the month – which is to coarse for my use case.

 

makeholidaydataset=function(daysbefore=30,daysafter=7,years=2001:2013){
  #this function generates Austrian holidays dataset usable for regressions; adjust the fixed holidays in fh
  # to adapt for other countries add line to national
 
  library(data.table)
  library(foreign)
 
  #fixed holidays
 
  fh=matrix(c(
  "NewYear",1,1,
  "MayDay" ,5,1,
  "Halloween",10,31,
  "AllSaintsDay",11,1,
  "Christmas",12,25),ncol=3,byrow=T)
  national=matrix(c(
    "Nationalholiday",10,26,
    "StMary",8,15,
    "MariaEmpfaegnis",12,8),ncol=3,byrow=T)
 
  fixedholidays=rbind(fh,national)
  fixedholidays=data.table(holiday=fixedholidays[,1],month=as.integer(fixedholidays[,2]),day=as.integer(fixedholidays[,3]))
 
  library(plyr)
  gendates=function(year){
    dates=as.IDate(ISOdate(year=year,month=fixedholidays$month, day=fixedholidays$day))
    data.table(date=dates,holiday=fixedholidays$holiday)
  }
  DTholidays=data.table(ldply(years,gendates))
   
 
  #variable holidays: easter related
  #easter=read.csv(file="c:/git/da/data/holiday/easter.csv",row.names=NULL)
 
  easter=c("1990-04-15","1991-03-31","1992-04-19","1993-04-11","1994-04-03","1995-04-16","1996-04-07","1997-03-30","1998-04-12","1999-04-04",
           "2000-04-23","2001-04-15","2002-03-31","2003-04-20","2004-04-11","2005-03-27","2006-04-16","2007-04-08",
           "2008-03-23","2009-04-12","2010-04-04","2011-04-24","2012-04-08","2013-03-31","2014-04-20","2015-04-05",
           "2016-03-27","2017-04-16","2018-04-01","2019-04-21")
  easterdates=as.IDate(easter)
  eastermonday=easterdates+1
  ashwednesday=easterdates-46
  ascensionday=easterdates+40
  pentecost=easterdates+50
  whitmonday=pentecost+1
  maundythursday=easterdates-3
  goodfriday=easterdates-2
 
variableholidays= rbind(
    data.table(holiday="eastersunday",date=easterdates),
  data.table(holiday="eastermonday",date=eastermonday),
  data.table(holiday="ashwednesday",date=ashwednesday),
  data.table(holiday="ascensionday",date=ascensionday),
  data.table(holiday="pentecost",date=pentecost),
  data.table(holiday="whitmonday",date=whitmonday),
  data.table(holiday="maundythursday",date=maundythursday),
  data.table(holiday="goodfriday",date=goodfriday)
  )
 
  allholidays=rbind(DTholidays,variableholidays,use.names=T)
  allholidays$value=1
 
  #generate before and after window
  library(reshape2)
 
  A=acast(allholidays,date~holiday,fun.aggregate=sum)
  DATES=as.IDate(rownames(A))
  offsets=-daysbefore:daysafter
 
  returnoffseteddswithdate=function(offset){
    d=DATES+offset
    class(d)=c("IDate" ,"Date" )
   
    B=copy(A)
    rownames(B)=as.character(d)
    B[B==1]=offset
    B=as.data.frame(B)
    B$date=as.IDate(rownames(B))
    B
  }
  all=data.table(ldply(offsets,returnoffseteddswithdate))
  m=melt(all,id.vars="date")
  m=data.table(m)
  m=m[,list(value=sum(value)),by=list(date,variable)]
  all=data.table(dcast(m,date~variable,fun.aggregate=sum))
  all=all[order(date)]
  all
}

DTholiday=makeholidaydataset()
save(DTholiday,file="holidays.rdata")

easter=DTholiday[eastersunday!=0,list(date,eastersunday)]
setkey(easter,date)
DTisholiday=makeholidaydataset(daysbefore=0,daysafter=0)[,list(date,isholiday=1)]
setkey(DTisholiday,date)
myholidays=merge(DTisholiday,easter,all=T)

Dienstag, 23. April 2013

Connect your Windows 7 phone to your Cisco VPN

 

You want to connect your Windows 7 phone with your Cisco VPN. Maybe you want to sync our Mail with a server that lies on the other side of a firewalll. maybe you want to access a local web server. There is no Cisco Software I know of, that will connect your Windows phone directly via VPN, but you can use the shared network and wireless hostednetwork features from Windows 7.

Here is how you do it:

1. follow  the instructions here:

http://winsupersite.com/article/faqtip/windows-7-tip-of-the-week-use-wireless-hosted-networking-to-share-an-internet-connection-wirelessly

then copy the code below into a .vbs file and run it. You probably have to rename your connections in con and con2.

 

'this script: runs itself with elvated rights; starts the internet connection sharing feature of windows with the two configured networks (public and VPN)
' to allow your WINDOWS 7 PHONE to finally enter your cisco vpn

'Originally from http://www.autoitscript.com/forum/topic/28897-switch-ics/
'Changed to VBS, added arguments and fixed for private/public networkds by Dror Gluska 2012-06-25
'Dror Gluska (2012) - http://uhurumkate.blogspot.co.il/
'http://www.winhelponline.com/articles/185/1/VBScripts-and-UAC-elevation.html


option explicit


dim con
dim con2
dim objShell

con="Cisco AnyConnect VPN Client Connection"
con2="Drahtlosnetzwerkverbindung 2"
Set objShell = CreateObject("Shell.Application")
 
dim wsh
set wsh  = WScript.CreateObject("WScript.shell")

If WScript.Arguments.length =0 Then
  'Pass a bogus argument with leading blank space, say [ uac]
  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
    'reset internet sharing to allow cisco vpn to connect, needed if the last attempt failed
    wsh.run "cmd /c netsh wlan stop hostednetwork" 
    EnableDisableICS con,con2, false
   
    'connect with anyconnect now
    objShell.ShellExecute "C:\Program Files (x86)\Cisco\Cisco AnyConnect VPN Client\vpnui.exe"
    wsh.run "cmd /c netsh wlan start hostednetwork" 
    MsgBox "Click AFTER VPN was established"
    'now enable internet sharing
    EnableDisableICS con,con2, true
   
    dim i
    dim e
    For i=1 To 100
        e=InputBox("write 'exit'    to disable internet sharing")
        If e="exit" Then Exit For
    Next
    'cleanup
    EnableDisableICS con,con2, false
    wsh.run "cmd /c netsh wlan stop hostednetwork" 
End If

function EnableDisableICS(sPublicConnectionName, sPrivateConnectionName, bEnable)
   
    dim  bFound
    bFound = FALSE
    dim oNetSharingManager, oConnectionCollection, oItem, EveryConnection, objNCProps
   
    set oNetSharingManager = Wscript.CreateObject("HNetCfg.HNetShare.1")
     if (IsObject(oNetSharingManager)) = FALSE then
        Wscript.Echo("Unable to get the HNetCfg.HnetShare.1 object.")
        Exit function
    End if
   
    if (IsNull(oNetSharingManager.SharingInstalled) = TRUE) then
        Wscript.Echo( "Sharing is not available on this platform.")
        Exit function
    End if

   

     set oConnectionCollection = oNetSharingManager.EnumEveryConnection
    for each oItem In oConnectionCollection
        set EveryConnection = oNetSharingManager.INetSharingConfigurationForINetConnection (oItem)
        set objNCProps = oNetSharingManager.NetConnectionProps (oItem)
         If  objNCProps.name = sPrivateConnectionName Then
            bFound = True
           'Wscript.Echo("Setting ICS Private to " & bEnable & " on connection: " & objNCProps.name)
            If bEnable Then
                EveryConnection.EnableSharing (1)
            Else
                EveryConnection.DisableSharing
            End if
        End if
    Next

    set oConnectionCollection = oNetSharingManager.EnumEveryConnection
    for each oItem In oConnectionCollection
        set EveryConnection = oNetSharingManager.INetSharingConfigurationForINetConnection (oItem)
        set objNCProps = oNetSharingManager.NetConnectionProps (oItem)
       
        If  objNCProps.name = sPublicConnectionName Then
            bFound = True
           'Wscript.Echo("Setting ICS Public to " & bEnable & " on connection: " & objNCProps.name)
            If bEnable Then
                EveryConnection.EnableSharing (0)
            Else
                EveryConnection.DisableSharing
            End if
        End if
    next
   
    If Not bFound Then
       Wscript.Echo("Unable to find the connection " & sPublicConnectionName)
    End if

end function