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

Montag, 29. Oktober 2012

XSLT script to transform xml tree to table for import into sql database

This XSLT script transforms a Tree from freemind to a CSV file in the form of
PARENT_ID, CHILD_ID, CHILD Text

I need this to get it into a postgresql database.



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" encoding="UTF-8"/>
 <xsl:template match="/">
 <xsl:apply-templates/>
 </xsl:template>
  <xsl:template name="linebreak">
   <xsl:text>
</xsl:text>
  </xsl:template>
  <xsl:template match="map">
   <xsl:apply-templates select="child::node"/>
  </xsl:template>
 
  <xsl:template match="node">
   <xsl:param name="commaCount">0</xsl:param>
  
  
    <xsl:if test="$commaCount > 0">
      <xsl:call-template name="writeCommas">
       <xsl:with-param name="commaCount" select="$commaCount"/>
      </xsl:call-template>
    </xsl:if>
   
   <xsl:value-of select="../@ID"/>,<xsl:value-of select="@ID"/>,<xsl:value-of select="@TEXT"/>
   
     <xsl:call-template name="linebreak"/>
      <xsl:apply-templates select="child::node">
        <xsl:with-param name="commaCount" select="$commaCount + 1"/>
      </xsl:apply-templates>
     </xsl:template>
     <xsl:template name="writeCommas">
      <xsl:param name="commaCount">0</xsl:param>
       <xsl:if test="$commaCount > 0"> <xsl:call-template name="writeCommas">
         <xsl:with-param name="commaCount" select="$commaCount - 1"/>
     </xsl:call-template>
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>

          

Dienstag, 23. Oktober 2012

household savings is not household savings

 

http://epp.eurostat.ec.europa.eu/tgm/table.do?tab=table&init=1&plugin=1&language=en&pcode=tsdec240

is the household savings rate calculated by eurostat which is defined as:

“(ESA95 code: B8G) divided by gross disposable income (B6G), with the latter being adjusted for the change in the net equity of households in pension funds reserves (D8net)”

I have documented the differences here:http://rpubs.com/johanneskutsam/savingsrate

Texreg and discourse analysis

Phillip Leifeld has developed two useful tools: texreg – a tool to print latex tables from different types  regressions in R and a software for discourse analysis that looks useful for my political science amigos.

Mittwoch, 10. Oktober 2012

multiple regressions in one table

 

You often want to compare coefficients between different models. mtable from the memisc package does exactly that. As its author (Martin Elff) just pointed out to me it is possible to use mtable of the number of regression results is variable. See the following example:

library(memisc)
x=rnorm(10)
y=rnorm(10)
z=rnorm(10)
l1=lm(x~z)
l2=lm(z~y)
li=list(l1,l2)
attributes(li)=list(names=list(1,2))
do.call(mtable,li)

In a future version of mtable Martin said there will be a functionality to include lists directly

mtable(list=list("1"=l1,"2"=l2))

Thanks Maritn for the great tool!