【问题标题】:linear fit with error on x and y with p-value在 x 和 y 上带有 p 值的误差线性拟合
【发布时间】:2018-07-12 14:08:18
【问题描述】:

我目前正在从事一个项目,我想在该项目中查找两个变量 x 和 y 之间是否存在关系。对于这两个值,我也计算了误差。 df 如下所示。

x   y    x_error    y_error
5   1      0.5      0.2
6   2      0.5      0.15
7   1.75   0.5      0.3
7   0.5    0.5      0.1254
...

如您所见,x 上的错误是恒定的,但 y 上的错误不是。 我研究过在 R 中使用 lm() 函数,但似乎我只能使用权重设置 y 轴上的误差。我对这种统计分析很陌生,到目前为止我所做的研究并不多。我想绘制线性拟合并找到回归斜率的统计显着性的 p 值。

有谁知道如何做到这一点? 最好在 R 中回答,但 python 也可以:)

提前感谢您的任何回答/帮助

【问题讨论】:

  • 如果我理解正确,对于 Python,在这些情况下使用 scipy.odr(正交距离回归)包。如果 R 也有 ODR 也应该满足你的需要。
  • 感谢您的建议,我会研究一下!

标签: python r statistics linear-regression standard-error


【解决方案1】:

按照 cmets 中的建议(感谢 James),orthogonal distance regression 应该可以工作。 R 中的 deming() 包同时采用 x_errory_error (post)。下面是一个示例code

# Import libraries
library(deming)

# Create sample data
x <- rnorm(100, mean=10, sd=.01)
y <- x * rnorm(100, mean=20, sd=.01)
x_error <- x * 0.01
y_error <- y * 0.01
df <- data.frame(x, y, x_error, y_error)
head(df)

# Fit lm()
lm.fit <- lm(y ~ x, data=df)
summary(lm.fit)

# Fit deming()
deming.fit = deming(y ~ x, data=df, xstd=x_error, ystd=y_error)
print(deming.fit)

# Plot fit
plot(df$x, df$y, xlab='x', ylab='y')
abline(lm.fit, col='red', lty=1)
abline(deming.fit, col='blue', lty=2)
legend('topleft',legend= c("lm()", "deming()"), lty=c(1,2), col=c('red','blue'))

【讨论】:

  • 非常感谢您!至于线性拟合数据,这正是我所需要的,thnx!
  • OP 表示他想确定 p 值,我在这个答案中没有看到。由于我也想知道这个问题的答案,请您详细说明一下吗?
猜你喜欢
  • 1970-01-01
  • 2020-07-04
  • 2020-05-06
  • 1970-01-01
  • 2021-10-05
  • 2015-01-07
  • 2012-07-29
  • 2020-10-08
  • 2018-10-13
相关资源
最近更新 更多