+ All Categories
Home > Documents > Tutorial7 Dummy Variables

Tutorial7 Dummy Variables

Date post: 02-Jun-2018
Category:
Upload: rosa-maria-vega-torres
View: 238 times
Download: 0 times
Share this document with a friend

of 47

Transcript
  • 8/10/2019 Tutorial7 Dummy Variables

    1/47

    EViews TrainingDummy Variables

    Note: Data and workfiles for this tutorial are provided in:

    Data: Data.xlsx

    Results: Results.wf1

    Practice Workfile: Data.wf1

  • 8/10/2019 Tutorial7 Dummy Variables

    2/47

    Data and Workfile Documentation

    Data.wf1and Data.xlsx consist of two pages (tabs) with the following data:

    Workfile Page: Dated(Data.xlsx tab Dated): daily, from Jan 1, 1950August 2, 2012

    VolumeS&P 500 volume (source: St. Louis Federal Reserve Bank, FRED Database)

    Volume1- percent change in S&P500 Volume

    ReturnS&P 500 return (source: St. Louis Federal Reserve Bank, FRED Database)

    Workfile Page: Cross_Sect ion (Data.xlsx tab Cross_Section) data on 526 individuals

    wagewage per hour (in dollars)

    educnumber of years of education

    experexperience, number of years

    femaledummy variable, equal to 1 if female, 0 otherwise

    marr ieddummy variable, equal to 1 if married, 0 otherwise

    2

    * These data are from Wooldridge, Introductory Econometrics(4thEdition).

  • 8/10/2019 Tutorial7 Dummy Variables

    3/47

    Data Series Objects: Series and Groups

    It is fairly easy to create dummy variables in EViews by using sample

    commandsor a number of EViews functions:

    smp l

    @recode

    @date

    @expand

    This tutorial demonstrates how to create:

    Dummy Variables using samples

    Dummy Variables using the @recodefunction

    Date Dummy Variables using the @datefunction

    Categorical Dummy Variables using the @expandfunction

    3

  • 8/10/2019 Tutorial7 Dummy Variables

    4/47

    Creating Dummy VariablesUsing Samples

  • 8/10/2019 Tutorial7 Dummy Variables

    5/47

    Creating Dummy Variables Using Samples:

    Example 1

    The easiest way to create dummy variables in EViews is by using samples

    (smp lcommand).

    Lets illustrate a few examples using the Datedpage in Work fi le Data.wf 1.

    Suppose you would like to create a dummy variable equal to 1, if return>0.2,

    and 0 otherwise.

    5

    Dummies with samples: Example 1

    1. Open Data.wf1workfile. Click on the

    Datedpage.

    2. Type in the command window:

    smpl if return>0.2

    series dummy1=1

    smpl if return

  • 8/10/2019 Tutorial7 Dummy Variables

    6/47

    Creating Dummy Variables Using Samples:

    Example 1 (contd)

    The original series (return) and the new dummy variable are both shown here.

    Original series Dummy Variable

    6

  • 8/10/2019 Tutorial7 Dummy Variables

    7/47

    Creating Dummy Variables Using Samples:

    Example 2

    7

    Now suppose you would like to create a dummy variable equal to 1, if

    return>0.3, or return0.3 or return-0.3 and return

  • 8/10/2019 Tutorial7 Dummy Variables

    8/47

    Creating Dummy VariablesUsing @recode function

  • 8/10/2019 Tutorial7 Dummy Variables

    9/47

    Creating Dummy Variables Using @recode

    Another way to create dummy variables in EViews is by using @recode

    function.

    9

    Function Description

    @recode(s,x,y) Returns x if condition s is true; otherwise it returns y

  • 8/10/2019 Tutorial7 Dummy Variables

    10/47

    Creating Dummy Variables Using @recode:

    Example 1

    10

    Suppose you want to create a dummy equal to 1 ifreturn

  • 8/10/2019 Tutorial7 Dummy Variables

    11/47

    Creating Dummy Variables Using @recode:

    Example 1 (contd)

    The original series (return) and the new dummy variable are both shown here.

    Original series Dummy Variable

    11

  • 8/10/2019 Tutorial7 Dummy Variables

    12/47

    Creating Dummy Variables Using @recode:

    Example 2

    12

    Create a dummy variable equal to 1 if -0.2

  • 8/10/2019 Tutorial7 Dummy Variables

    13/47

    Creating Dummy Variables Using @recode:

    Example 3

    13

    Create a dummy variable equal to 1 if return=1, 0 otherwise.

    Dummies with @recode: Example 3

    1. Click on Dated workfile page. Type in the command window:

    series dummy6=@recode(return=1,1,0)

    2. Press Enter.

    Original Series Dummy Variable

  • 8/10/2019 Tutorial7 Dummy Variables

    14/47

    Creating Dummy Variables Using @recode:

    Example 4

    14

    Suppose you would like to create two new series by separating an

    existing series (return) as follows;

    series1 collects values of returnfor which -0.3

  • 8/10/2019 Tutorial7 Dummy Variables

    15/47

    Creating Dummy Variables Using @recode:

    Example 4 (contd)

    15

    The original series (return) and the two new dummy variables are shown here.

    Original series Series 1 Series 2

  • 8/10/2019 Tutorial7 Dummy Variables

    16/47

    Creating Dummy Variables Using @recode:

    Example 5

    16

    Lets create a total return index from daily S&P500 returns.

    Dummies with @recode: Example 5

    1. Click on Dated workfile page. Type in the command window:

    series index=@recode(@trend=0,100, index(-1)*(1+return/100))

    2. Press Enter.

    Original Series Index

  • 8/10/2019 Tutorial7 Dummy Variables

    17/47

    Creating Dummy Variables Using @recode:

    Example 6

    17

    Lastly, suppose you want to create a series that excludes outliers from the

    returnseries in the previous example. Specifically, suppose that the new series excludes the highest and the lowest

    2.5% values of returns.

    Dummies with @recode: Example 6

    1. Click on Dated workfile page. Type in the command window:

    series no_outlier=@recode(return>@quantile(return,0.025) and

    return

  • 8/10/2019 Tutorial7 Dummy Variables

    18/47

    Creating Dummy Variables Using @recode:

    Example 6 (contd)

    18

    The original series (return) and the new series are shown here.

    Original series No Outlier

  • 8/10/2019 Tutorial7 Dummy Variables

    19/47

    A Few Notes on Simple Dummy Variables

    For simple dummies, you dont need to use smp lor @recode.

    You can create them simply by defining the logical expression directly in the

    command window, as shown in the following examples.

    However, you may have to use smp lor @recode for more complex cases.

    19

  • 8/10/2019 Tutorial7 Dummy Variables

    20/47

    A few Notes on Simple Dummy Variables:

    Example 1

    20

    Create a dummy equal to 1 if return

  • 8/10/2019 Tutorial7 Dummy Variables

    21/47

    A few Notes on Simple Dummy Variables:

    Example 2

    21

    Create a dummy equal to 1 if return=0.1, and 0 otherwise.

    Simple Dummies: Example 2

    1. Click on Dated workfile page. Type in the command

    window:

    series d2=return=0.1

    2. Press Enter. Original Series Dummy Variable

  • 8/10/2019 Tutorial7 Dummy Variables

    22/47

    Date Dummy Variables

  • 8/10/2019 Tutorial7 Dummy Variables

    23/47

    Creating Date Dummies

    Dated dummies can be created by using @recode and @dateor @dateval.

    For more details on date functions, see tutorial on Date Functions.

    23

    Funct ion Description

    @date Returns the date associated with each observation

    @dateval Returns the date associated with a text representation of a date

    @year Returns the year in which each observation begins

    @quarter Returns the quarter of the year in which each observation begins

    @month Returns the month of the year in which each observation begins

    @day Returns the day of the month in which each observations begins

    @weekday Returns the day of the week

    Main Functions

  • 8/10/2019 Tutorial7 Dummy Variables

    24/47

    Creating Date Dummies:

    Example 1

    24

    Suppose you want to create a dummy variable equal to 1 for all dates after

    1995/03/15 and 0 otherwise.Date Dummies: Example 1

    1. Click on the Datedworkfile page. Now, type in the command window:

    series dumdate1=@recode(@date>@dateval("1995/3/15"),1,0)

    2. Press Enter.

    Alternatively, without @recode:

    1. Type in the command window:series dumdate1=@date>@dateval("1995/3/15")

    2. Press Enter.

    Dummy Variable

  • 8/10/2019 Tutorial7 Dummy Variables

    25/47

    Creating Date Dummies:

    Example 2

    25

    Create a dummy variable equal to 1 for all dates before April 1979 and after

    February 1994.Date Dummies: Example 2

    1. Click on the Datedworkfile page. Type in the command window:

    series dumdate2=@recode(@date@dateval("1994m2"),1,0)

    2. Press Enter (please type the command in one line).

    Alternatively, without @recode:

    1. Type in the command window:

    series dumdate2=@date@dateval("1994m2")

    2. Press Enter(please type the command in one line).

  • 8/10/2019 Tutorial7 Dummy Variables

    26/47

    26

    Creating Date Dummies:

    Example 2 (contd)

    Dummy Variable

    A graph of the new dummy variable is shown here.

    As you can see, it assumes a value of 1 prior to April 1979 and postFebruary 1994.

  • 8/10/2019 Tutorial7 Dummy Variables

    27/47

    Creating Date Dummies:

    Example 4

    27

    Dated dummies can also be created using @year, @month, @day date functions.

    Create a dummy equal to 1 if the month is January or the day of the week is Friday.

    Date Dummies: Example 4

    1. Click on the Datedworkfile page. Now, type in the command window:

    series dumdate4=@recode(@month=1 or @weekday=5,1,0)

    2. Press Enter.

    Alternatively, without @recode:1. Type in the command window:

    series dumdate4=@month=1 or @weekday=5

    2. Press Enter.

    Dummy Variable

  • 8/10/2019 Tutorial7 Dummy Variables

    28/47

    Creating Date Dummies:

    Example 5

    28

    Create a dummy equal to 1 for all years after 1994 if the month is January or the

    day of the week is Friday.

    Date Dummies: Example 5

    1. Click on the Datedworkfile page. Now, type in the command window:

    series dumdate5=@recode(@year>1994 and (@month=1 or @weekday=5),1,0)

    2. Press Enter.

    Alternatively, without @recode:1. Type in the command window:

    series dumdate5=@year>1994 and (@month=1 or @weekday=5)

    2. Press Enter.

  • 8/10/2019 Tutorial7 Dummy Variables

    29/47

    Creating Date Dummies:

    Example 6

    29

    Create a dummy variable equal to 1 for all months when there is a presidential

    election.Date Dummies: Example 6

    1. Click on the Datedworkfile page. Now, type in the command window:

    smpl @allseries dumpres=0

    smpl if @mod(@year,4)=0 and @month=11

    series dumpres=1smpl @all

    2. Press Enter after each command line.

    Dummy Variable

  • 8/10/2019 Tutorial7 Dummy Variables

    30/47

    Dummy Variables in Non-DatedWorkfiles

  • 8/10/2019 Tutorial7 Dummy Variables

    31/47

    Dummies in Non-dated Workfiles

    You can follow the methodology used in Dated Dummies to create dummies

    in non-dated workfiles.

    Simply use @obsnum (instead of @date) to create the dummy variables.

    31

  • 8/10/2019 Tutorial7 Dummy Variables

    32/47

    Creating Dummies in Non-Dated Workfiles:

    Example 1

    32

    In the Cross-Sect ionpage, create a dummy variable equal to 1 after the 5th

    observation, and 0 otherwise.Dummies in non-Dated Workfiles: Example 1

    1. Click on the Cross-Sect ionworkfile page. Now, type in the command window:

    series dum1=@recode(@obsnum>5,1,0)

    2. Press Enter.

    Dummy Variable

    Alternatively, without @recode:1. Type in the command window:

    series dum1=@obsnum>5

    2. Press Enter.

  • 8/10/2019 Tutorial7 Dummy Variables

    33/47

    Creating Dummies in Non-Dated Workfiles:

    Example 2

    33

    In the Cross-Sect ionpage, create a dummy variable equal to 1 for all the

    observations between the 7th

    and 12th

    observationDummies in non-Dated Workfiles: Example 2

    1. Click on the Cross-Sect ionworkfile page. Now, type in the command window:

    series dum2=@recode((@obsnum>=7 and @obsnum=7 and @ obsnum

  • 8/10/2019 Tutorial7 Dummy Variables

    34/47

    Creating Dummy VariablesUsing@expand function

  • 8/10/2019 Tutorial7 Dummy Variables

    35/47

    Creating Categorical Dummies

    Categorical dummies can be easily created by using @expand function.

    35

    Function Description

    @expand Allows you to create a group of dummy variables by expanding

    out one or more series into individual categories

  • 8/10/2019 Tutorial7 Dummy Variables

    36/47

    Creating Dummy Variables using @expand:

    Example 1: Categorical Dummies

    Lets create a group of series as follows:

    One series with 1 if female and 0 if male

    One series with 1 if male and 0 if female

    36

    Dummies using @expand: Example 1

    1. Click on the Cross-Sect ionworkfile page. Type in the command window:

    group g1 @expand(female)

    2. Press Enter.

    The group is shown here withthe two series separating

    males from females.

  • 8/10/2019 Tutorial7 Dummy Variables

    37/47

    Creating Dummy Variables using @expand:

    Example 2: Categorical Dummies

    Now lets create a new group of series as follows:

    One series containing 1 if male and single. One series containing 1 if male and married.

    One series containing 1 if female and single.

    One series containing 1 if female and married.

    37

    Dummies using @expand: Example 2

    1. Click on the Cross-Sect ionworkfile page. Type in the command window:group g2 @expand(female, married)

    2. Press Enter.

  • 8/10/2019 Tutorial7 Dummy Variables

    38/47

    Creating Dummy Variables using @expand:

    Example 3: Categorical Dummies

    Now lets create a new group of series as follows:

    One series equal to educ(years of education) if male (female=0). One series equal to educif female (female =1).

    38

    Dummies using @expand: Example 3

    1. Click on the Cross-Sect ionworkfile page. Type in the command window:

    group g3 educ*@expand(female)

    2. Press Enter.

    The group is shown here with the twoseries showing the years of

    education for males separately from

    years of education for females.

    C @

  • 8/10/2019 Tutorial7 Dummy Variables

    39/47

    Dummies can also be created by using @expand in conjunction with other

    date functions (@year, @month, @day, etc.).

    For example, suppose you would like to create a dummy variable for each

    day of the week.

    Creating Dummy Variables using @expand:

    Example 4: Date Dummies

    39

    Dummies using @expand: Example 4

    1. Click on the Datedworkfile page. Type in the command window:

    group g1 @expand(@weekday)

    2. Press Enter.

    C i D V i bl i @ d

  • 8/10/2019 Tutorial7 Dummy Variables

    40/47

    Suppose that now you would like to create a dummy variable for each year.

    Creating Dummy Variables using @expand:

    Example 5: Date Dummies

    40

    Dummies using @expand: Example 5

    1. Click on the Datedworkfile page. Type in the command window:

    group g2 @expand(@year)

    2. Press Enter.

  • 8/10/2019 Tutorial7 Dummy Variables

    41/47

    Dummy Variables in Regressions

    D i i R i

  • 8/10/2019 Tutorial7 Dummy Variables

    42/47

    Dummies in Regressions:

    Example 1

    In EViews you can use dummy variable expressions in regressions without

    having to first create and save the dummies. Suppose you would like to estimate a regression of returnon vo lume1and a

    dummy variable equal to 1 for all dates after 1994/12/2.

    42

    Dummies in Regressions: Example 1

    1. Click on the Datedworkfile page. Type in the

    command window:

    equation eq1.ls return c volume1

    @date>@dateval("1994/12/2")

    2. Press Enter (type the command in one line).

    Alternatively:

    1. Select QuickEstimate Equation from

    the top menu bar.

    2. The Equation Estimation box opens up.

    Specify here your equation (as shown in

    the figure)

    3. Click OK.

    D i i R i

  • 8/10/2019 Tutorial7 Dummy Variables

    43/47

    Dummies in Regressions:

    Example 1 (contd)

    43

    The estimation output is shown here.

    As you can see EViews estimates the coefficient of the dummy variable

    directly without us having to create and save the date dummy first.

    *Note: see tutorial on Basic Estimation for details

    on regression analysis in EViews.

    D i i R i

  • 8/10/2019 Tutorial7 Dummy Variables

    44/47

    Dummies in Regressions:

    Example 2 Now suppose you would like to estimate a regression of returnon vo lume1and

    a dummy variable equal to 1 for all Januaries since 1994/12/2.

    44

    Dummies in Regressions: Example 2

    1. Click on the Datedworkfile page. Type in the command window:

    equation eq2.ls return c volume1 (@date>@dateval("1994/12/2") and @month=1)

    2. Press Enter.

    Note that there are no spaces in

    the logical expression:

    @month=1 is correct @month = 1 is not correct

    D i i R i

  • 8/10/2019 Tutorial7 Dummy Variables

    45/47

    Dummies in Regressions:

    Example 3

    45

    Now lets carry out a regression in the Cross_Sect ion workfile page.

    Suppose we want to regress wageon exper, educand four dummy variablescreated by @expand(female,married).

    Dummies in Regressions: Example 3

    1. Click on the Cross_Sect ionworkfile page.

    Type in the command window:

    equation eq1.ls wage exper educ

    @expand(female, married)

    2. Press Enter (type command in one line).

    D i i R i

  • 8/10/2019 Tutorial7 Dummy Variables

    46/47

    Dummies in Regressions:

    Example 3 (contd)

    46

    In the previous example, notice that the constant was not included.

    If you include a constant, then EViews will be unable to estimate the

    regression and return an error message because of perfect collinearity (the

    dummy variable trap).

    You will receive an error message

    (shown here). In order to estimate the regression with

    a constant, you should exclude one of

    the dummies.

    Dummies in Regressions: Example 3

    1. Type in the command window:

    equation eq1.ls wage c exper educ@expand(female, married)

    2. Press Enter (type command in one line).

    D i i R i

  • 8/10/2019 Tutorial7 Dummy Variables

    47/47

    47

    Lets exclude one of the dummies. You can use either one of the following

    commands: @dropf i rs t (drops the first dummy).

    @droplast (drops the last dummy).

    Dummies in Regressions:

    Example 3 (contd)

    Dummies in Regressions: Example 3

    1. Type in the command window:

    equation eq2.ls wage c exper educ@expand(female, married,@dropfirst)

    2. Press Enter (type command in one line).


Recommended