-
Notifications
You must be signed in to change notification settings - Fork 0
regex
Rafat Hussain edited this page Apr 23, 2015
·
2 revisions
| Latest Version Of Code Here |
|---|
#include <stdio.h>
#include <stdlib.h>
#include "../header/cregres.h"
int main(void) {
int N, i, p;
double alpha;
double *X,*Y,*res2;
reg_object fit;
p = 2;// Total Number of variables. Simple Model -> Y = b0 + b1 * X + u with p = 2 ( two variables).
double varcovar[4] = { 0.0, 0.0, 0.0, 0.0}; // Size p * p Variance Covariance Matrix
alpha = 0.05;
double tval3[2] = { 0.0, 0.0};// t values . Size p
double pval3[2] = { 0.0, 0.0 };//p values . Size p. (Probabilities associated with each t value)
FILE *ifp;
double temp[10000];
ifp = fopen("Galton.txt","r");// Galton Child/Parent height dataset. Column 1 - Height of Child. Corresponding Column 2 - Height of Parent
i = 0;
if (!ifp) {
printf("Cannot Open File");
}
while(!feof(ifp)) {
fscanf(ifp,"%lf %lf \n",&temp[i],&temp[i+1]);
i = i + 2;;
}
N = i/2;
fclose(ifp);
X = (double*)malloc(sizeof(double)* N);// Independent Variable. Size N
Y = (double*)malloc(sizeof(double)* N);// Dependent Variable. Size N
res2 = (double*)malloc(sizeof(double)* N);// residuals . Size N
for (i = 0; i < N; ++i) {
Y[i] = temp[2*i];// Height Of Child
X[i] = temp[2*i + 1];// height of Parent
}
fit = reg_init(N, p);
regress(fit, X, Y, res2, varcovar, alpha);// Perform Regression
// res2 - residuals vector. varcovar - variance-covariance matrix
// alpha - Used to determine (1-alpha) * 100 % confidence interval
// alpha = 0.05 for 95% confidence interval
/*
Following functions are used to print various values and tables.
Please don't use these functions in any industrial codes as printing to
terminal incurs considerable cost. All of these values are available to
you already after you run regression functions. See classes and functions
*/
summary(fit); // summary of regression
anova(fit); // ANOVA Table
confint(fit); //Confidence Intervals of Regression Parameters
zerohyp_val(fit, tval3, pval3); // Print Zerohypothesis t-test values
double inpx[1] = { 68.4 };
double varx[2] = { 0.0, 0.0 };
double oupx;
oupx = pointpredict(fit, inpx, varcovar, varx); // Fit Values. Returns output
// for a given (p-1) input vector.
// varx[0] - Variance of Mean Prediction
// varx[1] - Variance Of Individual Prediction
printf("\nOutput %lf , Variance(Mean Pred) %lf , Variance(Indiv. Pred) %lf \n\n", oupx, varx[0], varx[1]);
free_reg(fit);
free(res2);
free(X);
free(Y);
return 0;
}
| 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;
reg_object fit;
double varcovar[9] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
alpha = 0.05;
double tval3[3] = { 0.0, 0.0, 0.0 };
double pval3[3] = { 0.0, 0.0, 0.0 };
/*
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
*/
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);
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 };
fit = reg_init(N, p);
regress(fit, XX, YY, res2, varcovar, alpha);// Perform Regression
// res2 - residuals vector. varcovar - variance-covariance matrix
// alpha - Used to determine (1-alpha) * 100 % confidence interval
// alpha = 0.05 for 95% confidence interval
summary(fit); // summary of regression
anova(fit); // ANOVA Table
confint(fit); //Confidence Intervals of Regression Parameters
zerohyp_val(fit, tval3, pval3); // Obtaon Zerohypothesis t-test values
double inpx[2] = { 2610, 16 };
double varx[2] = { 0.0, 0.0 };
double oupx;
oupx = pointpredict(fit, inpx, varcovar, varx); // Fit Values. Returns output
// for a given (p-1) input vector.
// varx[0] - Variance of Mean Prediction
// varx[1] - Variance Of Individual Prediction
printf("Output %lf , Variance(Mean Pred) %lf , Variance(Indiv. Pred) %lf \n", oupx, varx[0], varx[1]);
free_reg(fit);
free(res2);
return 0;
}
| 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;
reg_object fit;
double varcovar[9] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
alpha = 0.05;
double tval3[3] = { 0.0, 0.0, 0.0 };
double pval3[3] = { 0.0, 0.0, 0.0 };
N = 20;
p = 3;
// This is an example of polynomial regression
// Brockwell and Davis US populaton example data from 1790-1980
// p = 3 corresponds to one dependent variable (YY) and two independent variables (***)
// YY = b0 + b1 * X + b2 * X * X + u
// where u is residual vector of length N
// YY, X and X*X are vectors each of length N = 20
// (***) Even though X1 = X and X2 = X^2 , X1 and X2 are linearly independent
// IMPORTANT - You may want to transform input data instead of using it as is.
// For example YY can be scaled down or transformed to a log scale.
// XX can be transformed to 1,2,3 instead of 1790,1800,1810 etc.
// This example simply demonstrates the usage of polynomial regression.
res2 = (double*)malloc(sizeof(double)* N);
double XX[20] = { 1790, 1800, 1810, 1820, 1830, 1840, 1850, 1860, 1870, 1880,
1890, 1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980 };
double YY[20] = { 3929214, 5308483, 7239881, 9638453, 12860702, 17063353, 23191876, 31443321, 38558371, 50189209, 62979766,
76212168, 92228496, 106021537, 123202624, 132164569, 151325798, 179323175, 203302031, 226542203 };
fit = reg_init(N, p);
regress_poly(fit, XX, YY, res2, varcovar, alpha);// Perform Polynomial Regression
// res2 - residuals vector. varcovar - variance-covariance matrix
// alpha - Used to determine (1-alpha) * 100 % confidence interval
// alpha = 0.05 for 95% confidence interval
summary(fit); // summary of regression
anova(fit); // ANOVA Table
confint(fit); //Confidence Intervals of Regression Parameters
zerohyp_val(fit, tval3, pval3); // Obtain Zerohypothesis t-test values
double inpx[2] = { 1990, 1990 * 1990 };// Find population estimate for the year 1990
double varx[2] = { 0.0, 0.0 };
double oupx;
oupx = pointpredict(fit, inpx, varcovar, varx); // Fit Values. Returns output
// for a given (p-1) input vector.
// varx[0] - Variance of Mean Prediction
// varx[1] - Variance Of Individual Prediction
printf("Output %lf , Variance(Mean Pred) %lf , Variance(Indiv. Pred) %lf \n", oupx, varx[0], varx[1]);
free_reg(fit);
free(res2);
return 0;
}


