How to retriev V_r2 in FuncFit

Hi I am using

Function

straightline(w,x) :Fitfunc

WAVE w ; Variable x

return w[0]+w[1]*x

End

 

in a looped FuncFit command in a procedure. I could not use the CurveFit command  - there seem to be some complication in my particular code that only work with FuncFit. So Far so good. How can I retrieve the V_r2 variable here? it does not seem to be generated automatically as during the CurveFit command.

Thanks 

 

Remo

CurveFit can work in a function:

Function DoStuff()
    Make/O/D wData = {1,2,4,3,5,6}
    Display wData
    ModifyGraph mode=3
    CurveFit/NTHR=0/TBOX=768 line  wData  /D
    Variable/G Vr2 = V_r2
End

Note that in the above, V_r2 is a local variable when CurveFit is run in a function, whereas it is global if run interactively.

DisplayHelpTopic("Special Variables for Curve Fitting")

So I simply copied it to a global variable in the above code.

Hope this helps.

EDIT:

Here is code that works with FuncFit - note that V_r2 is not calculated directly from the fit - I used StatsLinearRegression.

Function straightline(w, x) :FitFunc
    Wave w
    Variable x
    return w[0] + w[1] * x
End

Function DoStuff()
    Make/O/D wData = {1,2,4,3,5,6}
    Display wData
    ModifyGraph mode=3
    Make/D W_coef = {0,1}
    FuncFit/NTHR=0 straightline W_coef  wData  /D
    StatsLinearRegression wData
    Wave/D W_StatsLinearRegression
    Variable/G Vr2 = W_StatsLinearRegression[0][%r2]
End

 

The assumption is that a user-defined fit function is nonlinear, and the r-squared statistic is not valid. You can compute it pretty easily after the fact; it helps to generate the residual wave from your fit as a start.

But you should be able to use CurveFit line in your code- what went wrong?

Thanks for the replies, 

The issue with using CurveFit instead of FuncFit is that I get a syntax error for the former in the expression that defines my X range as a function of two variables:

FuncFit  straightline W_coef y_wave[iNr,iNr+nrX] /X= x_wave[iNr,iNr+nrX]
         
Curvefit  line        W_coef y_wave[iNr,iNr+nrX] /X= x_wave[iNr,iNr+nrX]

Remo

CurveFit requires usage of the "kwCWavef" keyword if an explicit coefficient wave is given

Curvefit line, kwCWave=W_coef y_wave[10,50] /X= x_wave[10,50]

Have a close look on the definitions of FuncFit and CurveFit in the manual

HJ

Ah I see, thanks for that hint HJ.

Now I can use Curvefit and the issue with the V_r2 is resolved.

 

Thanks!