Categorical Predictors in Regression Analyses

Hello,

I would like to perform orthogonal distance regression (/ODR=2) for a linear model with an extra categorical predictor. Ideally, the equation would be the following:

y = b0 + b1x1 + b2x1x2, where x2 = 0 or 1 and is a 'dummy variable' encoding whether or not the points belong to a specific category. Effectively, if the points belonged to group A final equation would be y =  b0 + b1x1 and if the points belonged to group B then the equation would be y = b0 + (b1+b2)x1

Can a custom fitting function with categorical predictors be employed and fit with ODR=2 in Igor? If so, how? 

I am only familiar with using the built in linear fit with the CurveFit command. Thank you!

Yes, you can write a user-defined fitting function to do this. The easy but error-prone way is to maintain a separate wave with the x2 flags. In your fitting function, do something like this:

Function myfit(Wave pw, Wave yw, Wave xw) : FitFunc
    WAVE x2w = root:x2vals      // or whatever data folder path you use
   
    yw = x2 == 1 ? <equation for x2 == 1> : <equation for x2 == 0>
    return 0
end

With this function, the x2vals wave must always have the same name and data folder path. That's inconvenient; the solution would be a global variable to give the path and name of the wave. The least error-prone way to handle that would be to write a wrapper function that calls FuncFit after first setting up all the infrastructure required.

A better way would be to use a structure fit function. That actually REQUIRES a wrapper function. It also eliminates the WAVE statement at the start, and makes it less likely that you will provide incorrect inputs.

Please note that if there are no x2==1 values, then the b2  coefficient will not be constrained and would have to be held to avoid a singular matrix error.

Yet another approach would be to split your data sets into two separate data waves, and use the Global Fit package. If you do that, then we do all the bookkeeping.

You would use SplitWave or Extract to separate your data sets. To learn more about Global Fit, go to the demo: File->Example Experiments->Curve Fitting->Global Fit Demo.

Thanks John. Could you advise me on how to then run the function you built using FuncFit? This is what I was able to make from your suggestions.

 

Function myfit(Wave pw, Wave yw, Wave xw)
    WAVE x2w = root:x2vals
    yw = x2w == 1 ? pw[0]+pw[1]*xw : pw[0]+(pw[1]+pw[2])*xw
    return 0
end

The global fit package seems like a very useful package that I would definitely want to use in the future.

 

Thank you!

Your function seems fine. It will be more convenient for curve fitting if you add the "FitFunc" keyword:

Function myfit(Wave pw, Wave yw, Wave xw): FitFunc

Are you asking how to write the wrapper function I recommend?

Hi John,

I was just having trouble running it with the Curve Fit analysis GUI, but adding this did the trick! Thank you.