This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sreg.c
executable file
·172 lines (157 loc) · 4.64 KB
/
sreg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* sreg.c
*
* David Garen 11/89, 8/91, 11/91, 8/92
*
* Perform simple linear regression. Computes slope, intercept,
* correlation coefficient, standard error, and t-statistic.
*
* Returns 0 for normal completion,
* 1 if x data are all equal,
* 3 if both x and y data are all equal.
*/
#include <math.h>
int sreg(x, y, b0, b1, r, se, t, n)
double *x; /* independent variable data vector */
double *y; /* dependent variable data vector */
double *b0; /* intercept */
double *b1; /* slope */
double *r; /* correlation coefficient */
double *se; /* standard error */
double *t; /* t-statistic */
int n; /* number of observations */
{
double dum1, dum2, dum3; /* dummy variables for calculations */
double err; /* error */
int i; /* loop index */
double sqrt(); /* square root function */
double sumx = 0; /* sum of x variable */
double sumx2 = 0; /* sum of x variable squared */
double sumxy = 0; /* sum of x times y */
double sumy = 0; /* sum of y variable */
double sumy2 = 0; /* sum of y variable squared */
int xequal = 0; /* flag for all x data being equal */
int yequal = 0; /* flag for all y data being equal */
/* Check for all x or y data being equal */
for (i = 1; i < n; i++)
if (x[i] != x[0])
break;
if (i == n)
xequal = 1;
for (i = 1; i < n; i++)
if (y[i] != y[0])
break;
if (i == n)
yequal = 1;
if (xequal == 1 && yequal != 1)
return(1);
if (xequal == 1 && yequal == 1)
return(3);
/* Do regression */
for (i = 0; i < n; i++) {
sumx += x[i];
sumx2 += x[i] * x[i];
sumxy += x[i] * y[i];
sumy += y[i];
sumy2 += y[i] * y[i];
}
dum1 = n * sumxy - sumx * sumy;
dum2 = n * sumx2 - sumx * sumx;
dum3 = n * sumy2 - sumy * sumy;
*b1 = dum1 / dum2;
*b0 = (sumy / n) - (*b1 * sumx / n);
if (yequal == 1) {
*r = 1;
*se = 0;
*t = 0;
return(0);
}
*r = dum1 / sqrt(dum2 * dum3);
if (n > 2) {
dum1 = 0;
for (i = 0; i < n; i++) {
err = y[i] - *b0 - *b1 * x[i];
dum1 += (err * err);
}
dum1 /= (n - 2);
*se = sqrt(dum1);
*t = (*b1 / *se) * sqrt(dum2 / n);
}
else {
*se = 0;
*t = 0;
}
return(0);
}
int sreg_const(x, y, b0, b1, r, se, t, n)
double *x; /* independent variable data vector */
double *y; /* dependent variable data vector */
double *b0; /* intercept */
double b1; /* slope */
double *r; /* correlation coefficient */
double *se; /* standard error */
double *t; /* t-statistic */
int n; /* number of observations */
{
double dum1, dum2, dum3; /* dummy variables for calculations */
double err; /* error */
int i; /* loop index */
double sqrt(); /* square root function */
double sumx = 0; /* sum of x variable */
double sumx2 = 0; /* sum of x variable squared */
double sumxy = 0; /* sum of x times y */
double sumy = 0; /* sum of y variable */
double sumy2 = 0; /* sum of y variable squared */
int xequal = 0; /* flag for all x data being equal */
int yequal = 0; /* flag for all y data being equal */
/* Check for all x or y data being equal */
for (i = 1; i < n; i++)
if (x[i] != x[0])
break;
if (i == n)
xequal = 1;
for (i = 1; i < n; i++)
if (y[i] != y[0])
break;
if (i == n)
yequal = 1;
if (xequal == 1 && yequal != 1)
return(1);
if (xequal == 1 && yequal == 1)
return(3);
/* Do regression */
for (i = 0; i < n; i++) {
sumx += x[i];
sumx2 += x[i] * x[i];
sumxy += x[i] * y[i];
sumy += y[i];
sumy2 += y[i] * y[i];
}
dum1 = n * sumxy - sumx * sumy;
dum2 = n * sumx2 - sumx * sumx;
dum3 = n * sumy2 - sumy * sumy;
// *b1 = dum1 / dum2;
*b0 = (sumy / n) - (b1 * sumx / n);
if (yequal == 1) {
*r = 1;
*se = 0;
*t = 0;
return(0);
}
*r = dum1 / sqrt(dum2 * dum3);
if (n > 2) {
dum1 = 0;
for (i = 0; i < n; i++) {
err = y[i] - *b0 - b1 * x[i];
dum1 += (err * err);
}
dum1 /= (n - 2);
*se = sqrt(dum1);
*t = (b1 / *se) * sqrt(dum2 / n);
}
else {
*se = 0;
*t = 0;
}
return(0);
}