Fox J, Kleiber C, Zeileis A (2023).
90 | ivreg: Instrumental-Variables Regression by '2SLS', '2SM', or '2SMM', with Diagnostics.
91 | R package version 0.6-3, https://zeileis.github.io/ivreg/.
92 |
93 |
@Manual{,
94 | title = {ivreg: Instrumental-Variables Regression by '2SLS', '2SM', or '2SMM', with Diagnostics},
95 | author = {John Fox and Christian Kleiber and Achim Zeileis},
96 | year = {2023},
97 | note = {R package version 0.6-3},
98 | url = {https://zeileis.github.io/ivreg/},
99 | }
Two-Stage Least-Squares Regression with Diagnostics
96 |
97 |
An implementation of instrumental variables regression using two-stage least-squares (2SLS) estimation, based on the ivreg() function previously in the AER package. In addition to standard regression functionality (parameter estimation, inference, predictions, etc.) the package provides various regression diagnostics, including hat values, deletion diagnostics such as studentized residuals and Cook’s distances; graphical diagnostics such as component-plus-residual plots and added-variable plots; and effect plots with partial residuals.
98 |
Instrumental variables regression:
99 |
library("ivreg")
100 | ivreg(Q ~ P + D | D + F + A, data = Kmenta)
if in dropdown
35 | if (pos >= 0) {
36 | var menu_anchor = $(links[pos]);
37 | menu_anchor.parent().addClass("active");
38 | menu_anchor.closest("li.dropdown").addClass("active");
39 | }
40 | });
41 |
42 | function paths(pathname) {
43 | var pieces = pathname.split("/");
44 | pieces.shift(); // always starts with /
45 |
46 | var end = pieces[pieces.length - 1];
47 | if (end === "index.html" || end === "")
48 | pieces.pop();
49 | return(pieces);
50 | }
51 |
52 | // Returns -1 if not found
53 | function prefix_length(needle, haystack) {
54 | if (needle.length > haystack.length)
55 | return(-1);
56 |
57 | // Special case for length-0 haystack, since for loop won't run
58 | if (haystack.length === 0) {
59 | return(needle.length === 0 ? 0 : -1);
60 | }
61 |
62 | for (var i = 0; i < haystack.length; i++) {
63 | if (needle[i] != haystack[i])
64 | return(i);
65 | }
66 |
67 | return(haystack.length);
68 | }
69 |
70 | /* Clipboard --------------------------*/
71 |
72 | function changeTooltipMessage(element, msg) {
73 | var tooltipOriginalTitle=element.getAttribute('data-original-title');
74 | element.setAttribute('data-original-title', msg);
75 | $(element).tooltip('show');
76 | element.setAttribute('data-original-title', tooltipOriginalTitle);
77 | }
78 |
79 | if(ClipboardJS.isSupported()) {
80 | $(document).ready(function() {
81 | var copyButton = "";
82 |
83 | $("div.sourceCode").addClass("hasCopyButton");
84 |
85 | // Insert copy buttons:
86 | $(copyButton).prependTo(".hasCopyButton");
87 |
88 | // Initialize tooltips:
89 | $('.btn-copy-ex').tooltip({container: 'body'});
90 |
91 | // Initialize clipboard:
92 | var clipboardBtnCopies = new ClipboardJS('[data-clipboard-copy]', {
93 | text: function(trigger) {
94 | return trigger.parentNode.textContent.replace(/\n#>[^\n]*/g, "");
95 | }
96 | });
97 |
98 | clipboardBtnCopies.on('success', function(e) {
99 | changeTooltipMessage(e.trigger, 'Copied!');
100 | e.clearSelection();
101 | });
102 |
103 | clipboardBtnCopies.on('error', function() {
104 | changeTooltipMessage(e.trigger,'Press Ctrl+C or Command+C to copy');
105 | });
106 | });
107 | }
108 | })(window.jQuery || window.$)
109 |
--------------------------------------------------------------------------------
/docs/pkgdown.yml:
--------------------------------------------------------------------------------
1 | pandoc: 2.19.2
2 | pkgdown: 2.0.7
3 | pkgdown_sha: ~
4 | articles:
5 | Diagnostics-for-2SLS-Regression: Diagnostics-for-2SLS-Regression.html
6 | ivreg: ivreg.html
7 | last_built: 2023-05-15T22:50Z
8 | urls:
9 | reference: https://zeileis.github.io/ivreg/reference
10 | article: https://zeileis.github.io/ivreg/articles
11 |
12 |
--------------------------------------------------------------------------------
/docs/reference/Rplot001.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot001.png
--------------------------------------------------------------------------------
/docs/reference/Rplot002.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot002.png
--------------------------------------------------------------------------------
/docs/reference/Rplot003.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot003.png
--------------------------------------------------------------------------------
/docs/reference/Rplot004.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot004.png
--------------------------------------------------------------------------------
/docs/reference/Rplot005.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot005.png
--------------------------------------------------------------------------------
/docs/reference/Rplot006.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot006.png
--------------------------------------------------------------------------------
/docs/reference/Rplot007.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot007.png
--------------------------------------------------------------------------------
/docs/reference/Rplot008.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot008.png
--------------------------------------------------------------------------------
/docs/reference/Rplot009.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/Rplot009.png
--------------------------------------------------------------------------------
/docs/reference/figures/README-effects-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/figures/README-effects-1.png
--------------------------------------------------------------------------------
/docs/reference/figures/README-influenceplot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/figures/README-influenceplot-1.png
--------------------------------------------------------------------------------
/docs/reference/figures/README-qqplot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/figures/README-qqplot-1.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-1.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-2.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-3.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-4.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-5.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-6.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-7.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-8.png
--------------------------------------------------------------------------------
/docs/reference/ivregDiagnostics-9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/docs/reference/ivregDiagnostics-9.png
--------------------------------------------------------------------------------
/docs/sitemap.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | https://zeileis.github.io/ivreg/404.html
5 |
6 |
7 | https://zeileis.github.io/ivreg/articles/Diagnostics-for-2SLS-Regression.html
8 |
9 |
10 | https://zeileis.github.io/ivreg/articles/index.html
11 |
12 |
13 | https://zeileis.github.io/ivreg/articles/ivreg.html
14 |
15 |
16 | https://zeileis.github.io/ivreg/authors.html
17 |
18 |
19 | https://zeileis.github.io/ivreg/index.html
20 |
21 |
22 | https://zeileis.github.io/ivreg/news/index.html
23 |
24 |
25 | https://zeileis.github.io/ivreg/reference/CigaretteDemand.html
26 |
27 |
28 | https://zeileis.github.io/ivreg/reference/Kmenta.html
29 |
30 |
31 | https://zeileis.github.io/ivreg/reference/SchoolingReturns.html
32 |
33 |
34 | https://zeileis.github.io/ivreg/reference/index.html
35 |
36 |
37 | https://zeileis.github.io/ivreg/reference/ivreg.fit.html
38 |
39 |
40 | https://zeileis.github.io/ivreg/reference/ivreg.html
41 |
42 |
43 | https://zeileis.github.io/ivreg/reference/ivregDiagnostics.html
44 |
45 |
46 | https://zeileis.github.io/ivreg/reference/ivregMethods.html
47 |
48 |
49 |
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/logo.png
--------------------------------------------------------------------------------
/man/CigaretteDemand.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/CigaretteDemand.R
3 | \docType{data}
4 | \name{CigaretteDemand}
5 | \alias{CigaretteDemand}
6 | \title{U.S. Cigarette Demand Data}
7 | \format{
8 | A data frame with 48 rows and 10 columns.
9 | \describe{
10 | \item{packs}{Number of cigarette packs per capita sold in 1995.}
11 | \item{rprice}{Real price in 1995 (including sales tax).}
12 | \item{rincome}{Real per capita income in 1995.}
13 | \item{salestax}{Sales tax in 1995.}
14 | \item{cigtax}{Cigarette-specific taxes (federal and average local excise taxes) in 1995.}
15 | \item{packsdiff}{Difference in \code{log(packs)} (between 1995 and 1985).}
16 | \item{pricediff}{Difference in \code{log(rprice)} (between 1995 and 1985).}
17 | \item{incomediff}{Difference in \code{log(rincome)} (between 1995 and 1985).}
18 | \item{salestaxdiff}{Difference in \code{salestax} (between 1995 and 1985).}
19 | \item{cigtaxdiff}{Difference in \code{cigtax} (between 1995 and 1985).}
20 | }
21 | }
22 | \source{
23 | Online complements to Stock and Watson (2007).
24 | }
25 | \usage{
26 | data("CigaretteDemand", package = "ivreg")
27 | }
28 | \description{
29 | Determinants of cigarette demand for the 48 continental US States in 1995 and
30 | compared between 1995 and 1985.
31 | }
32 | \details{
33 | The data are taken from the online complements to Stock and Watson (2007) and
34 | had been prepared as panel data (in long form) in \code{\link[AER]{CigarettesSW}}
35 | from the AER package (Kleiber and Zeileis 2008). Here, the data are provided by
36 | state (in wide form), readily preprocessed to contain all variables needed for
37 | illustrations of OLS and IV regressions. More related examples from Stock and
38 | Watson (2007) are provided in the AER package in \code{\link[AER]{StockWatson2007}}.
39 | A detailed discussion of the various cigarette demand examples with R code
40 | is provided by Hanck et al. (2020, Chapter 12).
41 | }
42 | \examples{
43 | ## load data
44 | data("CigaretteDemand", package = "ivreg")
45 |
46 | ## basic price elasticity: OLS vs. IV
47 | cig_ols <- lm(log(packs) ~ log(rprice), data = CigaretteDemand)
48 | cig_iv <- ivreg(log(packs) ~ log(rprice) | salestax, data = CigaretteDemand)
49 | cbind(OLS = coef(cig_ols), IV = coef(cig_iv))
50 |
51 | ## adjusting for income differences (exogenous)
52 | cig_iv2 <- ivreg(log(packs) ~ log(rprice) + log(rincome) | salestax + log(rincome),
53 | data = CigaretteDemand)
54 | ## adding a second instrument for log(rprice)
55 | cig_iv3 <- update(cig_iv2, . ~ . | . + cigtax)
56 |
57 | ## comparison using heteroscedasticity-consistent standard errors
58 | library("lmtest")
59 | library("sandwich")
60 | coeftest(cig_iv2, vcov = vcovHC, type = "HC1")
61 | coeftest(cig_iv3, vcov = vcovHC, type = "HC1")
62 |
63 | ## long-run price elasticity using differences between 1995 and 1985
64 | cig_ivdiff1 <- ivreg(packsdiff ~ pricediff + incomediff | incomediff + salestaxdiff,
65 | data = CigaretteDemand)
66 | cig_ivdiff2 <- update(cig_ivdiff1, . ~ . | . - salestaxdiff + cigtaxdiff)
67 | cig_ivdiff3 <- update(cig_ivdiff1, . ~ . | . + cigtaxdiff)
68 | coeftest(cig_ivdiff1, vcov = vcovHC, type = "HC1")
69 | coeftest(cig_ivdiff2, vcov = vcovHC, type = "HC1")
70 | coeftest(cig_ivdiff3, vcov = vcovHC, type = "HC1")
71 | }
72 | \references{
73 | Hanck, C., Arnold, M., Gerber, A., and Schmelzer, M. (2020).
74 | \emph{Introduction to Econometrics with R}. \url{https://www.econometrics-with-r.org/}
75 |
76 | Kleiber, C. and Zeileis, A. (2008). \emph{Applied Econometrics with R}. Springer-Verlag
77 |
78 | Stock, J.H. and Watson, M.W. (2007). \emph{Introduction to Econometrics}, 2nd ed., Addison Wesley.
79 | }
80 | \seealso{
81 | \code{\link[AER]{CigarettesSW}}.
82 | }
83 | \keyword{datasets}
84 |
--------------------------------------------------------------------------------
/man/Kmenta.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/Kmenta.R
3 | \docType{data}
4 | \name{Kmenta}
5 | \alias{Kmenta}
6 | \title{Partly Artificial Data on the U.S. Economy}
7 | \format{
8 | A data frame with 20 rows and 5 columns.
9 | \describe{
10 | \item{Q}{food consumption per capita.}
11 | \item{P}{ratio of food prices to general consumer prices.}
12 | \item{D}{disposable income in constant dollars.}
13 | \item{F}{ratio of preceding year's prices received by farmers to general consumer prices.}
14 | \item{A}{time in years.}
15 | }
16 | }
17 | \source{
18 | Kmenta, J. (1986) \emph{Elements of Econometrics}, 2nd ed., Macmillan.
19 | }
20 | \usage{
21 | data("Kmenta", package = "ivreg")
22 | }
23 | \description{
24 | These are partly contrived data from Kmenta (1986), constructed
25 | to illustrate estimation of a simultaneous-equation econometric model. The data
26 | are an annual time-series for the U.S. economy from 1922 to 1941. The values of the
27 | exogenous variables D, and F, and A are real, while those of the endogenous
28 | variables Q and P are simulated according to the linear simultaneous equation model
29 | fit in the examples.
30 | }
31 | \examples{
32 | data("Kmenta", package = "ivreg")
33 | deq <- ivreg(Q ~ P + D | D + F + A, data = Kmenta) # demand equation
34 | seq <- ivreg(Q ~ P + F + A | D + F + A, data = Kmenta) # supply equation
35 | summary(deq, tests = TRUE)
36 | summary(seq, tests = TRUE)
37 | }
38 | \seealso{
39 | \code{\link{ivreg}}.
40 | }
41 | \keyword{datasets}
42 |
--------------------------------------------------------------------------------
/man/SchoolingReturns.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/SchoolingReturns.R
3 | \docType{data}
4 | \name{SchoolingReturns}
5 | \alias{SchoolingReturns}
6 | \title{U.S. Returns to Schooling Data}
7 | \format{
8 | A data frame with 3010 rows and 22 columns.
9 | \describe{
10 | \item{wage}{Raw wages in 1976 (in cents per hour).}
11 | \item{education}{Education in 1976 (in years).}
12 | \item{experience}{Years of labor market experience, computed as \code{age - education - 6}.}
13 | \item{ethnicity}{Factor indicating ethnicity. Is the individual African-American
14 | (\code{"afam"}) or not (\code{"other"})?}
15 | \item{smsa}{Factor. Does the individual reside in a SMSA (standard metropolitan statistical area) in 1976?}
16 | \item{south}{Factor. Does the individual reside in the South in 1976?}
17 | \item{age}{Age in 1976 (in years).}
18 | \item{nearcollege}{Factor. Did the individual grow up near a 4-year college?}
19 | \item{nearcollege2}{Factor. Did the individual grow up near a 2-year college?}
20 | \item{nearcollege4}{Factor. Did the individual grow up near a 4-year public or private college?}
21 | \item{enrolled}{Factor. Is the individual enrolled in college in 1976?}
22 | \item{married}{factor. Is the individual married in 1976?}
23 | \item{education66}{Education in 1966 (in years).}
24 | \item{smsa66}{Factor. Does the individual reside in a SMSA in 1966?}
25 | \item{south66}{Factor. Does the individual reside in the South in 1966?}
26 | \item{feducation}{Father's educational attainment (in years). Imputed with average if missing.}
27 | \item{meducation}{Mother's educational attainment (in years). Imputed with average if missing.}
28 | \item{fameducation}{Ordered factor coding family education class (from 1 to 9).}
29 | \item{kww}{Knowledge world of work (KWW) score.}
30 | \item{iq}{Normed intelligence quotient (IQ) score}
31 | \item{parents14}{Factor coding living with parents at age 14:
32 | both parents, single mother, step parent, other}
33 | \item{library14}{Factor. Was there a library card in home at age 14?}
34 | }
35 | }
36 | \source{
37 | Supplementary material for Verbeek (2004).
38 | }
39 | \usage{
40 | data("SchoolingReturns", package = "ivreg")
41 | }
42 | \description{
43 | Data from the U.S. National Longitudinal Survey of Young Men (NLSYM) in 1976
44 | but using some variables dating back to earlier years.
45 | }
46 | \details{
47 | Investigating the causal link of schooling on earnings in a classical model
48 | for wage determinants is problematic because it can be argued that schooling
49 | is endogenous. Hence, one possible strategy is to use an exogonous variable
50 | as an instrument for the years of education. In his well-known study, Card (1995)
51 | uses geographical proximity to a college when growing up as such an instrument,
52 | showing that this significantly increases both the years of education and the
53 | wage level obtained on the labor market. Using instrumental variables regression
54 | Card (1995) shows that the estimated returns to schooling are much higher than
55 | when simply using ordinary least squares.
56 |
57 | The data are taken from the supplementary material for Verbeek (2004) and are based
58 | on the work of Card (1995). The U.S. National Longitudinal Survey of Young Men
59 | (NLSYM) began in 1966 and included 5525 men, then aged between 14 and 24.
60 | Card (1995) employs labor market information from the 1976 NLSYM interview which
61 | also included information about educational attainment. Out of the 3694 men
62 | still included in that wave of NLSYM, 3010 provided information on both wages
63 | and education yielding the subset of observations provided in \code{SchoolingReturns}.
64 |
65 | The examples replicate the results from Verbeek (2004) who used the simplest
66 | specifications from Card (1995). Including further region or family background
67 | characteristics improves the model significantly but does not affect much the
68 | main coefficients of interest, namely that of years of education.
69 | }
70 | \examples{
71 | ## load data
72 | data("SchoolingReturns", package = "ivreg")
73 |
74 | ## Table 5.1 in Verbeek (2004) / Table 2(1) in Card (1995)
75 | ## Returns to education: 7.4\%
76 | m_ols <- lm(log(wage) ~ education + poly(experience, 2, raw = TRUE) + ethnicity + smsa + south,
77 | data = SchoolingReturns)
78 | summary(m_ols)
79 |
80 | ## Table 5.2 in Verbeek (2004) / similar to Table 3(1) in Card (1995)
81 | m_red <- lm(education ~ poly(age, 2, raw = TRUE) + ethnicity + smsa + south + nearcollege,
82 | data = SchoolingReturns)
83 | summary(m_red)
84 |
85 | ## Table 5.3 in Verbeek (2004) / similar to Table 3(5) in Card (1995)
86 | ## Returns to education: 13.3\%
87 | m_iv <- ivreg(log(wage) ~ education + poly(experience, 2, raw = TRUE) + ethnicity + smsa + south |
88 | nearcollege + poly(age, 2, raw = TRUE) + ethnicity + smsa + south,
89 | data = SchoolingReturns)
90 | summary(m_iv)
91 | }
92 | \references{
93 | Card, D. (1995). Using Geographical Variation in College Proximity to Estimate the Return to
94 | Schooling. In: Christofides, L.N., Grant, E.K., and Swidinsky, R. (eds.),
95 | \emph{Aspects of Labour Market Behaviour: Essays in Honour of John Vanderkamp},
96 | University of Toronto Press, Toronto, 201-222.
97 |
98 | Verbeek, M. (2004). \emph{A Guide to Modern Econometrics}, 2nd ed. John Wiley.
99 | }
100 | \keyword{datasets}
101 |
--------------------------------------------------------------------------------
/man/figures/README-effects-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/man/figures/README-effects-1.png
--------------------------------------------------------------------------------
/man/figures/README-influenceplot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/man/figures/README-influenceplot-1.png
--------------------------------------------------------------------------------
/man/figures/README-qqplot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/man/figures/README-qqplot-1.png
--------------------------------------------------------------------------------
/man/ivreg.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ivreg.R
3 | \name{ivreg}
4 | \alias{ivreg}
5 | \title{Instrumental-Variable Regression by 2SLS, 2SM, or 2SMM Estimation}
6 | \usage{
7 | ivreg(
8 | formula,
9 | instruments,
10 | data,
11 | subset,
12 | na.action,
13 | weights,
14 | offset,
15 | contrasts = NULL,
16 | model = TRUE,
17 | y = TRUE,
18 | x = FALSE,
19 | method = c("OLS", "M", "MM"),
20 | ...
21 | )
22 | }
23 | \arguments{
24 | \item{formula, instruments}{formula specification(s) of the regression
25 | relationship and the instruments. Either \code{instruments} is missing and
26 | \code{formula} has three parts as in \code{y ~ x1 + x2 | z1 + z2 + z3}
27 | (recommended) or \code{formula} is \code{y ~ x1 + x2} and \code{instruments}
28 | is a one-sided formula \code{~ z1 + z2 + z3} (only for backward
29 | compatibility).}
30 |
31 | \item{data}{an optional data frame containing the variables in the model.
32 | By default the variables are taken from the environment of the
33 | \code{formula}.}
34 |
35 | \item{subset}{an optional vector specifying a subset of observations to be
36 | used in fitting the model.}
37 |
38 | \item{na.action}{a function that indicates what should happen when the data
39 | contain \code{NA}s. The default is set by the \code{na.action} option.}
40 |
41 | \item{weights}{an optional vector of weights to be used in the fitting
42 | process.}
43 |
44 | \item{offset}{an optional offset that can be used to specify an a priori
45 | known component to be included during fitting.}
46 |
47 | \item{contrasts}{an optional list. See the \code{contrasts.arg} of
48 | \code{\link[stats:model.matrix]{model.matrix.default}}.}
49 |
50 | \item{model, x, y}{logicals. If \code{TRUE} the corresponding components of
51 | the fit (the model frame, the model matrices, the response) are returned. These
52 | components are necessary for computing regression diagnostics.}
53 |
54 | \item{method}{the method used to fit the stage 1 and 2 regression:
55 | \code{"OLS"} for traditional 2SLS regression (the default),
56 | \code{"M"} for M-estimation, or \code{"MM"} for MM-estimation, with the
57 | latter two robust-regression methods implemented via the \code{\link[MASS]{rlm}}
58 | function in the \pkg{MASS} package.}
59 |
60 | \item{\dots}{further arguments passed to \code{\link{ivreg.fit}}.}
61 | }
62 | \value{
63 | \code{ivreg} returns an object of class \code{"ivreg"} that inherits from
64 | class \code{"lm"}, with the following components:
65 | \item{coefficients}{parameter estimates, from the stage-2 regression.}
66 | \item{residuals}{vector of model residuals.}
67 | \item{residuals1}{matrix of residuals from the stage-1 regression.}
68 | \item{residuals2}{vector of residuals from the stage-2 regression.}
69 | \item{fitted.values}{vector of predicted means for the response.}
70 | \item{weights}{either the vector of weights used (if any) or \code{NULL} (if none).}
71 | \item{offset}{either the offset used (if any) or \code{NULL} (if none).}
72 | \item{estfun}{a matrix containing the empirical estimating functions.}
73 | \item{n}{number of observations.}
74 | \item{nobs}{number of observations with non-zero weights.}
75 | \item{p}{number of columns in the model matrix x of regressors.}
76 | \item{q}{number of columns in the instrumental variables model matrix z}
77 | \item{rank}{numeric rank of the model matrix for the stage-2 regression.}
78 | \item{df.residual}{residual degrees of freedom for fitted model.}
79 | \item{cov.unscaled}{unscaled covariance matrix for the coefficients.}
80 | \item{sigma}{residual standard deviation.}
81 | \item{qr}{QR decomposition for the stage-2 regression.}
82 | \item{qr1}{QR decomposition for the stage-1 regression.}
83 | \item{rank1}{numeric rank of the model matrix for the stage-1 regression.}
84 | \item{coefficients1}{matrix of coefficients from the stage-1 regression.}
85 | \item{df.residual1}{residual degrees of freedom for the stage-1 regression.}
86 | \item{exogenous}{columns of the \code{"regressors"} matrix that are exogenous.}
87 | \item{endogenous}{columns of the \code{"regressors"} matrix that are endogenous.}
88 | \item{instruments}{columns of the \code{"instruments"} matrix that are
89 | instruments for the endogenous variables.}
90 | \item{method}{the method used for the stage 1 and 2 regressions, one of \code{"OLS"},
91 | \code{"M"}, or \code{"MM"}.}
92 | \item{rweights}{a matrix of robustness weights with columns for each of the stage-1
93 | regressions and for the stage-2 regression (in the last column) if the fitting method is
94 | \code{"M"} or \code{"MM"}, \code{NULL} if the fitting method is \code{"OLS"}.}
95 | \item{hatvalues}{a matrix of hatvalues. For \code{method = "OLS"}, the matrix consists of two
96 | columns, for each of the stage-1 and stage-2 regression; for \code{method = "M"} or \code{"MM"},
97 | there is one column for \emph{each} stage=1 regression and for the stage-2 regression. }
98 | \item{df.residual}{residual degrees of freedom for fitted model.}
99 | \item{call}{the original function call.}
100 | \item{formula}{the model formula.}
101 | \item{na.action}{function applied to missing values in the model fit.}
102 | \item{terms}{a list with elements \code{"regressors"} and \code{"instruments"}
103 | containing the terms objects for the respective components.}
104 | \item{levels}{levels of the categorical regressors.}
105 | \item{contrasts}{the contrasts used for categorical regressors.}
106 | \item{model}{the full model frame (if \code{model = TRUE}).}
107 | \item{y}{the response vector (if \code{y = TRUE}).}
108 | \item{x}{a list with elements \code{"regressors"}, \code{"instruments"}, \code{"projected"},
109 | containing the model matrices from the respective components (if \code{x = TRUE}).
110 | \code{"projected"} is the matrix of regressors projected on the image of the instruments.}
111 | }
112 | \description{
113 | Fit instrumental-variable regression by two-stage least squares (2SLS). This is
114 | equivalent to direct instrumental-variables estimation when the number of
115 | instruments is equal to the number of regressors. Alternative robust-regression
116 | estimators are also provided, based on M-estimation (2SM) and MM-estimation
117 | (2SMM).
118 | }
119 | \details{
120 | \code{ivreg} is the high-level interface to the work-horse function
121 | \code{\link{ivreg.fit}}. A set of standard methods (including \code{print},
122 | \code{summary}, \code{vcov}, \code{anova}, \code{predict}, \code{residuals},
123 | \code{terms}, \code{model.matrix}, \code{bread}, \code{estfun}) is available
124 | and described in \code{\link{ivregMethods}}. For methods related to regression
125 | diagnostics, see \code{\link{ivregDiagnostics}}.
126 |
127 | Regressors and instruments for \code{ivreg} are most easily specified in a
128 | formula with two parts on the right-hand side, e.g., \code{y ~ x1 + x2 | z1
129 | + z2 + z3}, where \code{x1} and \code{x2} are the explanatory variables and \code{z1},
130 | \code{z2}, and \code{z3} are the instrumental variables. Note that exogenous regressors
131 | have to be included as instruments for themselves.
132 |
133 | For example, if there is
134 | one exogenous regressor \code{ex} and one endogenous regressor \code{en}
135 | with instrument \code{in}, the appropriate formula would be \code{y ~ en +
136 | ex | in + ex}. Alternatively, a formula with three parts on the right-hand
137 | side can also be used: \code{y ~ ex | en | in}. The latter is typically more convenient, if
138 | there is a large number of exogenous regressors.
139 |
140 | Moreover, two further equivalent specification strategies are possible that are
141 | typically less convenient compared to the strategies above. One option is to use
142 | an update formula with a \code{.} in the second part of the formula is used:
143 | \code{y ~ en + ex | . - en + in}. Another option is to use a separate formula
144 | for the instruments (only for backward compatibility with earlier versions):
145 | \code{formula = y ~ en + ex, instruments = ~ in + ex}.
146 |
147 | Internally, all specifications are converted to the version with two parts
148 | on the right-hand side.
149 | }
150 | \examples{
151 | \dontshow{ if(!requireNamespace("sandwich")) {
152 | if(interactive() || is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA))) {
153 | stop("not all packages required for the example are installed")
154 | } else q() }}
155 | ## data
156 | data("CigaretteDemand", package = "ivreg")
157 |
158 | ## model
159 | m <- ivreg(log(packs) ~ log(rprice) + log(rincome) | salestax + log(rincome),
160 | data = CigaretteDemand)
161 | summary(m)
162 | summary(m, vcov = sandwich::sandwich, df = Inf)
163 |
164 | ## ANOVA
165 | m2 <- update(m, . ~ . - log(rincome) | . - log(rincome))
166 | anova(m, m2)
167 | car::Anova(m)
168 |
169 | ## same model specified by formula with three-part right-hand side
170 | ivreg(log(packs) ~ log(rincome) | log(rprice) | salestax, data = CigaretteDemand)
171 |
172 | # Robust 2SLS regression
173 | data("Kmenta", package = "ivreg")
174 | Kmenta1 <- Kmenta
175 | Kmenta1[20, "Q"] <- 95 # corrupted data
176 | deq <- ivreg(Q ~ P + D | D + F + A, data=Kmenta) # demand equation, uncorrupted data
177 | deq1 <- ivreg(Q ~ P + D | D + F + A, data=Kmenta1) # standard 2SLS, corrupted data
178 | deq2 <- ivreg(Q ~ P + D | D + F + A, data=Kmenta1, subset=-20) # standard 2SLS, removing bad case
179 | deq3 <- ivreg(Q ~ P + D | D + F + A, data=Kmenta1, method="MM") # 2SLS MM estimation
180 | car::compareCoefs(deq, deq1, deq2, deq3)
181 | round(deq3$rweights, 2) # robustness weights
182 |
183 | }
184 | \references{
185 | Greene, W.H. (2003) \emph{Econometric Analysis}, 5th ed., Upper Saddle River: Prentice Hall.
186 | }
187 | \seealso{
188 | \code{\link{ivreg.fit}}, \code{\link{ivregDiagnostics}}, \code{\link{ivregMethods}},
189 | \code{\link[stats]{lm}}, \code{\link[stats:lmfit]{lm.fit}}
190 | }
191 | \keyword{regression}
192 |
--------------------------------------------------------------------------------
/man/ivreg.fit.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ivreg.fit.R
3 | \name{ivreg.fit}
4 | \alias{ivreg.fit}
5 | \title{Fitting Instrumental-Variable Regressions by 2SLS, 2SM, or 2SMM Estimation}
6 | \usage{
7 | ivreg.fit(
8 | x,
9 | y,
10 | z,
11 | weights,
12 | offset,
13 | method = c("OLS", "M", "MM"),
14 | rlm.args = list(),
15 | ...
16 | )
17 | }
18 | \arguments{
19 | \item{x}{regressor matrix.}
20 |
21 | \item{y}{vector for the response variable.}
22 |
23 | \item{z}{instruments matrix.}
24 |
25 | \item{weights}{an optional vector of weights to be used in the fitting
26 | process.}
27 |
28 | \item{offset}{an optional offset that can be used to specify an a priori
29 | known component to be included during fitting.}
30 |
31 | \item{method}{the method used to fit the stage 1 and 2 regression:
32 | \code{"OLS"} for traditional 2SLS regression (the default),
33 | \code{"M"} for M-estimation, or \code{"MM"} for MM-estimation, with the
34 | latter two robust-regression methods implemented via the \code{\link[MASS]{rlm}}
35 | function in the \pkg{MASS} package.}
36 |
37 | \item{rlm.args}{a list of optional arguments to be passed to the \code{\link[MASS]{rlm}}
38 | function in the \pkg{MASS} package if robust regression is used for the stage 1 and 2 regressions.}
39 |
40 | \item{\dots}{further arguments passed to \code{\link[stats:lmfit]{lm.fit}}
41 | or \code{\link[stats:lmfit]{lm.wfit}}, respectively.}
42 | }
43 | \value{
44 | \code{ivreg.fit} returns an unclassed list with the following
45 | components:
46 | \item{coefficients}{parameter estimates, from the stage-2 regression.}
47 | \item{residuals}{vector of model residuals.}
48 | \item{residuals1}{matrix of residuals from the stage-1 regression.}
49 | \item{residuals2}{vector of residuals from the stage-2 regression.}
50 | \item{fitted.values}{vector of predicted means for the response.}
51 | \item{weights}{either the vector of weights used (if any) or \code{NULL} (if none).}
52 | \item{offset}{either the offset used (if any) or \code{NULL} (if none).}
53 | \item{estfun}{a matrix containing the empirical estimating functions.}
54 | \item{n}{number of observations.}
55 | \item{nobs}{number of observations with non-zero weights.}
56 | \item{p}{number of columns in the model matrix x of regressors.}
57 | \item{q}{number of columns in the instrumental variables model matrix z}
58 | \item{rank}{numeric rank of the model matrix for the stage-2 regression.}
59 | \item{df.residual}{residual degrees of freedom for fitted model.}
60 | \item{cov.unscaled}{unscaled covariance matrix for the coefficients.}
61 | \item{sigma}{residual standard error; when method is \code{"M"} or \code{"MM"}, this
62 | is based on the MAD of the residuals (around 0) --- see \code{\link[stats]{mad}}.}
63 | \item{x}{projection of x matrix onto span of z.}
64 | \item{qr}{QR decomposition for the stage-2 regression.}
65 | \item{qr1}{QR decomposition for the stage-1 regression.}
66 | \item{rank1}{numeric rank of the model matrix for the stage-1 regression.}
67 | \item{coefficients1}{matrix of coefficients from the stage-1 regression.}
68 | \item{df.residual1}{residual degrees of freedom for the stage-1 regression.}
69 | \item{exogenous}{columns of the \code{"regressors"} matrix that are exogenous.}
70 | \item{endogenous}{columns of the \code{"regressors"} matrix that are endogenous.}
71 | \item{instruments}{columns of the \code{"instruments"} matrix that are
72 | instruments for the endogenous variables.}
73 | \item{method}{the method used for the stage 1 and 2 regressions, one of \code{"OLS"},
74 | \code{"M"}, or \code{"MM"}.}
75 | \item{rweights}{a matrix of robustness weights with columns for each of the stage-1
76 | regressions and for the stage-2 regression (in the last column) if the fitting method is
77 | \code{"M"} or \code{"MM"}, \code{NULL} if the fitting method is \code{"OLS"}.}
78 | \item{hatvalues}{a matrix of hatvalues. For \code{method = "OLS"}, the matrix consists of two
79 | columns, for each of the stage-1 and stage-2 regression; for \code{method = "M"} or \code{"MM"},
80 | there is one column for \emph{each} stage-1 regression and for the stage-2 regression. }
81 | }
82 | \description{
83 | Fit instrumental-variable regression by two-stage least squares (2SLS). This is
84 | equivalent to direct instrumental-variables estimation when the number of
85 | instruments is equal to the number of predictors. Alternative robust-regression
86 | estimation is also supported, based on M-estimation (22M) or MM-estimation (2SMM).
87 | }
88 | \details{
89 | \code{\link{ivreg}} is the high-level interface to the work-horse function
90 | \code{ivreg.fit}. \code{ivreg.fit} is essentially a convenience interface to
91 | \code{\link[stats:lmfit]{lm.fit}} (or \code{\link[stats:lmfit]{lm.wfit}})
92 | for first projecting \code{x} onto the image of
93 | \code{z}, then running a regression of \code{y} on the projected
94 | \code{x}, and computing the residual standard deviation.
95 | }
96 | \examples{
97 | ## data
98 | data("CigaretteDemand", package = "ivreg")
99 |
100 | ## high-level interface
101 | m <- ivreg(log(packs) ~ log(rprice) + log(rincome) | salestax + log(rincome),
102 | data = CigaretteDemand)
103 |
104 | ## low-level interface
105 | y <- m$y
106 | x <- model.matrix(m, component = "regressors")
107 | z <- model.matrix(m, component = "instruments")
108 | ivreg.fit(x, y, z)$coefficients
109 |
110 | }
111 | \seealso{
112 | \code{\link{ivreg}}, \code{\link[stats:lmfit]{lm.fit}},
113 | \code{\link[stats:lmfit]{lm.wfit}}, \code{\link[MASS]{rlm}}, \code{\link[stats]{mad}}
114 | }
115 | \keyword{regression}
116 |
--------------------------------------------------------------------------------
/man/ivregDiagnostics.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ivregDiagnostics.R
3 | \name{influence.ivreg}
4 | \alias{influence.ivreg}
5 | \alias{ivregDiagnostics}
6 | \alias{rstudent.ivreg}
7 | \alias{cooks.distance.ivreg}
8 | \alias{dfbeta.influence.ivreg}
9 | \alias{hatvalues.ivreg}
10 | \alias{rstudent.influence.ivreg}
11 | \alias{hatvalues.influence.ivreg}
12 | \alias{cooks.distance.influence.ivreg}
13 | \alias{qqPlot.ivreg}
14 | \alias{qqPlot.influence.ivreg}
15 | \alias{influencePlot.ivreg}
16 | \alias{influencePlot.influence.ivreg}
17 | \alias{infIndexPlot.ivreg}
18 | \alias{infIndexPlot.influence.ivreg}
19 | \alias{model.matrix.influence.ivreg}
20 | \alias{avPlots.ivreg}
21 | \alias{avPlot.ivreg}
22 | \alias{mcPlots.ivreg}
23 | \alias{mcPlot.ivreg}
24 | \alias{Boot.ivreg}
25 | \alias{crPlots.ivreg}
26 | \alias{crPlot.ivreg}
27 | \alias{ceresPlots.ivreg}
28 | \alias{ceresPlot.ivreg}
29 | \alias{plot.ivreg}
30 | \alias{outlierTest.ivreg}
31 | \alias{spreadLevelPlot.ivreg}
32 | \alias{ncvTest.ivreg}
33 | \alias{deviance.ivreg}
34 | \alias{dfbeta.ivreg}
35 | \alias{influence.rivreg}
36 | \title{Deletion and Other Diagnostic Methods for \code{"ivreg"} Objects}
37 | \usage{
38 | \method{influence}{ivreg}(
39 | model,
40 | sigma. = n <= 1000,
41 | type = c("stage2", "both", "maximum"),
42 | applyfun = NULL,
43 | ncores = NULL,
44 | ...
45 | )
46 |
47 | \method{rstudent}{ivreg}(model, ...)
48 |
49 | \method{cooks.distance}{ivreg}(model, ...)
50 |
51 | \method{dfbeta}{influence.ivreg}(model, ...)
52 |
53 | \method{dfbeta}{ivreg}(model, ...)
54 |
55 | \method{hatvalues}{ivreg}(model, type = c("stage2", "both", "maximum", "stage1"), ...)
56 |
57 | \method{rstudent}{influence.ivreg}(model, ...)
58 |
59 | \method{hatvalues}{influence.ivreg}(model, ...)
60 |
61 | \method{cooks.distance}{influence.ivreg}(model, ...)
62 |
63 | \method{qqPlot}{influence.ivreg}(
64 | x,
65 | ylab = paste("Studentized Residuals(", deparse(substitute(x)), ")", sep = ""),
66 | distribution = c("t", "norm"),
67 | ...
68 | )
69 |
70 | \method{influencePlot}{ivreg}(model, ...)
71 |
72 | \method{influencePlot}{influence.ivreg}(model, ...)
73 |
74 | \method{infIndexPlot}{ivreg}(model, ...)
75 |
76 | \method{infIndexPlot}{influence.ivreg}(model, ...)
77 |
78 | \method{model.matrix}{influence.ivreg}(object, ...)
79 |
80 | \method{avPlots}{ivreg}(model, terms, ...)
81 |
82 | \method{avPlot}{ivreg}(model, ...)
83 |
84 | \method{mcPlots}{ivreg}(model, terms, ...)
85 |
86 | \method{mcPlot}{ivreg}(model, ...)
87 |
88 | \method{Boot}{ivreg}(
89 | object,
90 | f = coef,
91 | labels = names(f(object)),
92 | R = 999,
93 | method = "case",
94 | ncores = 1,
95 | ...
96 | )
97 |
98 | \method{crPlots}{ivreg}(model, terms, ...)
99 |
100 | \method{crPlot}{ivreg}(model, ...)
101 |
102 | \method{ceresPlots}{ivreg}(model, terms, ...)
103 |
104 | \method{ceresPlot}{ivreg}(model, ...)
105 |
106 | \method{plot}{ivreg}(x, ...)
107 |
108 | \method{qqPlot}{ivreg}(x, distribution = c("t", "norm"), ...)
109 |
110 | \method{outlierTest}{ivreg}(model, ...)
111 |
112 | \method{spreadLevelPlot}{ivreg}(x, main = "Spread-Level Plot", ...)
113 |
114 | \method{ncvTest}{ivreg}(model, ...)
115 |
116 | \method{deviance}{ivreg}(object, ...)
117 |
118 | \method{influence}{rivreg}(model, ...)
119 | }
120 | \arguments{
121 | \item{model, x, object}{A \code{"ivreg"} or \code{"influence.ivreg"} object.}
122 |
123 | \item{sigma.}{If \code{TRUE} (the default for 1000 or fewer cases), the deleted value
124 | of the residual standard deviation is computed for each case; if \code{FALSE}, the
125 | overall residual standard deviation is used to compute other deletion diagnostics.}
126 |
127 | \item{type}{If \code{"stage2"} (the default), hatvalues are for the second stage regression;
128 | if \code{"both"}, the hatvalues are the geometric mean of the casewise hatvalues for the
129 | two stages; if \code{"maximum"}, the hatvalues are the larger of the casewise
130 | hatvalues for the two stages. In computing the geometric mean or casewise maximum hatvalues,
131 | the hatvalues for each stage are first divided by their average (number of coefficients in
132 | stage regression/number of cases); the geometric mean or casewise maximum values are then
133 | multiplied by the average hatvalue from the second stage.}
134 |
135 | \item{applyfun}{Optional loop replacement function that should work like
136 | \code{\link[base]{lapply}} with arguments \code{function(X, FUN, ...)}. The default
137 | is to use a loop unless the \code{ncores} argument is specified (see below).}
138 |
139 | \item{ncores}{Numeric, number of cores to be used in parallel computations. If set
140 | to an integer the \code{applyfun} is set to use either \code{\link[parallel:clusterApply]{parLapply}}
141 | (on Windows) or
142 | #ifdef windows
143 | \code{\link[parallel:mcdummies]{mclapply}}
144 | #endif
145 | #ifdef unix
146 | \code{\link[parallel]{mclapply}}
147 | #endif
148 | (otherwise) with the desired number of cores.}
149 |
150 | \item{...}{arguments to be passed down.}
151 |
152 | \item{ylab}{The vertical axis label.}
153 |
154 | \item{distribution}{\code{"t"} (the default) or \code{"norm"}.}
155 |
156 | \item{terms}{Terms for which added-variable plots are to be constructed; the default,
157 | if the argument isn't specified, is the \code{"regressors"} component of the model formula.}
158 |
159 | \item{f, labels, R}{see \code{\link[car]{Boot}}.}
160 |
161 | \item{method}{only \code{"case"} (case resampling) is supported: see \code{\link[car]{Boot}}.}
162 |
163 | \item{main}{Main title for the graph.}
164 | }
165 | \value{
166 | In the case of \code{influence.ivreg}, an object of class \code{"influence.ivreg"}
167 | with the following components:
168 | \describe{
169 | \item{\code{coefficients}}{the estimated regression coefficients}
170 | \item{\code{model}}{the model matrix}
171 | \item{\code{dfbeta}}{influence on coefficients}
172 | \item{\code{sigma}}{deleted values of the residual standard deviation}
173 | \item{\code{dffits}}{overall influence on the regression coefficients}
174 | \item{\code{cookd}}{Cook's distances}
175 | \item{\code{hatvalues}}{hatvalues}
176 | \item{\code{rstudent}}{Studentized residuals}
177 | \item{\code{df.residual}}{residual degrees of freedom}
178 | }
179 | In the case of other methods, such as \code{rstudent.ivreg} or
180 | \code{rstudent.influence.ivreg}, the corresponding diagnostic statistics.
181 | Many other methods (e.g., \code{crPlot.ivreg}, \code{avPlot.ivreg}, \code{Effect.ivreg})
182 | draw graphs.
183 | }
184 | \description{
185 | Methods for computing deletion and other regression diagnostics for 2SLS regression.
186 | It's generally more efficient to compute the deletion diagnostics via the \code{influence}
187 | method and then to extract the various specific diagnostics with the methods for
188 | \code{"influence.ivreg"} objects. Other diagnostics for linear models, such as
189 | added-variable plots (\code{\link[car]{avPlots}}) and component-plus-residual
190 | plots (\code{\link[car]{crPlots}}), also work, as do effect plots
191 | (e.g., \code{\link[effects]{predictorEffects}}) with residuals (see the examples below).
192 | The pointwise confidence envelope for the \code{\link[car]{qqPlot}} method assumes an independent random sample
193 | from the t distribution with degrees of freedom equal to the residual degrees of
194 | freedom for the model and so are approximate, because the studentized residuals aren't
195 | independent.
196 |
197 | For additional information, see the vignette
198 | \href{../doc/Diagnostics-for-2SLS-Regression.pdf}{Diagnostics for 2SLS Regression}.
199 | }
200 | \examples{
201 | kmenta.eq1 <- ivreg(Q ~ P + D | D + F + A, data = Kmenta)
202 | summary(kmenta.eq1)
203 | car::avPlots(kmenta.eq1)
204 | car::mcPlots(kmenta.eq1)
205 | car::crPlots(kmenta.eq1)
206 | car::ceresPlots(kmenta.eq1)
207 | car::influencePlot(kmenta.eq1)
208 | car::influenceIndexPlot(kmenta.eq1)
209 | car::qqPlot(kmenta.eq1)
210 | car::spreadLevelPlot(kmenta.eq1)
211 | plot(effects::predictorEffects(kmenta.eq1, residuals = TRUE))
212 | set.seed <- 12321 # for reproducibility
213 | confint(car::Boot(kmenta.eq1, R = 250)) # 250 reps for brevity
214 | car::outlierTest(kmenta.eq1)
215 | car::ncvTest(kmenta.eq1)
216 |
217 | }
218 | \seealso{
219 | \code{\link{ivreg}}, \code{\link[car]{avPlots}},
220 | \code{\link[car]{crPlots}}, \code{\link[effects]{predictorEffects}},
221 | \code{\link[car]{qqPlot}}, \code{\link[car]{influencePlot}},
222 | \code{\link[car]{infIndexPlot}}, \code{\link[car]{Boot}},
223 | \code{\link[car]{outlierTest}}, \code{\link[car]{spreadLevelPlot}},
224 | \code{\link[car]{ncvTest}}.
225 | }
226 |
--------------------------------------------------------------------------------
/man/ivregMethods.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ivregMethods.R
3 | \name{coef.ivreg}
4 | \alias{coef.ivreg}
5 | \alias{ivregMethods}
6 | \alias{vcov.ivreg}
7 | \alias{bread.ivreg}
8 | \alias{estfun.ivreg}
9 | \alias{terms.ivreg}
10 | \alias{model.matrix.ivreg}
11 | \alias{predict.ivreg}
12 | \alias{print.ivreg}
13 | \alias{update.ivreg}
14 | \alias{residuals.ivreg}
15 | \alias{Effect.ivreg}
16 | \alias{formula.ivreg}
17 | \alias{find_formula.ivreg}
18 | \alias{alias.ivreg}
19 | \alias{qr.ivreg}
20 | \alias{vcovHC.ivreg}
21 | \alias{model.matrix.ivreg_projected}
22 | \alias{weights.ivreg}
23 | \title{Methods for \code{"ivreg"} Objects}
24 | \usage{
25 | \method{coef}{ivreg}(object, component = c("stage2", "stage1"), complete = TRUE, ...)
26 |
27 | \method{vcov}{ivreg}(object, component = c("stage2", "stage1"), complete = TRUE, ...)
28 |
29 | \method{bread}{ivreg}(x, ...)
30 |
31 | \method{estfun}{ivreg}(x, ...)
32 |
33 | \method{vcovHC}{ivreg}(x, ...)
34 |
35 | \method{terms}{ivreg}(x, component = c("regressors", "instruments", "full"), ...)
36 |
37 | \method{model.matrix}{ivreg}(
38 | object,
39 | component = c("regressors", "projected", "instruments"),
40 | ...
41 | )
42 |
43 | \method{model.matrix}{ivreg_projected}(object, ...)
44 |
45 | \method{predict}{ivreg}(
46 | object,
47 | newdata,
48 | type = c("response", "terms"),
49 | na.action = na.pass,
50 | se.fit = FALSE,
51 | interval = c("none", "confidence", "prediction"),
52 | df = Inf,
53 | level = 0.95,
54 | weights,
55 | ...
56 | )
57 |
58 | \method{print}{ivreg}(x, digits = max(3, getOption("digits") - 3), ...)
59 |
60 | \method{update}{ivreg}(object, formula., ..., evaluate = TRUE)
61 |
62 | \method{residuals}{ivreg}(
63 | object,
64 | type = c("response", "projected", "regressors", "working", "deviance", "pearson",
65 | "partial", "stage1"),
66 | ...
67 | )
68 |
69 | \method{Effect}{ivreg}(focal.predictors, mod, ...)
70 |
71 | \method{formula}{ivreg}(x, component = c("complete", "regressors", "instruments"), ...)
72 |
73 | \method{find_formula}{ivreg}(x, ...)
74 |
75 | \method{alias}{ivreg}(object, ...)
76 |
77 | \method{qr}{ivreg}(x, ...)
78 |
79 | \method{weights}{ivreg}(object, type = c("variance", "robustness"), ...)
80 | }
81 | \arguments{
82 | \item{object, model, mod}{An object of class \code{"ivreg"}.}
83 |
84 | \item{component}{For \code{\link{terms}}, \code{"regressors"}, \code{"instruments"}, or \code{"full"};
85 | for \code{\link{model.matrix}}, \code{"projected"}, \code{"regressors"}, or \code{"instruments"};
86 | for \code{\link{formula}}, \code{"regressors"}, \code{"instruments"}, or \code{"complete"};
87 | for \code{\link{coef}} and \code{\link{vcov}}, \code{"stage2"} or \code{"stage1"}.}
88 |
89 | \item{complete}{If \code{TRUE}, the default, the returned coefficient vector (for \code{coef}) or coefficient-covariance matrix (for \code{vcov}) includes elements for aliased regressors.}
90 |
91 | \item{...}{arguments to pass down.}
92 |
93 | \item{x}{An object of class \code{"ivreg"}.}
94 |
95 | \item{newdata}{Values of predictors for which to obtain predicted values; if missing
96 | predicted (i.e., fitted) values are computed for the data to which the model was fit.}
97 |
98 | \item{type}{For \code{predict}, one of \code{"response"} (the default) or \code{"terms"};
99 | for \code{residuals}, one of \code{"response"} (the default), \code{"projected"}, \code{"regressors"},
100 | \code{"working"}, \code{"deviance"}, \code{"pearson"}, or \code{"partial"};
101 | \code{type = "working"} and \code{"response"} are equivalent, as are
102 | \code{type = "deviance"} and \code{"pearson"}; for \code{weights}, \code{"variance"} (the default)
103 | for invariance-variance weights (which is \code{NULL} for an unweighted fit)
104 | or \code{"robustness"} for robustness weights (available for M or MM estimation).}
105 |
106 | \item{na.action}{\code{na} method to apply to predictor values for predictions; default is \code{\link{na.pass}}.}
107 |
108 | \item{se.fit}{Compute standard errors of predicted values (default \code{FALSE}).}
109 |
110 | \item{interval}{Type of interval to compute for predicted values: \code{"none"} (the default),
111 | \code{"confidence"} for confidence intervals for the expected response, or \code{"prediction"} for
112 | prediction intervals for future observations.}
113 |
114 | \item{df}{For \code{predict}, degrees of freedom for computing t-distribution confidence- or prediction-interval limits; the
115 | default, \code{Inf}, is equivalent to using the normal distribution; if \code{NULL},
116 | \code{df} is taken from the residual degrees of freedom for the model.
117 | These tests are not to be confused with the \emph{regression diagnostics} provided elsewhere in the \pkg{ivreg}
118 | package: see \code{\link{ivregDiagnostics}}.}
119 |
120 | \item{level}{for confidence or prediction intervals, default \code{0.95}.}
121 |
122 | \item{weights}{Either a numeric vector or a one-sided formula to provide weights for prediction
123 | intervals when the fit is weighted. If \code{weights} and \code{newdata} are missing, the weights
124 | are those used for fitting the model.}
125 |
126 | \item{digits}{For printing.}
127 |
128 | \item{formula.}{To update model.}
129 |
130 | \item{evaluate}{If \code{TRUE}, the default, the updated model is evaluated; if \code{FALSE} the updated call is returned.}
131 |
132 | \item{focal.predictors}{Focal predictors for effect plot, see \code{\link[effects:effect]{Effect}}.}
133 | }
134 | \description{
135 | Various methods for processing \code{"ivreg"} objects; for diagnostic methods,
136 | see \code{\link{ivregDiagnostics}}.
137 | }
138 | \seealso{
139 | \code{\link{ivreg}}, \code{\link{ivreg.fit}}, \code{\link{ivregDiagnostics}}
140 | }
141 |
--------------------------------------------------------------------------------
/man/summary.ivreg.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/summary.ivreg.R
3 | \name{confint.ivreg}
4 | \alias{confint.ivreg}
5 | \alias{summary.ivreg}
6 | \alias{print.summary.ivreg}
7 | \alias{anova.ivreg}
8 | \alias{Anova.ivreg}
9 | \alias{linearHypothesis.ivreg}
10 | \title{Summary and Inference Methods for \code{"ivreg"} Objects}
11 | \usage{
12 | \method{confint}{ivreg}(
13 | object,
14 | parm,
15 | level = 0.95,
16 | component = c("stage2", "stage1"),
17 | complete = TRUE,
18 | vcov. = NULL,
19 | df = NULL,
20 | ...
21 | )
22 |
23 | \method{summary}{ivreg}(object, vcov. = NULL, df = NULL, diagnostics = NULL, ...)
24 |
25 | \method{print}{summary.ivreg}(
26 | x,
27 | digits = max(3, getOption("digits") - 3),
28 | signif.stars = getOption("show.signif.stars"),
29 | ...
30 | )
31 |
32 | \method{anova}{ivreg}(object, object2, test = "F", vcov. = NULL, ...)
33 |
34 | \method{Anova}{ivreg}(mod, test.statistic = c("F", "Chisq"), vcov. = NULL, ...)
35 |
36 | \method{linearHypothesis}{ivreg}(
37 | model,
38 | hypothesis.matrix,
39 | rhs = NULL,
40 | test = c("F", "Chisq"),
41 | vcov. = NULL,
42 | ...
43 | )
44 | }
45 | \arguments{
46 | \item{object, object2, model, mod}{An object of class \code{"ivreg"}.}
47 |
48 | \item{parm}{parameters for which confidence intervals are to be computed; a vector or numbers or names; the default is all parameters.}
49 |
50 | \item{level}{confidence level; the default is \code{0.95}.}
51 |
52 | \item{component}{Character indicating \code{"stage2"} or \code{"stage1"}.}
53 |
54 | \item{complete}{If \code{TRUE}, the default, the returned coefficient vector (for \code{coef}) or coefficient-covariance matrix (for \code{vcov}) includes elements for aliased regressors.}
55 |
56 | \item{vcov.}{Optionally either a coefficient covariance matrix or a function to compute such a covariance
57 | matrix from fitted \code{ivreg} model objects. If \code{NULL} (the default) the standard covariance matrix
58 | (based on the information matrix) is used. Alternatively, covariance matrices (e.g., clustered and/or
59 | heteroscedasticity-consistent) can be plugged in to adjust Wald tests or confidence intervals etc.
60 | In \code{summary}, if \code{diagnostics = TRUE}, \code{vcov.} must be a function (not a matrix) because
61 | the alternative covariances are also needed for certain auxiliary models in the diagnostic tests.
62 | If \code{vcov.} is a function, the \code{...} argument can be used to pass on further arguments to
63 | this function.}
64 |
65 | \item{df}{For \code{summary}, optional residual degrees of freedom to use in computing model summary.}
66 |
67 | \item{...}{arguments to pass down.}
68 |
69 | \item{diagnostics}{Report 2SLS "diagnostic" tests in model summary (default is \code{TRUE}).
70 | These tests are not to be confused with the \emph{regression diagnostics} provided elsewhere in the \pkg{ivreg}
71 | package: see \code{\link{ivregDiagnostics}}.}
72 |
73 | \item{x}{An object of class \code{"summary.ivreg"}.}
74 |
75 | \item{digits}{Minimal number of significant digits for printing.}
76 |
77 | \item{signif.stars}{Show "significance stars" in summary output?}
78 |
79 | \item{test, test.statistic}{Test statistics for ANOVA table computed by \code{anova}, \code{\link[car]{Anova}},
80 | or \code{\link[car]{linearHypothesis}}. Only \code{test = "F"} is supported by \code{anova}; this is also
81 | the default for \code{Anova} and \code{linearHypothesis}, which also allow \code{test = "Chisq"} for
82 | asymptotic tests.}
83 |
84 | \item{hypothesis.matrix, rhs}{For formulating a linear hypothesis; see the documentation
85 | for \code{\link[car]{linearHypothesis}} for details.}
86 | }
87 | \description{
88 | Summary method, including Wald tests and (by default) certain diagnostic tests, for
89 | \code{"ivreg"} model objects, as well as other related inference functions.
90 | }
91 | \examples{
92 | \dontshow{ if(!requireNamespace("sandwich")) {
93 | if(interactive() || is.na(Sys.getenv("_R_CHECK_PACKAGE_NAME_", NA))) {
94 | stop("not all packages required for the example are installed")
95 | } else q() }}
96 | ## data and model
97 | data("CigaretteDemand", package = "ivreg")
98 | m <- ivreg(log(packs) ~ log(rincome) | log(rprice) | salestax, data = CigaretteDemand)
99 |
100 | ## summary including diagnostics
101 | summary(m)
102 |
103 | ## replicate global F test from summary (against null model) "by hand"
104 | m0 <- ivreg(log(packs) ~ 1, data = CigaretteDemand)
105 | anova(m0, m)
106 |
107 | ## or via linear hypothesis test
108 | car::linearHypothesis(m, c("log(rincome)", "log(rprice)"))
109 |
110 | ## confidence intervals
111 | confint(m)
112 |
113 | ## just the Wald tests for the coefficients
114 | library("lmtest")
115 | coeftest(m)
116 |
117 | ## plug in a heteroscedasticity-consistent HC1 covariance matrix (from sandwich)
118 | library("sandwich")
119 | ## - as a function passing additional type argument through ...
120 | coeftest(m, vcov = vcovHC, type = "HC1")
121 | ## - as a function without additional arguments
122 | hc1 <- function(object, ...) vcovHC(object, type = "HC1", ...)
123 | coeftest(m, vcov = hc1)
124 | ## - as a matrix
125 | vc1 <- vcovHC(m, type = "HC1")
126 | coeftest(m, vcov = vc1)
127 |
128 | ## in summary() with diagnostics = TRUE use one of the function specifications,
129 | ## the matrix is only possible when diagnostics = FALSE
130 | summary(m, vcov = vcovHC, type = "HC1") ## function + ...
131 | summary(m, vcov = hc1) ## function
132 | summary(m, vcov = vc1, diagnostics = FALSE) ## matrix
133 |
134 | ## in confint() and anova() any of the three specifications can be used
135 | anova(m0, m, vcov = vcovHC, type = "HC1") ## function + ...
136 | anova(m0, m, vcov = hc1) ## function
137 | anova(m0, m, vcov = vc1) ## matrix
138 |
139 | }
140 | \seealso{
141 | \code{\link{ivreg}}, \code{\link{ivreg.fit}}, \code{\link{ivregDiagnostics}}
142 | }
143 |
--------------------------------------------------------------------------------
/pkgdown/assets/logo_wide.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/assets/logo_wide.png
--------------------------------------------------------------------------------
/pkgdown/extra.css:
--------------------------------------------------------------------------------
1 | /* Edit pkgdown/extra.css to customize the theme */
2 |
3 | /* Color:
4 | * logo-purple: #8709b0;
5 | */
6 |
7 | @import url('https://fonts.googleapis.com/css?family=Roboto:300,500');
8 |
9 | body {
10 | font-family: 'Roboto', sans-serif;
11 | font-weight: 300;
12 | }
13 |
14 | /* The "default" hyperlinks: default bright blue, hover pinkish */
15 | a { color: #8709b0;}
16 |
17 | /* Font size for the content and the navigation bar (top): increase
18 | * font size by a factor of 1.3 (1.3 elements, global) */
19 | #navbar, div.contents { font-size: 1.3em; }
20 |
21 | #tocnav > ul > li > a:hover {
22 | color: #8709b0;
23 | }
24 |
25 | h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
26 | font-family: 'Roboto', sans-serif;
27 | font-weight: 500;
28 | }
29 |
30 | b, strong, th {
31 | font-weight: 500;
32 | }
33 |
34 | .navbar-brand {
35 | background-image: url('logo_wide.png');
36 | background-repeat: no-repeat;
37 | background-position: 12.5px 5px;
38 | background-size: 110px auto;
39 | width: 200px;
40 | height: 58px;
41 | }
42 |
43 | .navbar-brand > .navbar-link {
44 | opacity: 0;
45 | width: 0;
46 | height: 0;
47 | display: block;
48 | font-size: 1.7em;
49 | line-height: 1em;
50 | width: 110px;
51 | height: 30px;
52 | float: left;
53 | }
54 |
55 | /* Adjust main navigation bar (top) to match the size of the
56 | * branding/logo */
57 | .navbar-brand > .label {
58 | display: block;
59 | margin-left: 110px;
60 | margin-top: 14px;
61 | font-weight: normal;
62 | background-color: transparent;
63 | /*color: #8709b0;*/
64 | color: #777;
65 | }
66 |
67 | /* Make the logo slightly smaller to avoid line breaks
68 | * on the collapsed view */
69 | @media all and (max-width: 250px) {
70 | .navbar-brand {
71 | width: 110px;
72 | background-size: 100px;
73 | background-position: 10px 5px;
74 | height: 39px;
75 | }
76 | .navbar-brand > .navbar-link { height: 25px; width: 100px; }
77 | .navbar-brand > .label { margin-top: 10px; margin-left: 100px; }
78 | }
79 |
80 | .navbar .navbar-nav > li > a {
81 | margin: 11px 0.5em;
82 | padding: 8px 8px;
83 | }
84 | .navbar > .navbar-nav > li.dropdown.open > a,
85 | .navbar > .navbar-nav > li.dropdown.open:hover,
86 | .navbar > .navbar-nav > li.dropdown.open > a:hover {
87 | background-color: transparent;
88 | }
89 |
90 | .dropdown-menu > li {
91 | background-color: white;
92 | }
93 | .dropdown-menu > li > a:hover {
94 | color: #8709b0;
95 | }
96 | .navbar .navbar-nav > li:hover,
97 | .navbar .navbar-nav > li.dropdown:hover {
98 | background-color: #8709b0;
99 | }
100 | .navbar .navbar-nav > li > a:hover, .navbar .navbar-nav > li.dropdown > a:hover,
101 | .navbar .navbar-nav > li:hover > a, .navbar .navbar-nav > li.dropdown:hover > a {
102 | background-color: #8709b0;
103 | color: white;
104 | }
105 | .navbar .navbar-nav > .active {
106 | background-color: #ececec;
107 | }
108 | .navbar .navbar-nav > .active:hover {
109 | background-color: #8709b0;
110 | }
111 | .navbar .navbar-nav > .active:hover > *,
112 | .navbar .navbar-nav > .active > a:hover {
113 | color: white;
114 | background-color: transparent;
115 | }
116 | .navbar .navbar-nav > li.dropdown.open:hover,
117 | .navbar .navbar-nav > li.dropdown:hover {
118 | background-color: #8709b0;
119 | }
120 | .navbar .navbar-nav > li.dropdown.open:hover,
121 | .navbar .navbar-nav > li.dropdown:hover,
122 | .navbar .navbar-nav > li.dropdown > *:hover {
123 | color: white;
124 | }
125 | .navbar .navbar-nav > .active > a,
126 | .navbar .navbar-nav > .active > a:hover {
127 | background-color: transparent;
128 | }
129 |
130 |
131 | div#overview div.page-header h1 {
132 | margin-top: 22px;
133 | padding: 0;
134 | font-size: 250%;
135 | color: dimgray;
136 | }
137 |
138 | div#system-requirements th {
139 | text-align: center;
140 | }
141 |
142 | div.page-header h1 {
143 | font-size: 250%;
144 | color: dimgray;
145 | }
146 |
147 | div.page-header h4 {
148 | padding-top: 10px;
149 | font-size: 120%;
150 | }
151 |
152 | div.contents h1 {
153 | color: dimgray;
154 | font-size: 200%;
155 | padding-top: 80px;
156 | }
157 |
158 | div.contents h2 {
159 | font-size: 150%;
160 | padding-top: 70px;
161 | }
162 |
163 | div.contents h3 {
164 | font-size: 120%;
165 | padding-top: 60px;
166 | }
167 |
168 | div.contents h4 {
169 | font-size: 100%;
170 | padding-top: 60px;
171 | }
172 |
173 | pre {
174 | overflow-x: auto;
175 | font-size: 80%;
176 | line-height: 150%;
177 | }
178 |
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-120x120.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/apple-touch-icon-120x120.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-152x152.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/apple-touch-icon-152x152.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-180x180.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/apple-touch-icon-180x180.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-60x60.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/apple-touch-icon-60x60.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon-76x76.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/apple-touch-icon-76x76.png
--------------------------------------------------------------------------------
/pkgdown/favicon/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/apple-touch-icon.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/favicon-16x16.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/favicon-32x32.png
--------------------------------------------------------------------------------
/pkgdown/favicon/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/favicon/favicon.ico
--------------------------------------------------------------------------------
/pkgdown/logo-favicon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zeileis/ivreg/7105f6c095dd3dd36c2051d4f3a6573fa13c3678/pkgdown/logo-favicon.png
--------------------------------------------------------------------------------
/pkgdown/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
99 |
--------------------------------------------------------------------------------
/tests/testthat.R:
--------------------------------------------------------------------------------
1 | library("testthat")
2 | library("ivreg")
3 |
4 | test_check("ivreg")
5 |
--------------------------------------------------------------------------------
/tests/testthat/test-case-deletion.R:
--------------------------------------------------------------------------------
1 | m <- ivreg(Q ~ P + D | D + F + A, data=Kmenta)
2 | m1 <- update(m, subset = -1)
3 | test_that("dfbeta computed correctly 1", {
4 | expect_equal(dfbeta(m)[1, ], coef(m) - coef(m1))
5 | })
6 |
7 | m.ivreg <- ivreg(Q ~ P + D | P + D, data=Kmenta) # OLS
8 | m.lm <- lm(Q ~ P + D, data=Kmenta)
9 |
10 | test_that("hatvalues computed correctly", {
11 | expect_equal(as.vector(hatvalues(m.ivreg)), as.vector(hatvalues(m.lm)))
12 | })
13 |
14 | test_that("Cook's distances computed correctly", {
15 | expect_equal(cooks.distance(m.ivreg), cooks.distance(m.lm))
16 | })
17 |
18 | test_that("dfbeta computed correctly 2", {
19 | expect_equal(dfbeta(m.ivreg), dfbeta(m.lm))
20 | })
21 |
22 | Kmenta2 <- Kmenta
23 | Kmenta2$Q[c(1, 4, 10)] <- NA
24 | m.miss <- update(m, data=Kmenta2, na.action=na.exclude)
25 |
26 | test_that("deletion statistics computed correctly with na.exclude", {
27 | expect_true(all(is.na(hatvalues(m.miss)[c(1, 4, 10)])))
28 | expect_true(all(is.na(residuals(m.miss)[c(1, 4, 10)])))
29 | expect_true(all(is.na(fitted(m.miss)[c(1, 4, 10)])))
30 | expect_true(all(is.na(rstudent(m.miss)[c(1, 4, 10)])))
31 | expect_true(all(is.na(dffits(m.miss)[c(1, 4, 10)])))
32 | expect_true(all(is.na(cooks.distance(m.miss)[c(1, 4, 10)])))
33 | expect_true(all(is.na(dfbeta(m.miss)[c(1, 4, 10), ])))
34 | })
35 |
36 | m.miss.2<- update(m, data=Kmenta2)
37 | nms <- rownames(Kmenta)[-c(1, 4, 10)]
38 | test_that("rownames of deletion statistics preserved with na.omit", {
39 | expect_equal(names(hatvalues(m.miss.2)), nms)
40 | expect_equal(names(residuals(m.miss.2)), nms)
41 | expect_equal(names(fitted(m.miss.2)), nms)
42 | expect_equal(names(rstudent(m.miss.2)), nms)
43 | expect_equal(names(dffits(m.miss.2)), nms)
44 | expect_equal(names(cooks.distance(m.miss.2)), nms)
45 | expect_equal(rownames(dfbeta(m.miss.2)), nms)
46 | })
47 |
48 | m.ivreg.w <- ivreg(Q ~ P + D | P + D, weights=Q, data=Kmenta) # WLS
49 | m.lm.w <- lm(Q ~ P + D, data=Kmenta, weights=Q)
50 |
51 | test_that("hatvalues computed correctly with weights", {
52 | expect_equal(as.vector(hatvalues(m.ivreg.w)), as.vector(hatvalues(m.lm.w)))
53 | })
54 |
55 | test_that("Cook's distances computed correctly with weights", {
56 | expect_equal(cooks.distance(m.ivreg.w), cooks.distance(m.lm.w))
57 | })
58 |
59 | test_that("rstudent computed correctly", {
60 | expect_equal(rstudent(m.ivreg), rstudent(m.lm))
61 | })
62 |
63 | mw <- ivreg(Q ~ P + D | D + F + A, weights=Q, data=Kmenta)
64 | m1w <- update(mw, subset = -10)
65 | test_that("dfbeta computed correctly with weights", {
66 | expect_equal(dfbeta(mw)[10, ], coef(mw) - coef(m1w))
67 | })
68 |
69 | test_that("rstudent computed correctly with weights", {
70 | expect_equal(rstudent(m.ivreg.w), rstudent(m.lm.w))
71 | })
72 |
73 | test_that("influence measures computed correctly in paralllel", {
74 | expect_equal(influence(m), influence(m, ncores=2))
75 | })
76 |
77 |
--------------------------------------------------------------------------------
/tests/testthat/test-ivreg.R:
--------------------------------------------------------------------------------
1 | m.fitivreg <- with(Kmenta, ivreg.fit(cbind(1, P, D), Q, cbind(1, D, F, A)))
2 | m.ivreg <- ivreg(Q ~ P + D | D + F + A, data=Kmenta)
3 |
4 | test_that("2SLS coefficients are computed correctly", {
5 | expect_equal(as.vector(coef(m.fitivreg)), as.vector(coef(m.ivreg)))
6 | })
7 |
8 | test_that("2SLS coefficient covariance matrix is computed correctly", {
9 | expect_equal(as.vector(m.fitivreg$sigma^2*m.fitivreg$cov.unscaled), as.vector(vcov(m.ivreg)))
10 | })
11 |
--------------------------------------------------------------------------------
/tests/testthat/test-ivreg.fit.R:
--------------------------------------------------------------------------------
1 |
2 | m.ivreg <- with(Kmenta, ivreg.fit(cbind(1, P, D), Q, cbind(1, D, F, A)))
3 | m.stage1 <- lm(cbind(1, P, D) ~ cbind(1, D, F, A) - 1, data=Kmenta)
4 | m.stage2 <- lm(Kmenta$Q ~ fitted(m.stage1) - 1)
5 |
6 |
7 | test_that("2SLS coefficients are computed correctly", {
8 | expect_equal(as.vector(coef(m.ivreg)), as.vector(coef(m.stage2)))
9 | })
10 |
11 | se.ivreg <- as.vector(sqrt(diag(m.ivreg$sigma^2*m.ivreg$cov.unscaled)))
12 | se <- as.vector(sqrt(diag(vcov(m.stage2))))
13 | residuals <- as.vector(with(Kmenta, Q - cbind(1, P, D) %*% coef(m.stage2)))
14 | se <- sqrt(sum(residuals^2)/(nrow(Kmenta) - 3))*se/sigma(m.stage2)
15 |
16 | test_that("2SLS coefficient standard errors are computed correctly", {
17 | expect_equal(se.ivreg, se)
18 | })
19 |
20 | test_that("model residuals are computed correctly", {
21 | expect_equal(residuals, residuals(m.ivreg))
22 | })
23 |
24 |
25 | m.ivreg.w <- with(Kmenta, ivreg.fit(cbind(1, P, D), Q, cbind(1, D, F, A), weights=Q))
26 | m.stage1.w <- lm(cbind(1, P, D) ~ cbind(1, D, F, A) - 1, weights=Q, data=Kmenta)
27 | m.stage2.w <- lm(Kmenta$Q ~ fitted(m.stage1.w) - 1, weights=Kmenta$Q)
28 |
29 | test_that("2SLS coefficients are computed correctly with weights", {
30 | expect_equal(as.vector(coef(m.ivreg.w)), as.vector(coef(m.stage2.w)))
31 | })
32 |
33 | se.ivreg.w <- as.vector(sqrt(diag(m.ivreg.w$sigma^2*m.ivreg.w$cov.unscaled)))
34 | se.w <- as.vector(sqrt(diag(vcov(m.stage2.w))))
35 | residuals <- as.vector(with(Kmenta, Q - cbind(1, P, D) %*% coef(m.stage2.w)))
36 | se.w <- sqrt(sum(Kmenta$Q*residuals^2)/(nrow(Kmenta) - 3))*se.w/sigma(m.stage2.w)
37 |
38 | test_that("2SLS coefficient standard errors are computed correctly with weights", {
39 | expect_equal(se.ivreg.w, se.w)
40 | })
41 |
--------------------------------------------------------------------------------
/tests/testthat/test-predict.R:
--------------------------------------------------------------------------------
1 | deq <- ivreg(Q ~ P + D | D + F + A, data=Kmenta)
2 | new <- expand.grid(P = seq(90, 110, by=20), D = seq(75, 125, by=20),
3 | F=c(70, 110), A=c(1, 20))
4 | pr <- predict(deq, newdata=new, se.fit=TRUE, interval="prediction")
5 | X <- as.matrix(cbind(1, new[, c("P", "D")]))
6 | yhat <- X %*% coef(deq)
7 | se <- sqrt(diag(X %*% vcov(deq) %*% t(X)))
8 | t <- qt(.025, Inf, lower.tail=FALSE)
9 | lower <- yhat - t*sqrt(se^2 + sigma(deq)^2)
10 | upper <- yhat + t*sqrt(se^2 + sigma(deq)^2)
11 | test_that("predicted values, their std. errors, and prediction intervals are computed correctly", {
12 | expect_equal(as.vector(cbind(yhat, se, lower, upper)), as.vector(pr))
13 | })
14 |
--------------------------------------------------------------------------------
/tests/testthat/test-robust-2SLS.R:
--------------------------------------------------------------------------------
1 | if (require(MASS)){
2 |
3 | Kmenta1 <- Kmenta
4 | Kmenta1[20, "Q"] <- 95 # corrupted data
5 | deqr1 <- ivreg(Q ~ P + D | D + F + A, data=Kmenta1, method="MM")
6 | stg1 <- rlm(P ~ D + F + A, data=Kmenta1, method="MM")
7 | Kmenta1$P.hat <- fitted(stg1)
8 | stg2 <- rlm(Q ~ P.hat + D, data=Kmenta1, method="MM")
9 |
10 | test_that("robust 2SLS coefficients are computed correctly", {
11 | expect_equal(coef(deqr1), coef(stg2), check.attributes=FALSE)
12 | })
13 |
14 | test_that("hatvalues are computed correctly for robust 2SLS, stage 2",{
15 | hats <- c(hatvalues(lm(residuals(stg2) ~ model.matrix(stg2) - 1, weights=stg2$w)), 0)
16 | names(hats)[20] <- "1941"
17 | expect_equal(hats, hatvalues(deqr1))
18 | })
19 |
20 | test_that("hatvalues are computed correctly for robust 2SLS, stage 1",{
21 | expect_equal(hatvalues(stg1), hatvalues(deqr1, type="stage1"))
22 | })
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/tests/testthat/test-stage1.R:
--------------------------------------------------------------------------------
1 | set.seed(123)
2 | z1 <- rnorm(100)
3 | z2 <- rnorm(100)
4 | z3 <- rnorm(100)
5 | x1 <- z1 + z2 + rnorm(100)
6 | x2 <- z2 + z3 + rnorm(100)
7 | y <- 10 + z1 + x1 + x2 + rnorm(100)
8 |
9 | D <- data.frame(z1, z2, z3, x1, x2, y, const=1)
10 |
11 | m1 <- ivreg(y ~ z1 + x1 + x2 | z1 + z2 + z3, data=D)
12 | ml1 <- lm(cbind(x1, x2) ~ z1 + z2 + z3, data=D)
13 |
14 | test_that("stage1 coefficients are computed correctly", {
15 | expect_equal(as.vector(coef(m1, component="stage1")), as.vector(coef(ml1)))
16 | })
17 |
18 | test_that("stage1 coefficient covariances are computed correctly", {
19 | expect_equal(vcov(m1, component="stage1"), vcov(ml1))
20 | })
21 |
22 | m2 <- ivreg(y ~ z1 + x1 + x2 + const | z1 + z2 + z3 + const, data=D)
23 | ml2 <- lm(cbind(x1, x2) ~ z1 + z2 + z3 + const, data=D)
24 |
25 | test_that("stage1 rank-deficient coefficients are computed correctly", {
26 | expect_equal(as.vector(coef(m2, component="stage1")), as.vector(coef(ml2)))
27 | })
28 |
29 | test_that("stage1 rank-deficient coefficient covariances are computed correctly", {
30 | expect_equal(vcov(m2, component="stage1"), vcov(ml2))
31 | })
32 |
33 | test_that("stage1 rank-deficient coefficients are computed correctly, complete=FALSE", {
34 | expect_equal(as.vector(coef(m2, component="stage1", complete=FALSE)),
35 | as.vector(coef(ml2, complete=FALSE)))
36 | })
37 |
38 | test_that("stage1 rank-deficient coefficient covariances are computed correctly, rank=FALSE", {
39 | expect_equal(vcov(m2, component="stage1", complete=FALSE),
40 | vcov(ml2, complete=FALSE))
41 | })
42 |
43 |
44 | y2 <- 10 + z1 + x1 + rnorm(100)
45 |
46 | D2 <- data.frame(z1, z2, z3, x1, y, const=1)
47 |
48 | m3 <- ivreg(y2 ~ z1 + x1 | z1 + z2 + z3, data=D2)
49 | ml3 <- lm(x1 ~ z1 + z2 + z3, data=D)
50 |
51 | test_that("stage1 coefficients are computed correctly with one endog x", {
52 | expect_equal(as.vector(coef(m3, component="stage1")), as.vector(coef(ml3)))
53 | })
54 |
55 | test_that("stage1 coefficient covariances are computed correctly with one endog x", {
56 | expect_equal(vcov(m3, component="stage1"), vcov(ml3))
57 | })
58 |
59 | m4 <- ivreg(y2 ~ z1 + x1 + const | z1 + z2 + z3 + const, data=D)
60 | ml4 <- lm(x1 ~ z1 + z2 + z3 + const, data=D)
61 |
62 | test_that("stage1 rank-deficient coefficients are computed correctly with one endog x", {
63 | expect_equal(as.vector(coef(m4, component="stage1")), as.vector(coef(ml4)))
64 | })
65 |
66 | test_that("stage1 rank-deficient coefficient covariances are computed correctly with one endog x", {
67 | expect_equal(vcov(m4, component="stage1"), vcov(ml4))
68 | })
69 |
70 | test_that("stage1 rank-deficient coefficients are computed correctly with one endog x, complete=FALSE", {
71 | expect_equal(as.vector(coef(m4, component="stage1", complete=FALSE)),
72 | as.vector(coef(ml4, complete=FALSE)))
73 | })
74 |
75 | test_that("stage1 rank-deficient coefficient covariances are computed correctly with one endog x, rank=FALSE", {
76 | expect_equal(vcov(m4, component="stage1", complete=FALSE),
77 | vcov(ml4, complete=FALSE))
78 | })
79 |
--------------------------------------------------------------------------------
/tests/testthat/test-vcov.R:
--------------------------------------------------------------------------------
1 | ## packages
2 | library("ivreg")
3 | library("lmtest")
4 | library("sandwich")
5 |
6 | ## data and models: full vs. trivial
7 | data("CigaretteDemand", package = "ivreg")
8 | m <- ivreg(log(packs) ~ log(rincome) | log(rprice) | salestax, data = CigaretteDemand)
9 | m0 <- ivreg(log(packs) ~ 1, data = CigaretteDemand)
10 |
11 | ## heteroscedasticity-consistent HC1 covariance _matrix_ on full model
12 | vc1 <- vcovHC(m, type = "HC1")
13 |
14 | ## custom HC1 covariance _function_
15 | hc1 <- function(object, ...) vcovHC(object, type = "HC1", ...)
16 |
17 | ## confint
18 | test_that("confidence intervals are equal with vcov function and matrix", {
19 | expect_equal(confint(m, vcov = hc1), confint(m, vcov = vc1))
20 | })
21 | test_that("confidence intervals are equal with vcov function and function + ...", {
22 | expect_equal(confint(m, vcov = hc1), confint(m, vcov = vcovHC, type = "HC1"))
23 | })
24 |
25 | ## coeftest
26 | test_that("confidence intervals are equal with vcov function and matrix", {
27 | expect_equal(coeftest(m, vcov = hc1), coeftest(m, vcov = vc1))
28 | })
29 | test_that("coefficient tests are equal with vcov function and function + ...", {
30 | expect_equal(coeftest(m, vcov = hc1), coeftest(m, vcov = vcovHC, type = "HC1"))
31 | })
32 |
33 | ## anova
34 | test_that("analysis of variance is equal with vcov function and matrix", {
35 | expect_equal(anova(m0, m, vcov = hc1), anova(m0, m, vcov = vc1))
36 | })
37 | test_that("analysis of variance is equal with vcov function and function + ...", {
38 | expect_equal(anova(m0, m, vcov = hc1), anova(m0, m, vcov = vcovHC, type = "HC1"))
39 | })
40 |
41 | ## summary
42 | test_that("model summary (w/o diagnostics) is equal with vcov function and matrix", {
43 | expect_equal(summary(m, vcov = hc1, diagnostics = FALSE), summary(m, vcov = vc1, diagnostics = FALSE))
44 | })
45 | test_that("model summary (w/o diagnostics) is equal with vcov function and function + ...", {
46 | expect_equal(summary(m, vcov = hc1, diagnostics = FALSE), summary(m, vcov = vcovHC, type = "HC1", diagnostics = FALSE))
47 | })
48 | test_that("model summary is equal with vcov function and function + ...", {
49 | expect_equal(summary(m, vcov = hc1), summary(m, vcov = vcovHC, type = "HC1"))
50 | })
51 |
--------------------------------------------------------------------------------
/vignettes/.gitignore:
--------------------------------------------------------------------------------
1 | *.html
2 | *.R
3 |
--------------------------------------------------------------------------------
/vignettes/ivreg.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "ivreg: Two-Stage Least-Squares Regression with Diagnostics"
3 | author: "John Fox, Christian Kleiber, Achim Zeileis"
4 | date: "last modified: 2024-04-20"
5 | output:
6 | rmarkdown::html_vignette:
7 | highlight: monochrome
8 | toc: true
9 | vignette: >
10 | %\VignetteIndexEntry{ivreg: Two-Stage Least-Squares Regression with Diagnostics}
11 | %\VignetteEngine{knitr::rmarkdown}
12 | %\VignetteDepends{ivreg,car,effects,sandwich,modelsummary}
13 | %\VignetteKeywords{two-stage least squares, instrumental variables, diagnostics}
14 | %\VignettePackage{ivreg}
15 | %\VignetteEncoding{UTF-8}
16 | ---
17 |
18 | ```{r setup, include=FALSE}
19 | knitr::opts_chunk$set(echo = TRUE, fig.height=4, fig.width=4)
20 | ```
21 |
22 | ## Overview
23 |
24 | The **ivreg** package provides a comprehensive implementation of instrumental variables
25 | regression using two-stage least-squares (2SLS) estimation. The standard
26 | regression functionality (parameter estimation, inference, robust covariances,
27 | predictions, etc.) is derived from and supersedes the `ivreg()` function in the
28 | [**AER**](https://CRAN.R-project.org/package=AER) package. Additionally, various
29 | regression diagnostics are supported, including hat values, deletion diagnostics such
30 | as studentized residuals and Cook's distances; graphical diagnostics such as
31 | component-plus-residual plots and added-variable plots; and effect plots with partial
32 | residuals.
33 |
34 | In order to provide all of this functionality the **ivreg** package integrates
35 | seamlessly with other packages by providing suitable S3 methods, specifically for generic functions in the
36 | [base-R](https://www.R-project.org/) **stats** package, and in the
37 | [**car**](https://CRAN.R-project.org/package=car),
38 | [**effects**](https://CRAN.R-project.org/package=effects),
39 | [**lmtest**](https://CRAN.R-project.org/package=lmtest), and
40 | [**sandwich**](https://CRAN.R-project.org/package=sandwich) packages,
41 | among others.
42 |
43 | The package is accompanied by two online vignettes, namely this introduction and an
44 | article introducing the regression diagnostics and graphics:
45 |
46 | * [ivreg: Two-Stage Least-Squares Regression with Diagnostics](https://zeileis.github.io/ivreg/articles/ivreg.html)
47 | * [Diagnostics for 2SLS Regression](https://zeileis.github.io/ivreg/articles/Diagnostics-for-2SLS-Regression.html)
48 |
49 |
50 |
51 | ## Installation
52 |
53 | The stable release version of [**ivreg**](https://CRAN.R-project.org/package=ivreg)
54 | is hosted on the Comprehensive R Archive Network
55 | (CRAN) at and can be installed along with all
56 | dependencies via
57 |
58 | ```{r installation-cran, eval=FALSE}
59 | install.packages("ivreg", dependencies = TRUE)
60 | ```
61 |
62 | The development version of **ivreg** is hosted on GitHub at .
63 | It can be conveniently installed via the `install_github()` function in the
64 | [**remotes**](https://CRAN.R-project.org/package=remotes) package:
65 |
66 | ```{r installation-rforge, eval=FALSE}
67 | remotes::install_github("https://github.com/zeileis/ivreg/")
68 | ```
69 |
70 | ## Instrumental variables regression
71 |
72 | The main function in the **ivreg** package is `ivreg()`, which is a high-level
73 | formula interface to the work-horse `ivreg.fit()` function; both functions return
74 | a list of quantities similar to that returned by `lm()` (including coefficients, coefficient variance-covariance
75 | matrix, residuals, etc.). In the case of `ivreg()`, the returned list is of class `"ivreg"`,
76 | for which a wide range of standard methods is available, including `print()`,
77 | `summary()`, `coef()`, `vcov()`, `anova()`, `predict()`, `residuals()`, `terms()`,
78 | `model.matrix()`, `formula()`, `update()`, `hatvalues()`, `dfbeta()`, and `rstudent()`.
79 | Moreover, methods for functionality from other packages is provided,
80 | and is described in more detail in a companion vignette.
81 |
82 | Regressors and instruments for `ivreg()` are most easily specified in a
83 | formula with two parts on the right-hand side, for example, `y ~ x1 + x2 | x1 + z1 + z2`,
84 | where `x1` and `x2` are, respectively, exogenous and endogenous explanatory variables, and `x1`, `z1`, and `z2` are
85 | instrumental variables. Both components on the right-hand side of the model formula include
86 | an implied intercept, unless, as in a linear model estimated by `lm()`, the intercept is
87 | explicitly excluded via `-1`. Exogenous explanatory variables, such as `x1` in the example,
88 | must be included among the instruments. A worked example is described immediately below.
89 | As listing exogenous variables in both parts on the right-hand side of the formula
90 | may become tedious if there are many of them, an additional convenience option is to
91 | use a three-part right side like `y ~ x1 | x2 | z1 + z2`, listing the exogenous, endogenous,
92 | and instrumental variables (for the endogenous variables only), respectively.
93 |
94 |
95 | ## Illustration: Returns to schooling
96 |
97 | As an initial demonstration of the **ivreg** package, we investigate
98 | the effect of schooling on earnings in a classical model for wage determination.
99 | The data are from the United States, and are provided in the package as
100 | `SchoolingReturns`. This data set was originally studied by David Card, and was subsequently
101 | employed, as here, to illustrate 2SLS estimation in introductory econometrics textbooks. The relevant variables for this
102 | illustration are:
103 |
104 | ```{r data}
105 | data("SchoolingReturns", package = "ivreg")
106 | summary(SchoolingReturns[, 1:8])
107 | ```
108 |
109 | A standard wage equation uses a semi-logarithmic linear regression for `wage`, estimated by
110 | ordinary least squares (OLS), with years of `education` as the primary explanatory variable,
111 | adjusting for a quadratic term in labor-market `experience`, as well as for factors
112 | coding `ethnicity`, residence in a city (`smsa`), and residence in the U.S. `south`:
113 |
114 | ```{r lm}
115 | m_ols <- lm(log(wage) ~ education + poly(experience, 2) + ethnicity + smsa + south,
116 | data = SchoolingReturns)
117 | summary(m_ols)
118 | ```
119 |
120 | Thus, OLS estimation yields an estimate of `r round(100 * coef(m_ols)["education"], digits = 1)`%
121 | per year for returns to schooling. This estimate is problematic, however, because it can be argued
122 | that `education` is endogenous (and hence also `experience`, which is taken to be `age` minus
123 | `education` minus 6). We therefore use geographical proximity to a college when growing
124 | up as an exogenous instrument for `education`. Additionally, `age` is the natural
125 | exogenous instrument for `experience`, while the remaining explanatory variables can be considered
126 | exogenous and are thus used as instruments for themselves.
127 | Although it's a useful strategy to select an effective instrument or instruments for each endogenous
128 | explanatory variable, in 2SLS regression all of the instrumental variables are used to estimate all
129 | of the regression coefficients in the model.
130 |
131 | To fit this model with `ivreg()` we can simply extend the formula from `lm()` above, adding a second part after the `|` separator to specify the instrumental variables:
132 |
133 | ```{r ivreg}
134 | library("ivreg")
135 | m_iv <- ivreg(log(wage) ~ education + poly(experience, 2) + ethnicity + smsa + south |
136 | nearcollege + poly(age, 2) + ethnicity + smsa + south,
137 | data = SchoolingReturns)
138 | ```
139 |
140 | Equivalently, the same model can also be specified slightly more concisely using three parts on the
141 | right-hand side indicating the exogenous variables, the endogenous variables, and the additional
142 | instrumental variables only (in addition to the exogenous variables).
143 |
144 | ```{r ivreg-alternative, eval=FALSE}
145 | m_iv <- ivreg(log(wage) ~ ethnicity + smsa + south | education + poly(experience, 2) |
146 | nearcollege + poly(age, 2), data = SchoolingReturns)
147 | ```
148 |
149 | Both models yield the following results:
150 |
151 | ```{r ivreg-summary}
152 | summary(m_iv)
153 | ```
154 |
155 | Thus, using two-stage least squares to estimate the regression yields a much larger
156 | coefficient for the returns to schooling, namely `r round(100 * coef(m_iv)["education"], digits = 1)`% per year.
157 | Notice as well that the standard errors of the coefficients are larger for 2SLS estimation
158 | than for OLS, and that, partly as a consequence, evidence for the effects of `ethnicity`
159 | and the quadratic component of `experience` is now weak. These differences are brought
160 | out more clearly when showing coefficients and standard errors side by side, e.g., using the
161 | `compareCoefs()` function from the **car** package or the `msummary()` function from the
162 | [**modelsummary**](https://CRAN.R-project.org/package=modelsummary) package:
163 |
164 | ```{r modelsummary, message=FALSE}
165 | library("modelsummary")
166 | m_list <- list(OLS = m_ols, IV = m_iv)
167 | msummary(m_list)
168 | ```
169 |
170 | The change in coefficients and associated standard errors can also be brought out graphically
171 | using the `modelplot()` function from **modelsummary** which shows the coefficient estimates
172 | along with their 95% confidence intervals. Below we omit the intercept and experience terms
173 | as these are on a different scale than the other coefficients.
174 |
175 | ```{r modelplot, fig.height=5, fig.width = 7}
176 | modelplot(m_list, coef_omit = "Intercept|experience")
177 | ```
178 |
--------------------------------------------------------------------------------
/vignettes/ivreg.bib:
--------------------------------------------------------------------------------
1 | @Book{BelsleyKuhWelsch1980,
2 | title = {Regression Diagnostics: Identifying Influential Data and Sources of Collinearity},
3 | author = {D. A. Belsley and E. Kuh and R. E. Welsch},
4 | publisher = {John Wiley \& Sons},
5 | address = {New York},
6 | year = {1980},
7 | doi = {10.1002/0471725153},
8 | }
9 |
10 | @Article{Basmann1957,
11 | title = {A Generalized Classical Method of Linear Estimation of Coefficients in a Structural Equation},
12 | author = {R. L. Basmann},
13 | journal = {Econometrica},
14 | volume = {25},
15 | pages = {77--83},
16 | year = {1957},
17 | doi = {10.2307/1907743},
18 | }
19 |
20 | @Book{Theil1971,
21 | title = {Principles of Econometrics},
22 | author = {H. Theil},
23 | publisher = {John Wiley \& Sons},
24 | address = {New York},
25 | year = {1971},
26 | }
27 |
28 | @Article{Fox1979,
29 | title = {Simultaneous Equation Models and Two-Stage Least Squares},
30 | author = {J. Fox},
31 | journal = {Sociological Methodology},
32 | volume = {10},
33 | pages = {130--150},
34 | year = {1979},
35 | doi = {10.2307/270769},
36 | }
37 |
38 | @Book{Greene2003,
39 | title = {Econometric Analysis},
40 | author = {W. H. Greene},
41 | year = {2003},
42 | publisher = {Prentice Hall},
43 | address = {Upper Saddle River},
44 | edition = {5th},
45 | }
46 |
47 | @Article{Phillips1977,
48 | title = {Recursions for the Two-Stage Least-Squares Estimators},
49 | author = {G. D. A. Phillips},
50 | journal = {Journal of Econometrics},
51 | volume = {6},
52 | pages = {65--77},
53 | year = {1977},
54 | doi = {10.1016/0304-4076(77)90055-0},
55 | }
56 |
57 | @Article{Pregibon1981,
58 | title = {Logistic Regression Diagnostics},
59 | author = {D. Pregibon},
60 | journal = {The Annals of Statistics},
61 | volume = {9},
62 | pages = {705--724},
63 | year = {1981},
64 | doi = {10.1214/aos/1176345513},
65 | }
66 |
67 | @Book{Kmenta1986,
68 | title = {Elements of Econometrics},
69 | edition = {2nd},
70 | author = {J. Kmenta},
71 | publisher = {Macmillan},
72 | address = {New York},
73 | year = {1986},
74 | }
75 |
76 | @Article{Cook1993,
77 | title = {Exploring Partial Residual Plots},
78 | author = {R. D. Cook},
79 | journal = {Technometrics},
80 | volume = {35},
81 | pages = {351--362},
82 | year = {1993},
83 | doi = {10.1080/00401706.1993.10485350},
84 | }
85 |
86 | @Article{CookCroosDabrera1998,
87 | author = {R. D. Cook and R. Croos-Dabrera},
88 | year = {1998},
89 | title = {Partial Residual Plots in Generalized Linear Models},
90 | journal = {Journal of the American Statistical Association},
91 | volume = {93},
92 | pages = {730--739},
93 | doi = {10.1080/01621459.1998.10473725},
94 | }
95 |
96 | @Article{FoxWeisberg2018,
97 | title = {Visualizing Fit and Lack of Fit in Complex Regression Models with Predictor Effect Plots and Partial Residuals},
98 | author = {J. Fox and S. Weisberg},
99 | year = {2018},
100 | journal = {Journal of Statistical Software},
101 | volume = {87},
102 | number = {9},
103 | pages = {1--27},
104 | doi = {10.18637/jss.v087.i09},
105 | }
106 |
107 | @Book{Fox2016,
108 | title = {Applied Regression Analysis and Generalized Linear Models},
109 | edition = {3rd},
110 | author = {J. Fox},
111 | publisher = {Sage},
112 | address = {Thousand Oaks},
113 | year = {2016},
114 | }
115 |
116 | @Book{Tukey1977,
117 | author = {J. W. Tukey},
118 | year = {1977},
119 | title = {Exploratory Data Analysis},
120 | publisher = {Addison-Wesley},
121 | address = {Reading},
122 | }
123 |
124 | @Article{BreuschPagan1979,
125 | title = {A Simple Test for Heteroscedasticity and Random Coefficient Variation},
126 | author = {T. S. Breusch and A. R. Pagan},
127 | journal = {Econometrica},
128 | volume = {47},
129 | pages = {1287--1294},
130 | year = {1979},
131 | doi = {10.2307/1911963},
132 | }
133 |
134 | @Article{CookWeisberg1983,
135 | title = {Diagnostics for Heteroscedasticity in Regression},
136 | author = {R. D. Cook and S. Weisberg},
137 | journal = {Biometrika},
138 | volume = {70},
139 | pages = {1--10},
140 | year = {1983},
141 | doi = {10.1093/biomet/70.1.1},
142 | }
143 |
144 | @InProceedings{Huber1967,
145 | author = {P. J. Huber},
146 | title = {The Behavior of Maximum Likelihood Estimation under Nonstandard Conditions},
147 | editor = {L. M. LeCam and J. Neyman},
148 | booktitle = {Proceedings of the Fifth {B}erkeley Symposium on Mathematical Statistics and Probability},
149 | year = {1967},
150 | address = {Berkeley},
151 | publisher = {University of California Press},
152 | }
153 |
154 | @Article{White1980,
155 | title = {A Heteroskedasticity-Consistent Covariance Matrix Estimator and a Direct Test for Heteroskedasticity},
156 | author = {H. White},
157 | journal = {Econometrica},
158 | volume = {48},
159 | pages = {817--838},
160 | year = {1980},
161 | doi = {10.2307/1912934},
162 | }
163 |
164 | @Article{LongErvin2000,
165 | author = {J. S. Long and L. H. Ervin},
166 | title = {Using Heteroscedasticity Consistent Standard Errors in the Linear Regression Model},
167 | journal = {The American Statistician},
168 | volume = {54},
169 | pages = {217--224},
170 | year = {2000},
171 | doi = {10.1080/00031305.2000.10474549},
172 | }
173 |
174 | @Article{FoxMonette1992,
175 | author = {J. Fox and G. Monette},
176 | year = {1992},
177 | title = {Generalized Collinearity Diagnostics},
178 | journal = {Journal of the American Statistical Association},
179 | volumje = {87},
180 | pages = {178--183},
181 | doi = {10.1080/01621459.1992.10475190},
182 | }
183 |
184 | @Book{FoxWeisberg2019,
185 | author = {J. Fox and S. Weisberg},
186 | title = {An {R} Companion to Applied Regression},
187 | edition = {3rd},
188 | publisher = {Sage},
189 | address = {Thousand Oaks},
190 | year = {2019},
191 | }
192 |
193 | @Book{KleiberZeileis2008,
194 | title = {Applied Econometrics with {R}},
195 | author = {C. Kleiber and A. Zeileis},
196 | year = {2008},
197 | publisher = {Springer-Verlag},
198 | address = {New York},
199 | }
200 |
201 | @Manual{R,
202 | title = {{R}: A Language and Environment for Statistical Computing},
203 | author = {{R Core Team}},
204 | organization = {R Foundation for Statistical Computing},
205 | address = {Vienna, Austria},
206 | year = {2020},
207 | url = {https://www.R-project.org/},
208 | }
209 |
210 | @Manual{sem,
211 | title = {{sem}: Structural Equation Models},
212 | author = {J. Fox and Z. Nie and J. Byrnes},
213 | year = {2020},
214 | note = {R package version 3.1-11},
215 | url = {https://CRAN.R-project.org/package=sem},
216 | }
217 |
218 | @Article{Zeileis2006,
219 | title = {Object-Oriented Computation of Sandwich Estimators},
220 | author = {A. Zeileis},
221 | journal = {Journal of Statistical Software},
222 | year = {2006},
223 | volume = {16},
224 | number = {9},
225 | pages = {1--16},
226 | doi = {10.18637/jss.v016.i09},
227 | }
228 |
229 | @Manual{boot,
230 | title = {\pkg{boot}: Bootstrap {R} ({S-PLUS}) Functions},
231 | author = {Angelo Canty and Brian D. Ripley},
232 | year = {2020},
233 | note = {\proglang{R} package version 1.3-25},
234 | url = {https://CRAN.R-project.org/package=boot}
235 | }
236 |
237 | @Book{DavisonHinkley1997,
238 | title = {Bootstrap Methods and Their Applications},
239 | author = {A. C. Davison and D. V. Hinkley},
240 | publisher = {Cambridge University Press},
241 | address = {Cambridge},
242 | year = {1997},
243 | }
244 |
245 | @InCollection{Card1995,
246 | author = {David Card},
247 | title = {Using Geographical Variation in College Proximity to Estimate the Return to Schooling},
248 | editor = {L. N. Christofides and E. K. Grant and R. Swidinsky},
249 | booktitle = {Aspects of Labour Market Behaviour: Essays in Honour of John Vanderkamp},
250 | year = {1995},
251 | pages = {201--222},
252 | publisher = {University of Toronto Press},
253 | address = {Toronto}
254 | }
255 |
256 | @Book{Verbeek2004,
257 | author = {Marno Verbeek},
258 | title = {A Guide to Modern Econometrics},
259 | year = {2004},
260 | edition = {2nd},
261 | publisher = {John Wiley \& Sons},
262 | address = {Chichester}
263 | }
264 |
265 | @Book{StockWatson2007,
266 | author = {James H. Stock and Mark W. Watson},
267 | title = {Introduction to Econometrics},
268 | year = {2007},
269 | edition = {2nd},
270 | publisher = {Addison-Wesley},
271 | address = {Reading}
272 | }
273 |
274 | @Book{HanckArnoldGerberSchmelzer2020,
275 | author = {Christoph Hanck and Martin Arnold and Alexander Gerber and Martin Schmelzer},
276 | title = {Introduction to Econometrics with {R}},
277 | year={2020},
278 | publisher = {Universit\"at Duisburg-Essen},
279 | address = {Essen},
280 | url = {https://www.Econometrics-with-R.org/}
281 | }
282 |
283 | @Incollection{ClevelandGrosseShyu1992,
284 | author = {W. S. Cleveland and E. Grosse and W. M. Shyu},
285 | year = {1992},
286 | title = {Local regression models},
287 | pages = {309--376},
288 | booktitle = {Statistical Models in {S}},
289 | editor = {J.M. Chambers and T.J. Hastie},
290 | publisher = {Wadsworth \& Brooks/Cole},
291 | address = {Pacific Grove {CA}},
292 | }
293 |
294 | @Manual{HesterEtAl2020,
295 | title = {remotes: {R} Package Installation from Remote Repositories, Including '{GitHub}'},
296 | author = {Jim Hester and Gábor Csardi and Hadley Wickham and Winston Chang and Martin Morgan and Dan Tenenbaum},
297 | year = {2020},
298 | note = {R package version 2.1.1},
299 | url = {https://CRAN.R-project.org/package=remotes},
300 | }
301 |
302 | @Book{VenablesRipley2002,
303 | title = {Modern Applied Statistics with S},
304 | author = {W. N. Venables and B. D. Ripley},
305 | publisher = {Springer},
306 | edition = {Fourth},
307 | address = {New York},
308 | year = {2002},
309 | note = {ISBN 0-387-95457-0},
310 | url = {http://www.stats.ox.ac.uk/pub/MASS4/},
311 | }
312 |
313 | @Book{HuberRonchetti2009,
314 | title = {Robust Statistics},
315 | author = {Huber, P. and Ronchetti, E. M.},
316 | year = {2009},
317 | edition = {2nd ed.},
318 | publisher = {Wiley},
319 | address = {Hoboken {NJ}}
320 | }
321 |
--------------------------------------------------------------------------------