Linear model with errors of x- and y-values

997 Views Asked by At

I have data that I have to fit with a linear model. I also have errors associated with every one of the x and y values.

Basically I want to do something like this (all vectors of the same length):

lm(y_data +- y_errors ~ x_data +- x_errors)

But obviously that does not work.

It seems Origin can do it (http://www.originlab.com/doc/Origin-Help/LinearFit-XErr-Dialog).

How can I tell R to use the x- and y-errors for creating a linear model, like Origin does?

1

There are 1 best solutions below

2
On

Considering x and y can have errors of xe and ye, respectively. For every (x,y) combination if following combinations are added and analyzed, they may get you what you want. Try and compare with originlab results.

x,y
x-xe, y-ye
x+xe, y+ye
x-xe, y+ye
x+xe, y-y2

To get these combinations, try following code (replace x, y, xe and ye by your data)

x = c(1,2,3,4)
xe = 0.1
y = c(12, 24, 28, 46)
ye = 1.3

ddf = data.frame(xx=as.numeric(), yy=as.numeric())
i=1
counter=1
while(i<(length(x)+1)){
    ddf[counter,]=c(x[i],y[i])
    ddf[counter+1,]=c(x[i]-xe,y[i]-ye)
    ddf[counter+2,]=c(x[i]+xe,y[i]+ye)
    ddf[counter+3,]=c(x[i]-xe,y[i]+ye)
    ddf[counter+4,]=c(x[i]+xe,y[i]-ye)
    counter = counter+5
    i = i+1
}
ddf
    xx   yy
1  1.0 12.0
2  0.9 10.7
3  1.1 13.3
4  0.9 13.3
5  1.1 10.7
6  2.0 24.0
7  1.9 22.7
8  2.1 25.3
9  1.9 25.3
10 2.1 22.7
11 3.0 28.0
12 2.9 26.7
13 3.1 29.3
14 2.9 29.3
15 3.1 26.7
16 4.0 46.0
17 3.9 44.7
18 4.1 47.3
19 3.9 47.3
20 4.1 44.7

Then run the test:

with(ddf, lm(yy~xx))

Call:
lm(formula = yy ~ xx)

Coefficients:
(Intercept)           xx  
      1.169       10.533