regression with error bars [message #70719] |
Tue, 04 May 2010 12:57  |
Meagan Adams
Messages: 1 Registered: May 2010
|
Junior Member |
|
|
Hello
I have two columns of data y (with errors Err+ and Err- for each data
point) and x (with Err). I would like to find the linear regression
coefficients A and B (in y=Ax+B) considering the errors in my data
point. Is their a function in IDL which would do the trick? and the
regression has to do with the errors in y only but not in x, right?
Thank you for your help.
|
|
|
Re: regression with error bars [message #70812 is a reply to message #70719] |
Tue, 04 May 2010 19:19   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On May 4, 3:57 pm, Meagan Adams <meagan....@gmail.com> wrote:
> Hello
>
> I have two columns of data y (with errors Err+ and Err- for each data
> point) and x (with Err). I would like to find the linear regression
> coefficients A and B (in y=Ax+B) considering the errors in my data
> point. Is their a function in IDL which would do the trick? and the
> regression has to do with the errors in y only but not in x, right?
I don't believe there is a built-in function in IDL which can do this.
You can use MPFIT. The trick is that you will need to be able to form
your own scaled residual. Normally you would have a user function
something like this,
FUNCTION MYFUNCT, P, X=X, Y=Y, ERROR=ERROR
MODEL = F(X,P) ;; User model
RETURN, (Y - MODEL)/ERROR
END
but now you will need to reformulate as,
FUNCTION MYFUNCT, P, X=X, Y=Y, ERROR_PLUS=ERROR_PLUS,
ERROR_MINUS=ERROR_MINUS
MODEL = F(X,P) ;; User model
RESID_PLUS = (Y - MODEL) / ERROR_PLUS ;; Residual for positive
values
RESID_MINUS = (Y - MODEL) / ERROR_MINUS ;; Residual for negative
values
;; Combine the two kinds of residuals
RESID = (RESID_PLUS GT 0)*RESID_PLUS + (RESID_MINUS LT
0)*RESID_MINUS
RETURN, RESID
END
Happy fitting,
Craig
|
|
|
|
|
Re: regression with error bars [message #70921 is a reply to message #70719] |
Sat, 22 May 2010 08:02   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On May 22, 9:34 am, Meagan A. <u...@compgroups.net/> wrote:
> Hello,
>
> Thank you for your reply and examples. I am a newbie to IDL and I've never used MPFIT. I read the tutorial from this website:http://cow.physics.wisc.edu/~craigm/idl/mpfittut.htm l
>
> and I wrote a pro to fit a line to x and y if we had errors in y only (+/-):
>
> pro test
>
> xval = [1,2,3,4,5]
> yval = [1.2,1.9, 3.1, 4.5, 5]
> errval = [0.1,0.001,0.1,0.05,0.2]
>
> expr = 'P[0]+P[1]*X' ;line y=A+Bx
>
> result = MPFITEXPR(expr, xval, yval,errval)
>
> plot, xval, yval, psym=2
> oplot, xval, result[0]+result[1]*xval
>
> l=linfit(xval, yval) ;no errors considered
> oplot, xval, l[0]+l[1]*xval, linestyle=1
>
> end
>
> Are the error values (errval) used in this way in the function MPFITEXPR? And if not, after I calculate the residuals using your method, how do I feed it into the function MPFITFUN ?
There are several MPFIT* functions. You can't use MPFITFUN or
MPFITEXPR because you are changing the standard definition of
"residual" because you have different + and - error bars. Instead you
will need to use the core engine MPFIT(), and you will need to write a
user function like I described in my previous post.
Craig
|
|
|
Re: regression with error bars [message #70922 is a reply to message #70719] |
Sat, 22 May 2010 06:34   |
Giuseppe Papa
Messages: 27 Registered: February 2010
|
Junior Member |
|
|
Hello,
Thank you for your reply and examples. I am a newbie to IDL and I've never used MPFIT. I read the tutorial from this website:
http://cow.physics.wisc.edu/~craigm/idl/mpfittut.html
and I wrote a pro to fit a line to x and y if we had errors in y only (+/-):
pro test
xval = [1,2,3,4,5]
yval = [1.2,1.9, 3.1, 4.5, 5]
errval = [0.1,0.001,0.1,0.05,0.2]
expr = 'P[0]+P[1]*X' ;line y=A+Bx
result = MPFITEXPR(expr, xval, yval,errval)
plot, xval, yval, psym=2
oplot, xval, result[0]+result[1]*xval
l=linfit(xval, yval) ;no errors considered
oplot, xval, l[0]+l[1]*xval, linestyle=1
end
Are the error values (errval) used in this way in the function MPFITEXPR? And if not, after I calculate the residuals using your method, how do I feed it into the function MPFITFUN ?
Thank you!!
---
frmsrcurl: http://compgroups.net/comp.lang.idl-pvwave/regression-with-e rror-bars
|
|
|
Re: regression with error bars [message #71067 is a reply to message #70918] |
Sat, 22 May 2010 10:53  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On May 22, 12:44 pm, Meagan A. <u...@compgroups.net/> wrote:
> One more question, please.
>
> In:
> RESID = (RESID_PLUS GT 0)*RESID_PLUS + (RESID_MINUS LT
> 0)*RESID_MINUS
>
> Shouldn't we subtract the two resids instead of adding them? Thank you again!
Not really. The "addition" there is an IDL vectorized short-hand
notation for, "if residuals are greater than zero use RESID_PLUS;
otherwise use RESID_MINUS". Only one clause of expression is non-
zero, so you are never really adding or subtracting residual values.
Craig
|
|
|