Conditional Logic (Nonlinear Regression)
You can specify a segmented model using conditional logic. To use conditional logic within a model expression or a loss function, you form the sum of a series of terms, one for each condition. Each term consists of a logical expression (in parentheses) multiplied by the expression that should result when that logical expression is true.
For example, consider a segmented model that equals 0 for X<=0, X for 0<X<1, and 1 for X>=1. The expression for this is:
(X<=0)*0 + (X>0 & X<1)*X + (X>=1)*1.
The logical expressions in parentheses all evaluate to 1 (true) or 0 (false). Therefore:
If X<=0, the above reduces to 1*0 + 0*X + 0*1 = 0.
If 0<X<1, it reduces to 0*0 + 1*X + 0*1 = X.
If X>=1, it reduces to 0*0 + 0*X + 1*1 = 1.
More complicated examples can be easily built by substituting different logical expressions and outcome expressions. Remember that double inequalities, such as 0<X<1, must be written as compound expressions, such as (X>0 & X<1).
String variables can be used within logical expressions:
(city='New York')*costliv + (city='Des Moines')*0.59*costliv
This yields one expression (the value of the variable costliv) for New Yorkers and another (59% of that value) for Des Moines residents. String constants must be enclosed in quotation marks or apostrophes, as shown here.