The purpose of this in-class lab11 is to use R to practice estimating time series regression models and to test for serial correlation. The lab should be completed in your group. To get credit, upload your .R script to the appropriate place on Canvas.
First, install the pdfetch
, zoo
, and dynlm
packages. pdfetch
stands for “Public Data Fetch” and is a slick way of downloading statistics on stock prices, GDP, inflation, unemployment, etc. zoo
and dynlm
are packages useful for working with time series data.
Open up a new R script (named ICL11_XYZ.R
, where XYZ
are your initials) and add the usual “preamble” to the top:
# Add names of group members HERE
library(tidyverse)
library(wooldridge)
library(broom)
library(car)
library(pdfetch)
library(zoo)
library(dynlm)
library(magrittr)
We’re going to use data on US macroeconomic indicators. The wooldridge
data set is called intdef
.
df <- as_tibble(intdef)
df %>% DT::datatable()
df
as time series datadf.ts <- zoo(df, order.by=df$year)
df.ts
## year i3 inf rec out def i3_1 inf_1 def_1 ci3
## 1948 1948 1.04 8.1 16.2 11.6 -4.6000004 NA NA NA NA
## 1949 1949 1.10 -1.2 14.5 14.3 -0.1999998 1.04 8.1 -4.6000004 0.06000006
## 1950 1950 1.22 1.3 14.4 15.6 1.2000008 1.10 -1.2 -0.1999998 0.12000000
## 1951 1951 1.55 7.9 16.1 14.2 -1.9000006 1.22 1.3 1.2000008 0.32999992
## 1952 1952 1.77 1.9 19.0 19.4 0.3999996 1.55 7.9 -1.9000006 0.22000003
## 1953 1953 1.93 0.8 18.7 20.4 1.6999989 1.77 1.9 0.3999996 0.15999997
## 1954 1954 0.95 0.7 18.5 18.8 0.2999992 1.93 0.8 1.6999989 -0.97999996
## 1955 1955 1.75 -0.4 16.5 17.3 0.7999992 0.95 0.7 0.2999992 0.80000001
## 1956 1956 2.66 1.5 17.5 16.5 -1.0000000 1.75 -0.4 0.7999992 0.91000009
## 1957 1957 3.27 3.3 17.7 17.0 -0.7000008 2.66 1.5 -1.0000000 0.60999990
## 1958 1958 1.84 2.8 17.3 17.9 0.6000004 3.27 3.3 -0.7000008 -1.42999995
## 1959 1959 3.41 0.7 16.2 18.8 2.5999985 1.84 2.8 0.6000004 1.57000005
## 1960 1960 2.93 1.7 17.8 17.8 0.0000000 3.41 0.7 2.5999985 -0.48000002
## 1961 1961 2.38 1.0 17.8 18.4 0.6000004 2.93 1.7 0.0000000 -0.54999995
## 1962 1962 2.78 1.0 17.6 18.8 1.1999989 2.38 1.0 0.6000004 0.39999986
## 1963 1963 3.16 1.3 17.8 18.6 0.8000011 2.78 1.0 1.1999989 0.38000011
## 1964 1964 3.55 1.3 17.6 18.5 0.8999996 3.16 1.3 0.8000011 0.38999987
## 1965 1965 3.95 1.6 17.0 17.2 0.2000008 3.55 1.3 0.8999996 0.40000010
## 1966 1966 4.88 2.9 17.3 17.8 0.5000000 3.95 1.6 0.2000008 0.93000007
## 1967 1967 4.32 3.1 18.4 19.4 1.0000000 4.88 2.9 0.5000000 -0.55999994
## 1968 1968 5.34 4.2 17.6 20.5 2.8999996 4.32 3.1 1.0000000 1.01999998
## 1969 1969 6.68 5.5 19.7 19.4 -0.3000011 5.34 4.2 2.8999996 1.33999968
## 1970 1970 6.46 5.7 19.0 19.3 0.2999992 6.68 5.5 -0.3000011 -0.21999979
## 1971 1971 4.35 4.4 17.3 19.5 2.2000008 6.46 5.7 0.2999992 -2.11000013
## 1972 1972 4.07 3.2 17.6 19.6 2.0000000 4.35 4.4 2.2000008 -0.27999973
## 1973 1973 7.04 6.2 17.6 18.7 1.1000004 4.07 3.2 2.0000000 2.96999979
## 1974 1974 7.89 11.0 18.3 18.7 0.4000015 7.04 6.2 1.1000004 0.84999990
## 1975 1975 5.84 9.1 17.9 21.3 3.3999996 7.89 11.0 0.4000015 -2.04999971
## 1976 1976 4.99 5.8 17.1 21.4 4.2999992 5.84 9.1 3.3999996 -0.85000038
## 1977 1977 5.27 6.5 18.0 20.7 2.7000008 4.99 5.8 4.2999992 0.28000021
## 1978 1978 7.22 7.6 18.0 20.7 2.7000008 5.27 6.5 2.7000008 1.94999981
## 1979 1979 10.04 11.3 18.5 20.1 1.6000004 7.22 7.6 2.7000008 2.82000017
## 1980 1980 11.51 13.5 19.0 21.7 2.7000008 10.04 11.3 1.6000004 1.47000027
## 1981 1981 14.03 10.3 19.6 22.2 2.6000004 11.51 13.5 2.7000008 2.51999950
## 1982 1982 10.69 6.2 19.2 23.1 3.8999996 14.03 10.3 2.6000004 -3.34000015
## 1983 1983 8.63 3.2 17.4 23.5 6.1000004 10.69 6.2 3.8999996 -2.05999947
## 1984 1984 9.58 4.3 17.3 22.1 4.8000011 8.63 3.2 6.1000004 0.94999981
## 1985 1985 7.48 3.6 17.7 22.8 5.0999985 9.58 4.3 4.8000011 -2.09999990
## 1986 1986 5.98 1.9 17.5 22.5 5.0000000 7.48 3.6 5.0999985 -1.50000000
## 1987 1987 5.82 3.6 18.4 21.6 3.2000008 5.98 1.9 5.0000000 -0.15999985
## 1988 1988 6.69 4.1 18.1 21.2 3.1000004 5.82 3.6 3.2000008 0.86999989
## 1989 1989 8.12 4.8 18.3 21.2 2.9000015 6.69 4.1 3.1000004 1.42999983
## 1990 1990 7.51 5.4 18.0 21.8 3.7999992 8.12 4.8 2.9000015 -0.60999966
## 1991 1991 5.42 4.2 17.8 22.3 4.5000000 7.51 5.4 3.7999992 -2.09000015
## 1992 1992 3.45 3.0 17.5 22.1 4.6000004 5.42 4.2 4.5000000 -1.97000003
## 1993 1993 3.02 3.0 17.5 21.4 3.8999996 3.45 3.0 4.6000004 -0.43000007
## 1994 1994 4.29 2.6 18.1 21.0 2.8999996 3.02 3.0 3.8999996 1.26999998
## 1995 1995 5.51 2.8 18.5 20.7 2.2000008 4.29 2.6 2.8999996 1.22000027
## 1996 1996 5.02 3.0 18.9 20.3 1.3999996 5.51 2.8 2.2000008 -0.49000025
## 1997 1997 5.07 2.3 19.3 19.6 0.3000011 5.02 3.0 1.3999996 0.05000019
## 1998 1998 4.81 1.6 20.0 19.2 -0.7999992 5.07 2.3 0.3000011 -0.26000023
## 1999 1999 4.66 2.2 20.0 18.6 -1.3999996 4.81 1.6 -0.7999992 -0.15000010
## 2000 2000 5.85 3.4 20.9 18.4 -2.5000000 4.66 2.2 -1.3999996 1.19000006
## 2001 2001 3.45 2.8 19.8 18.6 -1.1999989 5.85 3.4 -2.5000000 -2.39999986
## 2002 2002 1.62 1.6 17.9 19.4 1.5000000 3.45 2.8 -1.1999989 -1.83000004
## 2003 2003 1.02 2.3 16.5 19.9 3.3999996 1.62 1.6 1.5000000 -0.60000002
## cinf cdef y77
## 1948 NA NA 0
## 1949 -9.3000002 4.40000057 0
## 1950 2.5000000 1.40000057 0
## 1951 6.6000004 -3.10000134 0
## 1952 -6.0000000 2.30000019 0
## 1953 -1.0999999 1.29999924 0
## 1954 -0.1000000 -1.39999962 0
## 1955 -1.1000000 0.50000000 0
## 1956 1.9000000 -1.79999924 0
## 1957 1.8000000 0.29999924 0
## 1958 -0.5000000 1.30000114 0
## 1959 -2.0999999 1.99999809 0
## 1960 1.0000000 -2.59999847 0
## 1961 -0.7000000 0.60000038 0
## 1962 0.0000000 0.59999847 0
## 1963 0.3000000 -0.39999771 0
## 1964 0.0000000 0.09999847 0
## 1965 0.3000001 -0.69999886 0
## 1966 1.3000001 0.29999924 0
## 1967 0.1999998 0.50000000 0
## 1968 1.0999999 1.89999962 0
## 1969 1.3000002 -3.20000076 0
## 1970 0.1999998 0.60000038 0
## 1971 -1.2999997 1.90000153 0
## 1972 -1.2000000 -0.20000076 0
## 1973 2.9999998 -0.89999962 0
## 1974 4.8000002 -0.69999886 0
## 1975 -1.8999996 2.99999809 0
## 1976 -3.3000002 0.89999962 0
## 1977 0.6999998 -1.59999847 1
## 1978 1.0999999 0.00000000 1
## 1979 3.7000003 -1.10000038 1
## 1980 2.1999998 1.10000038 1
## 1981 -3.1999998 -0.10000038 1
## 1982 -4.1000004 1.29999924 1
## 1983 -2.9999998 2.20000076 1
## 1984 1.1000001 -1.29999924 1
## 1985 -0.7000003 0.29999733 1
## 1986 -1.6999999 -0.09999847 1
## 1987 1.6999999 -1.79999924 1
## 1988 0.5000000 -0.10000038 1
## 1989 0.7000003 -0.19999886 1
## 1990 0.5999999 0.89999771 1
## 1991 -1.2000003 0.70000076 1
## 1992 -1.1999998 0.10000038 1
## 1993 0.0000000 -0.70000076 1
## 1994 -0.4000001 -1.00000000 1
## 1995 0.2000000 -0.69999886 1
## 1996 0.2000000 -0.80000114 1
## 1997 -0.7000000 -1.09999847 1
## 1998 -0.6999999 -1.10000038 1
## 1999 0.6000000 -0.60000038 1
## 2000 1.2000000 -1.10000038 1
## 2001 -0.6000001 1.30000114 1
## 2002 -1.1999999 2.69999886 1
## 2003 0.6999999 1.89999962 1
or:
df %>% ts(frequency = 1,start = c(1948))
## Time Series:
## Start = 1948
## End = 2003
## Frequency = 1
## year i3 inf rec out def i3_1 inf_1 def_1 ci3
## 1948 1948 1.04 8.1 16.2 11.6 -4.6000004 NA NA NA NA
## 1949 1949 1.10 -1.2 14.5 14.3 -0.1999998 1.04 8.1 -4.6000004 0.06000006
## 1950 1950 1.22 1.3 14.4 15.6 1.2000008 1.10 -1.2 -0.1999998 0.12000000
## 1951 1951 1.55 7.9 16.1 14.2 -1.9000006 1.22 1.3 1.2000008 0.32999992
## 1952 1952 1.77 1.9 19.0 19.4 0.3999996 1.55 7.9 -1.9000006 0.22000003
## 1953 1953 1.93 0.8 18.7 20.4 1.6999989 1.77 1.9 0.3999996 0.15999997
## 1954 1954 0.95 0.7 18.5 18.8 0.2999992 1.93 0.8 1.6999989 -0.97999996
## 1955 1955 1.75 -0.4 16.5 17.3 0.7999992 0.95 0.7 0.2999992 0.80000001
## 1956 1956 2.66 1.5 17.5 16.5 -1.0000000 1.75 -0.4 0.7999992 0.91000009
## 1957 1957 3.27 3.3 17.7 17.0 -0.7000008 2.66 1.5 -1.0000000 0.60999990
## 1958 1958 1.84 2.8 17.3 17.9 0.6000004 3.27 3.3 -0.7000008 -1.42999995
## 1959 1959 3.41 0.7 16.2 18.8 2.5999985 1.84 2.8 0.6000004 1.57000005
## 1960 1960 2.93 1.7 17.8 17.8 0.0000000 3.41 0.7 2.5999985 -0.48000002
## 1961 1961 2.38 1.0 17.8 18.4 0.6000004 2.93 1.7 0.0000000 -0.54999995
## 1962 1962 2.78 1.0 17.6 18.8 1.1999989 2.38 1.0 0.6000004 0.39999986
## 1963 1963 3.16 1.3 17.8 18.6 0.8000011 2.78 1.0 1.1999989 0.38000011
## 1964 1964 3.55 1.3 17.6 18.5 0.8999996 3.16 1.3 0.8000011 0.38999987
## 1965 1965 3.95 1.6 17.0 17.2 0.2000008 3.55 1.3 0.8999996 0.40000010
## 1966 1966 4.88 2.9 17.3 17.8 0.5000000 3.95 1.6 0.2000008 0.93000007
## 1967 1967 4.32 3.1 18.4 19.4 1.0000000 4.88 2.9 0.5000000 -0.55999994
## 1968 1968 5.34 4.2 17.6 20.5 2.8999996 4.32 3.1 1.0000000 1.01999998
## 1969 1969 6.68 5.5 19.7 19.4 -0.3000011 5.34 4.2 2.8999996 1.33999968
## 1970 1970 6.46 5.7 19.0 19.3 0.2999992 6.68 5.5 -0.3000011 -0.21999979
## 1971 1971 4.35 4.4 17.3 19.5 2.2000008 6.46 5.7 0.2999992 -2.11000013
## 1972 1972 4.07 3.2 17.6 19.6 2.0000000 4.35 4.4 2.2000008 -0.27999973
## 1973 1973 7.04 6.2 17.6 18.7 1.1000004 4.07 3.2 2.0000000 2.96999979
## 1974 1974 7.89 11.0 18.3 18.7 0.4000015 7.04 6.2 1.1000004 0.84999990
## 1975 1975 5.84 9.1 17.9 21.3 3.3999996 7.89 11.0 0.4000015 -2.04999971
## 1976 1976 4.99 5.8 17.1 21.4 4.2999992 5.84 9.1 3.3999996 -0.85000038
## 1977 1977 5.27 6.5 18.0 20.7 2.7000008 4.99 5.8 4.2999992 0.28000021
## 1978 1978 7.22 7.6 18.0 20.7 2.7000008 5.27 6.5 2.7000008 1.94999981
## 1979 1979 10.04 11.3 18.5 20.1 1.6000004 7.22 7.6 2.7000008 2.82000017
## 1980 1980 11.51 13.5 19.0 21.7 2.7000008 10.04 11.3 1.6000004 1.47000027
## 1981 1981 14.03 10.3 19.6 22.2 2.6000004 11.51 13.5 2.7000008 2.51999950
## 1982 1982 10.69 6.2 19.2 23.1 3.8999996 14.03 10.3 2.6000004 -3.34000015
## 1983 1983 8.63 3.2 17.4 23.5 6.1000004 10.69 6.2 3.8999996 -2.05999947
## 1984 1984 9.58 4.3 17.3 22.1 4.8000011 8.63 3.2 6.1000004 0.94999981
## 1985 1985 7.48 3.6 17.7 22.8 5.0999985 9.58 4.3 4.8000011 -2.09999990
## 1986 1986 5.98 1.9 17.5 22.5 5.0000000 7.48 3.6 5.0999985 -1.50000000
## 1987 1987 5.82 3.6 18.4 21.6 3.2000008 5.98 1.9 5.0000000 -0.15999985
## 1988 1988 6.69 4.1 18.1 21.2 3.1000004 5.82 3.6 3.2000008 0.86999989
## 1989 1989 8.12 4.8 18.3 21.2 2.9000015 6.69 4.1 3.1000004 1.42999983
## 1990 1990 7.51 5.4 18.0 21.8 3.7999992 8.12 4.8 2.9000015 -0.60999966
## 1991 1991 5.42 4.2 17.8 22.3 4.5000000 7.51 5.4 3.7999992 -2.09000015
## 1992 1992 3.45 3.0 17.5 22.1 4.6000004 5.42 4.2 4.5000000 -1.97000003
## 1993 1993 3.02 3.0 17.5 21.4 3.8999996 3.45 3.0 4.6000004 -0.43000007
## 1994 1994 4.29 2.6 18.1 21.0 2.8999996 3.02 3.0 3.8999996 1.26999998
## 1995 1995 5.51 2.8 18.5 20.7 2.2000008 4.29 2.6 2.8999996 1.22000027
## 1996 1996 5.02 3.0 18.9 20.3 1.3999996 5.51 2.8 2.2000008 -0.49000025
## 1997 1997 5.07 2.3 19.3 19.6 0.3000011 5.02 3.0 1.3999996 0.05000019
## 1998 1998 4.81 1.6 20.0 19.2 -0.7999992 5.07 2.3 0.3000011 -0.26000023
## 1999 1999 4.66 2.2 20.0 18.6 -1.3999996 4.81 1.6 -0.7999992 -0.15000010
## 2000 2000 5.85 3.4 20.9 18.4 -2.5000000 4.66 2.2 -1.3999996 1.19000006
## 2001 2001 3.45 2.8 19.8 18.6 -1.1999989 5.85 3.4 -2.5000000 -2.39999986
## 2002 2002 1.62 1.6 17.9 19.4 1.5000000 3.45 2.8 -1.1999989 -1.83000004
## 2003 2003 1.02 2.3 16.5 19.9 3.3999996 1.62 1.6 1.5000000 -0.60000002
## cinf cdef y77
## 1948 NA NA 0
## 1949 -9.3000002 4.40000057 0
## 1950 2.5000000 1.40000057 0
## 1951 6.6000004 -3.10000134 0
## 1952 -6.0000000 2.30000019 0
## 1953 -1.0999999 1.29999924 0
## 1954 -0.1000000 -1.39999962 0
## 1955 -1.1000000 0.50000000 0
## 1956 1.9000000 -1.79999924 0
## 1957 1.8000000 0.29999924 0
## 1958 -0.5000000 1.30000114 0
## 1959 -2.0999999 1.99999809 0
## 1960 1.0000000 -2.59999847 0
## 1961 -0.7000000 0.60000038 0
## 1962 0.0000000 0.59999847 0
## 1963 0.3000000 -0.39999771 0
## 1964 0.0000000 0.09999847 0
## 1965 0.3000001 -0.69999886 0
## 1966 1.3000001 0.29999924 0
## 1967 0.1999998 0.50000000 0
## 1968 1.0999999 1.89999962 0
## 1969 1.3000002 -3.20000076 0
## 1970 0.1999998 0.60000038 0
## 1971 -1.2999997 1.90000153 0
## 1972 -1.2000000 -0.20000076 0
## 1973 2.9999998 -0.89999962 0
## 1974 4.8000002 -0.69999886 0
## 1975 -1.8999996 2.99999809 0
## 1976 -3.3000002 0.89999962 0
## 1977 0.6999998 -1.59999847 1
## 1978 1.0999999 0.00000000 1
## 1979 3.7000003 -1.10000038 1
## 1980 2.1999998 1.10000038 1
## 1981 -3.1999998 -0.10000038 1
## 1982 -4.1000004 1.29999924 1
## 1983 -2.9999998 2.20000076 1
## 1984 1.1000001 -1.29999924 1
## 1985 -0.7000003 0.29999733 1
## 1986 -1.6999999 -0.09999847 1
## 1987 1.6999999 -1.79999924 1
## 1988 0.5000000 -0.10000038 1
## 1989 0.7000003 -0.19999886 1
## 1990 0.5999999 0.89999771 1
## 1991 -1.2000003 0.70000076 1
## 1992 -1.1999998 0.10000038 1
## 1993 0.0000000 -0.70000076 1
## 1994 -0.4000001 -1.00000000 1
## 1995 0.2000000 -0.69999886 1
## 1996 0.2000000 -0.80000114 1
## 1997 -0.7000000 -1.09999847 1
## 1998 -0.6999999 -1.10000038 1
## 1999 0.6000000 -0.60000038 1
## 2000 1.2000000 -1.10000038 1
## 2001 -0.6000001 1.30000114 1
## 2002 -1.1999999 2.69999886 1
## 2003 0.6999999 1.89999962 1
Now it will be easy to include lags of various variables into our regression models.
Let’s have a look at the inflation rate for the US over the period 1948–2003:
df.ts %>% class()
## [1] "zoo"
ggplot(df.ts, aes(year, inf)) +
geom_line() +
geom_point() +
scale_x_continuous(limits = c(1948,2004),breaks = seq(1948,2004,2)) +
scale_y_continuous(limits = c(-2.5,15),breaks = seq(-2.5,15,2.5)) +
theme(axis.text.x = element_text(angle = 90,hjust = 0.5,vjust = 0.5))
Now let’s estimate the following regression model:
\[ i3_{t} = \beta_0 + \beta_1 inf_t + \beta_2 inf_{t-1} + \beta_3 inf_{t-2} + \beta_4 def_{t} + u_t \]
where \(i3\) is the 3-month Treasury Bill interest rate, \(inf\) is the inflation rate (as measured by the CPI), and \(def\) is the budget deficit as a percentage of GDP.
The interface and internals of dynlm are very similar to lm, but currently dynlm offers three advantages over the direct use of lm:
est <- dynlm(i3 ~ inf + L(inf,1) + L(inf,2) + def, data=df.ts)
est %>% summary()
##
## Time series regression with "zoo" data:
## Start = 1950, End = 2003
##
## Call:
## dynlm(formula = i3 ~ inf + L(inf, 1) + L(inf, 2) + def, data = df.ts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6223 -1.3623 0.0655 1.2051 3.4422
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.6478 0.4519 3.647 0.000643 ***
## inf 0.4966 0.1317 3.770 0.000439 ***
## L(inf, 1) 0.1419 0.1562 0.908 0.368107
## L(inf, 2) 0.1350 0.1222 1.104 0.274941
## def 0.2216 0.1591 1.393 0.169826
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.778 on 49 degrees of freedom
## Multiple R-squared: 0.633, Adjusted R-squared: 0.603
## F-statistic: 21.12 on 4 and 49 DF, p-value: 3.576e-10
Now let’s test for serial correlation in our model. Serial correlation is defined as
\[ u_t = \rho u_{t-1} \]
with \(\vert\rho\vert>0\).
We want to test
\[ H_0: \rho = 0 \]
To do so, we need to run a regression of residuals (from est
) on lagged residuals and look at the \(t\)-stat.
resids <- resid(est)
est.resid <- dynlm(resids ~ L(resids))
tidy(est.resid)
## # A tibble: 2 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 0.000656 0.168 0.00390 0.997
## 2 L(resids) 0.712 0.102 7.01 0.00000000521
When \(x\) is correlated with lags of \(u\), we need to modify the above test to include our \(x\)’s from our original regression:
est.resid. <- dynlm(resids ~ L(resids) + inf + L(inf,1) + L(inf,2) + def, data = df.ts)
est.resid. %>% summary()
##
## Time series regression with "zoo" data:
## Start = 1951, End = 2003
##
## Call:
## dynlm(formula = resids ~ L(resids) + inf + L(inf, 1) + L(inf,
## 2) + def, data = df.ts)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.4923 -0.4932 0.0490 0.8664 3.2582
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.02948 0.31653 0.093 0.926
## L(resids) 0.72121 0.10379 6.949 9.77e-09 ***
## inf -0.01343 0.09333 -0.144 0.886
## L(inf, 1) -0.08945 0.12312 -0.727 0.471
## L(inf, 2) 0.14435 0.10001 1.443 0.156
## def -0.10479 0.11312 -0.926 0.359
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.243 on 47 degrees of freedom
## Multiple R-squared: 0.5148, Adjusted R-squared: 0.4631
## F-statistic: 9.972 on 5 and 47 DF, p-value: 1.51e-06