picewise function in nls....

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

picewise function in nls....

akshay kulkarni
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: picewise function in nls....

Ivan Krylov
On Thu, 18 Apr 2019 10:36:10 +0000
akshay kulkarni <[hidden email]> wrote:

> 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))   ?

For now, you can, since fx does not depend on any of the parameters you
optimize in the nls() call. (Actually, the model as presented should be
solveable by lm(y ~ I(sin(x2) + fx) + 0), since it is linear in its
only parameter.)

If you make fx a function and use ifelse() to provide different
outcomes depending on a condition in a vectorized fashion, you would
make it easier to add new parameters later, should the need arise:

fx <- function(x1, x2)
        ifelse(x1 <= -2, EXPR_IF_TRUE..., EXPR_IF_FALSE...)

NLS1 <- nls(y ~ a*(sin(x2) + fx(x1, x2)), ...)

--
Best regards,
Ivan

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: picewise function in nls....

akshay kulkarni
dear Ivan,
                   THanks for the reply......But what do you mean by "since fx does not depend on any of the parameters you
optimize in the nls() call."? Can you give an example?

very many thanks for your time and effort.....
Yours sincerely,

AKSHAY M KULKARNI



________________________________
From: Ivan Krylov <[hidden email]>
Sent: Thursday, April 18, 2019 4:50 PM
To: akshay kulkarni
Cc: R help Mailing list
Subject: Re: [R] picewise function in nls....

On Thu, 18 Apr 2019 10:36:10 +0000
akshay kulkarni <[hidden email]> wrote:

> 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))   ?

For now, you can, since fx does not depend on any of the parameters you
optimize in the nls() call. (Actually, the model as presented should be
solveable by lm(y ~ I(sin(x2) + fx) + 0), since it is linear in its
only parameter.)

If you make fx a function and use ifelse() to provide different
outcomes depending on a condition in a vectorized fashion, you would
make it easier to add new parameters later, should the need arise:

fx <- function(x1, x2)
        ifelse(x1 <= -2, EXPR_IF_TRUE..., EXPR_IF_FALSE...)

NLS1 <- nls(y ~ a*(sin(x2) + fx(x1, x2)), ...)

--
Best regards,
Ivan

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: picewise function in nls....

Ivan Krylov
On Fri, 19 Apr 2019 10:12:06 +0000
akshay kulkarni <[hidden email]> wrote:

> But what do you mean by "since fx does not depend on any of the
> parameters you optimize in the nls() call."? Can you give an example?

By "parameters you optimize in the nls() call" I mean `a`. `a` does
not seem to be used in the calculation of `fx`. If it were, it would
have to look like:

fx <- function(x1, x2, a) { ... }
nls(y ~ a*(sin(x2) + fx(x1, x2, a)), start = list(a = ...))

so that nls() would be able to check different values of `a`.

--
Best regards,
Ivan

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: picewise function in nls....

akshay kulkarni
dear Ivan,
                       Thanks a lot....!

yours sincerely,
AKSHAY M KULKARNI

________________________________
From: Ivan Krylov <[hidden email]>
Sent: Friday, April 19, 2019 3:59 PM
To: akshay kulkarni
Cc: R help Mailing list
Subject: Re: [R] picewise function in nls....

On Fri, 19 Apr 2019 10:12:06 +0000
akshay kulkarni <[hidden email]> wrote:

> But what do you mean by "since fx does not depend on any of the
> parameters you optimize in the nls() call."? Can you give an example?

By "parameters you optimize in the nls() call" I mean `a`. `a` does
not seem to be used in the calculation of `fx`. If it were, it would
have to look like:

fx <- function(x1, x2, a) { ... }
nls(y ~ a*(sin(x2) + fx(x1, x2, a)), start = list(a = ...))

so that nls() would be able to check different values of `a`.

--
Best regards,
Ivan

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.