Jonas
Jonas

Reputation: 67

linear fit with error on x and y with p-value

I am currently working on a project in which I want to find if there is a relation between two variables x an y. For both values I have calculated the errors as well. The df looks as follows.

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
...

As you can see the error on x is constant but the error on y is not. I looked into using the lm() function in R but it seems as if I can only set the error on the y-axis using weights. I am quite new to this kind of statistical analysis and the research I have done until now amounted to not much. I would like to plot the linear fit and also find the p-value for the statistical significance of the slope of the regression.

Anyone out there that knows how to do this? Answers preferably in R but python would also be fine :)

Thank you in advance for any answers/help

Upvotes: 0

Views: 742

Answers (1)

Nilesh Ingle
Nilesh Ingle

Reputation: 1883

As suggested in comments (Thanks James), the orthogonal distance regression should work. The deming() package in R takes both x_error and y_error (post). Below is a sample 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'))

enter image description here

Upvotes: 1

Related Questions