# The weightedIV package

This package serves to perform an instrumental-variable estimation, where the observations are weighted according to the values of an observable series. See also the documentation for gretl's `wls` estimation command for the role that the weights play.

# Arguments

The ordering of the inputs is similar to gretl's native `wls` command, but of course here we need an additional list specifying the instruments.

1. series `w` holding the weights
2. series `y` dependent variable
3. list   `X` regressors
4. list   `Z` instruments
5. bool   `robust` use robust inference? (default: no/FALSE)
6. int    `verbosity` verbosity level (default: 1, intermediate)

The series and list arguments are all mandatory.

If robust inference is chosen, the precise type depends on the current setting in gretl and can be adjusted as usual, before executing the package.

The usual Hausman exogeneity and Sargan over-identification tests are based on the weighted data.

For a verbosity setting of 2, the standard printout of gretl's underlying tsls routine is also shown.   

# Return value

The function returns a model-style bundle with the following result items. Many of them are directly copied from the ouput of the internal tsls command, while others are created in this package.

## Scalars

- df: degrees of freedom
- dfcorr: corrected degrees of freedom
- ess: error sum of squares
- ncoeff: number of coefficients
- ninst: number of instruments
- sample_t1: index number of starting obs
- sample_t2: index number of ending obs
- sigma: standard error of the regression
- T: sample size
- t1: index number of first obs
- t2: index number of last obs
- rsq: R2 (calculated as the squared correlation of actual and fitted)

## Series and list(s)

Users are reminded that series and lists inside a bundle only make sense in the dataset context from which they were taken.

- sample: indicator series for the sample used
- weightser: series used for weighting (as w above)
- yhat: fitted values based on the unweighted data
- uhat: estimated residuals based on the unweighted data
- w_yhat: fitted values (weighted data)
- w_uhat: estimated residuals (weighted data)
- instlist: Z input list
- xlist: X input list
- ylist: y input series wrapped in a list type
- endolist: implied list of non-instruments

## Matrix (and vector) results

- coeff: vector of estimated coefficients
- stderr: vector of standard errors of coefficients
- vcv: full variance-covariance matrix of coefficients
- hausman: vector of Hausman exogeneity test: 1) stat, 2) dof, 3) p-value -- if the IV and OLS estimates are identical, this result is an empty matrix
- sargan: vector of Sargan test for valid instruments: 1) stat, 2) dof, 3) p-value -- in the no over-identification case this result is an empty matrix

## String(s)

- command: "weightedIV"
- weightname: actual name of the w input
- depvar: actual name of the y input
- parnames: actual names of the X input series (strings array)


# Changelog

- v0.2, February 2024: add the Hausman endogeneity and Sargan over-identification tests (coming natively from gretl's tsls)
- v0.1, February 2024: initial release
