-
Notifications
You must be signed in to change notification settings - Fork 0
func
Rafat Hussain edited this page Apr 25, 2015
·
16 revisions
void linreg_clrm(double *x,double *y, int N, double* b,
double *var,double *res,double alpha,double *anv,
double* ci_lower, double* ci_upper);
/*
* Classic Linear Regression Model
* y = b[0] + b[1] * x + u
* x,y and res are all of same length - N
* where res is the residual
*
* alpha is used to determine confidence interval limits
* for a given 100*(1-alpha) % confidence interval
*
*
* Alpha takes values between 0 and 1.
*
* For 95% confidence interval, the value of alpha is 0.05
* For 90% confidence interval, the value of alpha is 0.10 etc.
*/
/*
* Parameters ( b is a double vector of length 2)
* b[0] - beta1
* b[1] - beta2
*/
/* Variances ( var is a double vector of length 5)
* var[0] - variance of residuals
* var[1] variance beta1
* var[2] variance beta2
* var[3] covariance beta1,beta2
* var[4] r^2 Goodness of Fit
*/
/*
* ANOVA ( anv is a double vector of length 7)
*
* anv[0] - TSS Total Sum Of Squares
* anv[1] - ESS Explained Sum Of Squares
* anv[2] - RSS Residual Sum Of Squares
* anv[3] - degrees of freedom of ESS
* anv[4] - degrees of freedom of RSS
* anv[5] - F Statistics = (anv[1] / anv[3]) / (anv[2] / anv[4])
* anv[6] - P value associated with anv[5] used to reject/accept
* zero hypothesis
*/
/* ci_lower and ci_upper (Confidence Interval Lower and Upper Limits)
* ( double vectors of length 3)
* ci_lower[0] - CI Lower Limit corresponding to b[0]
* ci_lower[1] - CI Lower Limit corresponding to b[1]
* ci_lower[2] - CI Lower Limit corresponding to var[0] (sigma^2)
* ci_upper[0] - CI Upper Limit corresponding to b[0]
* ci_upper[1] - CI Upper Limit corresponding to b[1]
* ci_upper[2] - CI Upper Limit corresponding to var[0] (sigma^2)
*/
void zerohyp_clrm(int N,double *b, double *var, double *tval, double *pval) {
Zero Hypothesis Test for CLRM
N is the length of the data series. b and var are parameters/variances obtained from linreg_clrm function.
tval and pval are double vectors of length 2 each containing Student's T values and the corresponding
probability distribution values.
| Latest Version Of Code Here |
|---|
#include <stdio.h>
#include <stdlib.h>
#include "../header/cregres.h"
int main(void) {
int N;
double alpha;
double *res;
double *b;
N = 10;
res = (double*)malloc(sizeof(double)* N);
b = (double*)malloc(sizeof(double)* 2);
// Simple Model -> Y = b0 + b1 * X + u
double X[10] = { 80, 100, 120, 140, 160, 180, 200, 220, 240, 260 };
double Y[10] = { 70, 65, 90, 95, 110, 115, 120, 140, 155, 150 };
double var[5] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
double anv[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
double lower[3] = { 0.0, 0.0, 0.0 };
double upper[3] = { 0.0, 0.0, 0.0 };
double tval[2] = { 0.0, 0.0 };
double pval[2] = { 0.0, 0.0 };
alpha = 0.05;
linreg_clrm(X, Y, N, b, var, res, alpha, anv, lower, upper);
printf("Estimates Regression Parameters : Beta1 = %lf ,Beta 2 = %lf \n", b[0], b[1]);
printf("Confidence Interval %lf \n", (1. - alpha) * 100.);
printf("Beta1 : Lower Limit = %lf ,Upper Limit = %lf \n", lower[0], upper[0]);
printf("Beta2 : Lower Limit = %lf ,Upper Limit = %lf \n", lower[1], upper[1]);
printf("Variance : Lower Limit = %lf ,Upper Limit = %lf \n", lower[2], upper[2]);
printf("%lf ,%lf %lf %lf %lf \n", var[0], var[1], var[2], var[3], var[4]);
zerohyp_clrm(N, b, var, tval, pval);
printf("Beta1 : T Value = %g ,P Value = %g \n", tval[0], pval[0]);
printf("Beta2 : T Value = %g ,P Value = %g \n", tval[1], pval[1]);
printf("\n ANOVA : \n");
printf("Regression : df = %lf ,SS = %lf ,MSS = %lf ,F Stat = %lf \n", anv[3], anv[1], anv[1] / anv[3], anv[5]);
printf("Residual : df = %lf ,SS = %lf ,MSS = %lf \n", anv[4], anv[2], anv[2] / anv[4]);
printf("Total : df = %lf ,SS = %lf \n", anv[3] + anv[4], anv[0]);
printf("F pvalue = %g \n", anv[6]);
free(res);
free(b);
return 0;
}
void linreg_multi(int p, double *x, double *y, int N, double* b, double *sigma2,
double *xxti, double *R2, double *res, double alpha, double *anv,
double* ci_lower, double* ci_upper);
/*
* Multiple Regression Model
* y = b[0] + b[1] * x1 + .. + b[p-1]* xp-1 + u
* p is the total number of parameters (p >= 2 for multiple Regression)
* y and res are all of same length - N
* where res is the residual
* x is of length (p-1) * N
* alpha is used to determine confidence interval limits
* for a given 100*(1-alpha) % confidence interval
*
*
* Alpha takes values between 0 and 1.
*
* For 95% confidence interval, the value of alpha is 0.05
* For 90% confidence interval, the value of alpha is 0.10 etc.
*/
/*
* Parameters ( b is a double vector of length 2)
* b[0] - beta1
* b[1] - beta2
*/
/* Variances ( var is a double vector of length 5)
* sigma2 - variance of residuals
* xxti - Variance-Covariance Matrix of size p*p
* R2[0] and R2[1] - R2 is double vector of length 2. r^2 Goodness of Fit and adjust r^2
*/
/*
* ANOVA ( anv is a double vector of length 7)
*
* anv[0] - TSS Total Sum Of Squares
* anv[1] - ESS Explained Sum Of Squares
* anv[2] - RSS Residual Sum Of Squares
* anv[3] - degrees of freedom of ESS
* anv[4] - degrees of freedom of RSS
* anv[5] - F Statistics = (anv[1] / anv[3]) / (anv[2] / anv[4])
* anv[6] - P value associated with anv[5] used to reject/accept
* zero hypothesis
*/
/* ci_lower and ci_upper (Confidence Interval Lower and Upper Limits)
* ( double vectors of length p+1)
* ci_lower[0] to ci_lower[p-1] corresponding to b[0],...,b[p-1]
* ci_lower[p] - CI Lower Limit corresponding to sigma2 (sigma^2)
* ci_upper[0] to ci_upper[p-1] corresponding to b[0],...,b[p-1]
* ci_upper[p] - CI Upper Limit corresponding to sigma2 (sigma^2)
*/
void zerohyp_multi(int N,double *b,int p, double *varcovar, double *tval, double *pval)
Zero Hypothesis Test for Multiple Regression Model
N is the length of the data series and p is the total number of parameters. b and varcovar are
parameters/Variance-Covariance Matrices obtained from linreg_multi function. tval and pval are double vectors
of length p-1 each containing Student's T values and the corresponding probability values.
| Latest Version Of Code Here |
|---|
#include <stdio.h>
#include <stdlib.h>
#include "../header/cregres.h"
int main(void) {
int N, p;
double alpha;
double *res2;
N = 15;
p = 3;
// p = 3 corresponds to one dependent variable (YY) and two independent variables
// YY = b0 + b1 * X1 + b2 * X2 + u
// where u is residual vector of length N
// contained in XX vector. where XX = [X1,X2] .
// YY, X1 and X2 are each of length N = 15
// X1 = [1839,1844,1831,1881,1883,1910,1969,2016,2126,2239,2336,2404,
// 2487,2535,2595]
// X2 =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
res2 = (double*)malloc(sizeof(double)* N);
/*
TABLE C.4 PER CAPITA PERSONAL CONSUMPTION EXPENDITURE (PPCE) AND PER CAPITA
PERSONAL DISPOSABLE INCOME (PPDI) IN THE UNITED STATES, 1956�1970,
IN 1958 DOLLARS, Gujarati D, Basic Econometrics, 4th Ed. McGraw-Hill
*/
double b2[3] = { 0.0, 0.0, 0.0 };// p = 3 Parameters
double anv2[7] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };// Anova
double low[4] = { 0.0, 0.0, 0.0,0.0 };// Lower limit for 3 parameters and Variance
double up[4] = { 0.0, 0.0, 0.0,0.0 };// Upper limit for 3 parameters and Variance
double tval2[3] = { 0.0, 0.0, 0.0 };// t values corresponding to the three parameters
double pval2[3] = { 0.0, 0.0, 0.0 };// Probablities Associated with t values
double sig2[1] = { 0.0 };
double R2[2] = { 0.0, 0.0 };// R2 values R2[0] = R2. R2[1] = R2 adjusted
double varcovar[9] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };// p * p Variance-Covariance Matrix
alpha = 0.05;
double XX[30] = { 1839, 1844, 1831, 1881, 1883, 1910, 1969, 2016, 2126, 2239, 2336, 2404,
2487, 2535, 2595, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
double YY[15] = { 1673, 1688, 1666, 1735, 1749, 1756, 1815, 1867, 1948, 2048, 2128, 2165,
2257, 2316, 2324 };
linreg_multi(p, XX, YY, N, b2, sig2, varcovar, R2, res2, alpha, anv2, low, up);
printf("Estimates Regression Parameters : Beta1 = %lf ,Beta 2 = %lf ,Beta 3 = %lf \n", b2[0], b2[1], b2[2]);
printf("sigma2 = %lf , R^2 = %lf R^2 (Adj.) = %lf \n", sig2[0], R2[0], R2[1]);
printf("Confidence Interval %lf \n", (1. - alpha) * 100.);
printf("Beta1 : Lower Limit = %lf ,Upper Limit = %lf \n", low[0], up[0]);
printf("Beta2 : Lower Limit = %lf ,Upper Limit = %lf \n", low[1], up[1]);
printf("Beta3 : Lower Limit = %lf ,Upper Limit = %lf \n", low[2], up[2]);
printf("Variance : Lower Limit = %lf ,Upper Limit = %lf \n", low[3], up[3]);
zerohyp_multi(N, b2, p, varcovar, tval2, pval2);
printf("Beta1 : T Value = %g ,P Value = %g \n", tval2[0], pval2[0]);
printf("Beta2 : T Value = %g ,P Value = %g \n", tval2[1], pval2[1]);
printf("Beta3 : T Value = %g ,P Value = %g \n", tval2[2], pval2[2]);
printf("\n ANOVA : \n");
printf("Regression : df = %lf ,SS = %lf ,MSS = %lf ,F Stat = %lf \n", anv2[3], anv2[1], anv2[1] / anv2[3], anv2[5]);
printf("Residual : df = %lf ,SS = %lf ,MSS = %lf \n", anv2[4], anv2[2], anv2[2] / anv2[4]);
printf("Total : df = %lf ,SS = %lf \n", anv2[3] + anv2[4], anv2[0]);
printf("F pvalue = %g \n", anv2[6]);
free(res2);
return 0;
}

