[R] picewise function in nls....
akshay kulkarni
@k@h@y_e4 @end|ng |rom hotm@||@com
Thu Apr 18 12:36:10 CEST 2019
dear members,
I have two predictors, x1 and x2, and one response variable, y. Moreover, a step function models the relationship between y and x1:
fx <- (x1 <= -2)*(x1^2) + (x1 > -2 && x1 < 2)*(x1^3) + (x1 > = 2)*(x1^4)
Can I include fx in an nls call to create something like this:
NLS1 <- nls(y ~ a*(sin(x2) + fx), start = list(a = 2)) ?
Will the above work? Or should I do something like this:
if(x1 <= -2) {
NLS2 <- nls(y[x1 <= -2] ~ a*(sin(x2[x1 <= -2]) + x1^2),start = list(a = 3)) }
if(x1 > -2 && x1 < 2) {
NLS3 <- nls(y[x1 > -2 && x1 < 2] ~ a*(sin(x2[x1 > -2 && x1 < 2]) + x1^3),start = list(a = 4)) }
if(x1 > = 2) {
NLS4 <- nls(y[x1 >= 2] ~ a*(sin(x2[x1 >= 2]) + x1^4),start = list(a = 5)) }
If the first case doesn't work, is there any other method to include the step function in the nls call without resorting to the if statements?
Also, the coefficient, a , gotten from the above two cases are different. Will this affect the precision of the nonlinear fir between
y ~ (f(x1),f(x2)) ?
very many thanks for your time and effort
yours sincerely,
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
More information about the R-help
mailing list