c语言gsl,从GSL库获取C gsl_fit_linear()函数中的线性回归的p值

一切都在:http://en.wikipedia.org/wiki/Ordinary_least_squares。但是这里有一段代码,在R中显示类似于summary(lmAvgs)的输出。要运行它,你需要GSL库:

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<|t|)"<

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<

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<

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<

double F=R2*dl/(1-R2);

double p_value=1-gsl_cdf_fdist_P(F,1,dl);

cout<

这使 :

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给出:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于GSL的线性代数实例,演示如何使用GSL来解决线性方程组和求逆矩阵问题。 ```c #include <stdio.h> #include <gsl/gsl_linalg.h> int main() { // 定义矩阵和向量 double mdata[] = {1.0, 2.0, 3.0, 2.0, 4.0, 5.0, 3.0, 5.0, 6.0}; double vdata[] = {1.0, 2.0, 3.0}; gsl_matrix_view m = gsl_matrix_view_array(mdata, 3, 3); gsl_vector_view v = gsl_vector_view_array(vdata, 3); // 解决线性方程组 Ax = b gsl_vector *x = gsl_vector_alloc(3); gsl_permutation *p = gsl_permutation_alloc(3); int signum; gsl_linalg_LU_decomp(&m.matrix, p, &signum); gsl_linalg_LU_solve(&m.matrix, p, &v.vector, x); printf("Solution to Ax = b:\n"); gsl_vector_fprintf(stdout, x, "%g"); // 求矩阵的逆 gsl_matrix *inv = gsl_matrix_alloc(3, 3); gsl_matrix *mcopy = gsl_matrix_alloc(3, 3); gsl_matrix_memcpy(mcopy, &m.matrix); gsl_linalg_LU_decomp(mcopy, p, &signum); gsl_linalg_LU_invert(mcopy, p, inv); printf("Inverse of A:\n"); gsl_matrix_fprintf(stdout, inv, "%g"); // 释放内存 gsl_vector_free(x); gsl_permutation_free(p); gsl_matrix_free(inv); gsl_matrix_free(mcopy); return 0; } ``` 该程序首先定义了一个3x3的矩阵和一个长度为3的向量,并将它们存储在GSL的矩阵和向量视图。然后,程序使用GSL的LU分解和求解线性方程组函数来解决线性方程组Ax=b,并打印解向量x的。接下来,程序使用GSL的LU分解和求逆矩阵函数来求解矩阵A的逆,并打印逆矩阵的。最后,程序释放了动态分配的内存。 需要注意的是,在编译该程序时,需要链接GSL,例如: ``` gcc -o example example.c -lgsl -lgslcblas -lm ``` 此处,-lgsl和-lgslcblas选项用于链接GSL和BLAS,-lm选项用于链接数学

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值