2011-03-31 2 views
4

C에서 R의 일부 코드를 reporduce하려고 했으므로 gsl_fit_linear() 함수를 사용하여 선형 회귀를 적용하려고합니다.GSL 라이브러리의 C gsl_fit_linear() 함수에서 선형 회귀에 대한 p 값 얻기

이 코드를 사용하여 착용감의 p 값을 반환 LM() 함수를 사용하십시오 R에서

: 나는에 C 출력에서 ​​이동하는 방법하지만 아무 생각도 없어

lmAvgs<- lm(c(1.23, 11.432, 14.653, 21.6534) ~ c(1970, 1980, 1990, 2000)) 
summary(lmAvgs) 

을 이 올바르게 기울기와 절편을 계산하는 것 같다하지만 난 p- 값을 얻는 방법을 모르는

int main(void) 
{ 
int i, n = 4; 
double x[4] = { 1970, 1980, 1990, 2000 }; 
double y[4] = {1.23, 11.432, 14.653, 21.6534}; 
double c0, c1, cov00, cov01, cov11, sumsq; 
gsl_fit_linear (x, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &sumsq); 
} 

: p 형 값은, 내 코드는 지금까지 같이 보입니다. 나는 stats와 C에 초보자이다!

답변

8

모두 켜져 있습니다 : http://en.wikipedia.org/wiki/Ordinary_least_squares. 제공

int n = 4; 
double x[4] = { 1970, 1980, 1990, 2000}; 
double y[4] = {1.23, 11.432, 14.653, 21.6534}; 
double c0, c1, cov00, cov01, cov11, sumsq; 
gsl_fit_linear (x, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &sumsq); 

cout<<"Coefficients\tEstimate\tStd. Error\tt value\tPr(>|t|)"<<endl; 

double stdev0=sqrt(cov00); 
double t0=c0/stdev0; 
double pv0=t0<0?2*(1-gsl_cdf_tdist_P(-t0,n-2)):2*(1-gsl_cdf_tdist_P(t0,n-2));//This is the p-value of the constant term 
cout<<"Intercept\t"<<c0<<"\t"<<stdev0<<"\t"<<t0<<"\t"<<pv0<<endl; 

double stdev1=sqrt(cov11); 
double t1=c1/stdev1; 
double pv1=t1<0?2*(1-gsl_cdf_tdist_P(-t1,n-2)):2*(1-gsl_cdf_tdist_P(t1,n-2));//This is the p-value of the linear term 
cout<<"x\t"<<c1<<"\t"<<stdev1<<"\t"<<t1<<"\t"<<pv1<<endl; 

double dl=n-2;//degrees of liberty 
double ym=0.25*(y[0]+y[1]+y[2]+y[3]); //Average of vector y 
double sct=pow(y[0]-ym,2)+pow(y[1]-ym,2)+pow(y[2]-ym,2)+pow(y[3]-ym,2); // sct = sum of total squares 
double R2=1-sumsq/sct; 
cout<<"Multiple R-squared: "<<R2<<", Adjusted R-squared: "<<1-double(n-1)/dl*(1-R2)<<endl; 
double F=R2*dl/(1-R2); 
double p_value=1-gsl_cdf_fdist_P(F,1,dl); 
cout<<"F-statistic: "<<F<<" on 1 and "<<n-2<<" DF, p-value: "<<p_value<<endl; 

:

Coefficients Estimate Std. Error t value Pr(>|t|) 
Intercept -1267.91 181.409 -6.98922 0.0198633 
x 0.644912 0.0913886 7.05681 0.0194956 
Multiple R-squared: 0.961389, Adjusted R-squared: 0.942083 
F-statistic: 49.7986 on 1 and 2 DF, p-value: 0.0194956 

R이 제공 :하지만 여기를 실행하려면 요약 R.에서 (lmAvgs)과 유사한 출력을 표시 코드의 조각, 당신은이 GSL Library 필요하다

Coefficients: 
Estimate Std. Error t value Pr(>|t|) 
(Intercept)    -1.268e+03 1.814e+02 -6.989 0.0199 * 
c(1970, 1980, 1990, 2000) 6.449e-01 9.139e-02 7.057 0.0195 * 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 2.044 on 2 degrees of freedom 
Multiple R-squared: 0.9614, Adjusted R-squared: 0.9421 
F-statistic: 49.8 on 1 and 2 DF, p-value: 0.01950