从 GSL 库中获取 C gsl_fit_linear() 函数中线性回归的 p 值
我正在尝试用 C 语言重现 R 中的一些代码,因此我尝试使用 gsl_fit_linear()
函数来拟合线性回归。
在 R 中,我使用 lm() 函数,它使用以下代码返回拟合的 p 值:
lmAvgs<- lm( c(1.23, 11.432, 14.653, 21.6534) ~ c(1970, 1980, 1990, 2000) )
summary(lmAvgs)
我不知道如何从 C 输出到 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 值。我是统计和 C 方面的新手!
I'm trying to reporduce some code from R in C, so I'm trying to fit a linear regression using the gsl_fit_linear()
function.
In R I'd use the lm() function, which returns a p-value for the fit using this code:
lmAvgs<- lm( c(1.23, 11.432, 14.653, 21.6534) ~ c(1970, 1980, 1990, 2000) )
summary(lmAvgs)
I've no idea though how to go from the C output to a p-value, my code looks something like this so far:
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);
}
This seems to correctly calculate slope and intercept but I don't know how to get a p-value. I'm novice at stats and C!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一切都在:http://en.wikipedia.org/wiki/Ordinary_least_squares。但这里有一段代码显示类似于 R 中的摘要(lmAvgs)的输出。要运行它,您需要 GSL 库 :
给出:
R 给出:
Everything is on : http://en.wikipedia.org/wiki/Ordinary_least_squares. but here is a piece of code which display an output similar to summary(lmAvgs) in R. To run this, you need the GSL Library :
Which gives :
R gives :