Alternatively try a more standard approach - https://stats.stackexchange.com/questions/472144/how-to-manually-calculate-standard-errors-for-instrumental-variables
xhat <- z %*%solve(t(z) %*% z) %*%t(z) %*% xbhat <-solve(t(xhat) %*% xhat) %*%t(xhat) %*% ye <- y - x %*% bhatC <-sum(e^2) / n *solve(t(xhat) %*% xhat)
Result
bhat
[,1]
[1,] 0.2237422
sqrt(C)
[,1]
[1,] 0.2310808
This matches ivreg more closely
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.2
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
time zone: Europe/London
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_1.1.2 ivreg_0.6-2
loaded via a namespace (and not attached):
[1] vctrs_0.6.2 cli_3.6.1 knitr_1.43 rlang_1.1.1
[5] xfun_0.39 Formula_1.2-5 car_3.1-2 generics_0.1.3
[9] jsonlite_1.8.5 glue_1.6.2 zoo_1.8-12 htmltools_0.5.5
[13] lmtest_0.9-40 fansi_1.0.4 rmarkdown_2.22 grid_4.3.0
[17] tibble_3.2.1 evaluate_0.21 carData_3.0-5 abind_1.4-5
[21] MASS_7.3-58.4 fastmap_1.1.1 lifecycle_1.0.3 yaml_2.3.7
[25] compiler_4.3.0 pkgconfig_2.0.3 htmlwidgets_1.6.2 rstudioapi_0.14
[29] lattice_0.21-8 digest_0.6.31 R6_2.5.1 tidyselect_1.2.0
[33] utf8_1.2.3 pillar_1.9.0 magrittr_2.0.3 tools_4.3.0