![]() |
|
OLS models. Much of the real work is done in apop_regression.c.
| apop_ls_settings* apop_ls_settings_init | ( | apop_ls_settings | in | ) |
Initialize the settings for a least-squares--type model. For use with Apop_model_add_group. See apop_ls_settings for the possible elements to set.
Instrumental variable regression
Operates much like the apop_ols model, but the input parameters also need to have a table of substitutions. The vector element of the table lists the column numbers to be substituted (the dependent var is zero; first independent col is one), and then one column for each item to substitute.
If the vector of your apop_data set is NULL, then I will use the row names to find the columns to substitute. This is generally more robust and/or convenient.
If the instruments data set is somehow NULL or empty, I'll just run OLS.
apop_data *submatrix =apop_data_alloc(0, data->matrix->size1, 2); APOP_COL(submatrix, 0, firstcol); gsl_vector_memcpy(firstcol, your_data_vector); APOP_COL(submatrix, 1, secondcol); gsl_vector_memcpy(firstcol, your_other_data_vector); apop_name_add(submatrix->names, "subme_1", 'r'); apop_name_add(submatrix->names, "subme_2", 'r'); Apop_model_add_group(&apop_iv, apop_ls, .instruments = submatrix); apop_model *est = apop_estimate(data, apop_iv); apop_model_show(est);
Ordinary least squares.
| inset | The first column is the dependent variable, and the remaining columns the independent. Is destroyed in the process, so make a copy beforehand if you need. | |
| epin | An apop_model object. It may have a apop_ls_settings group attached. I'll look at the destroy_data element; if this is NULL or destroy_data==0, then the entire data set is copied off, and then mangled. If destroy_data==1, then this doesn't copy off the data set, but destroys it in place. I also look at want_cov and want_expected_value, though I'll not produce the covariance matrix only if both are 'n'. |
*. The_result->parameters will hold the coefficients; the first coefficient will be the coefficient on the constant term, and the remaining will correspond to the independent variables. It will therefore be of size (data->size2). Do not pre-allocate.If you asked for them, the covariance matrix, confidence levels, and residuals will also be returned. The confidence intervals give the level of certainty with which we can reject the hypothesis that the given coefficient is zero.
See also the page on Data prep rules.
sample
First, you will need a file named data in comma-separated form. The first column is the dependent variable; the remaining columns are the independent. For example:
Y, X_1, X_2, X_3 2,3,4,5 1,2,9,3 4,7,9,0 2,4,8,16 1,4,2,9 9,8,7,6
The program:
#include <apop.h> int main(void){ apop_data *data; apop_model *est; apop_text_to_db("data","d"); data = apop_query_to_data("select * from d"); est = apop_estimate(data, apop_ols); apop_model_show(est); return 0; }
If you saved this code to sample.c, then you can compile it with
gcc sample.c -lapophenia -lgsl -lgslcblas -lsqlite3 -o run_me
and then run it with ./run_me. Alternatively, you may prefer to compile the program using a Makefile .
Feeling lazy? The program above was good form and demonstrated useful features, but the code below will do the same thing in two lines:
#include <apop.h> int main(){ apop_model_show(apop_estimate(apop_text_to_data("data"), apop_ols)); }
The WLS model
You will need to provide the weights in yourdata->weights. Otherwise, this model will behave just like apop_ols.