nls() in R: Missing value or an infinity produced when evaluating the model





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to use nls(), but the error in the question was made.



Following is the sample data set resembles the original one:



rh1 = rnorm(301, 0.75, 0.1)
rh1[rh1 > 1] = 1
ta1 = rnorm(301, 302, 3)
y1 = rnorm(301, 0.2, 0.05)

df_test = data.frame(rh1 = rh1,
rh2 = c(NA, rh1[-c(1)]),
ta1 = ta1,
ta2 = c(NA, ta1[-c(1)]),
y1 = y1,
y2 = c(NA, y1[-c(1)]))
df_test = df_test[-c(1), ] # this function cannot estimate for the first value


where rh is relative humidity of the air,
ta is air temperature in K,
and y is moisture content of an object. 1 means today's value; 2 means yesterday's value.



I'm trying to estimate y using y2, rh1&2 and ta1&2 by a model below:



nls(y1 ~
coef1 ^ 2 * y2 +
coef1 * (1 - coef1) *
(coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
(1 - coef1) *
(coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
data = df_test,
algorithm = "port",
start = list(coef1 = 0.7,
coef2 = 0.15,
coef3 = 0),
upper = c(exp(-0.00005), Inf, Inf),
lower = c(exp(-0.5), Inf, Inf))


Coef1, 2, and 3 are the parameters to be estimated.



The initial values were determined by manual calculation for the first row of the data.



But this script made the error in the title.




Missing value or an infinity produced when evaluating the model




I also tried using minpack.lm::nlsLM() function according to the link below:



nls troubles: Missing value or an infinity produced when evaluating the model



library(minpack.lm)
nlsLM(y1 ~
coef1 ^ 2 * y2 +
coef1 * (1 - coef1) *
(coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
(1 - coef1) *
(coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
data = df_test,
start = list(coef1 = 0.7,
coef2 = 0.15,
coef3 = 0),
upper = c(exp(-0.00005), Inf, Inf),
lower = c(exp(-0.5), Inf, Inf))


but still got the same error.










share|improve this question





























    1















    I'm trying to use nls(), but the error in the question was made.



    Following is the sample data set resembles the original one:



    rh1 = rnorm(301, 0.75, 0.1)
    rh1[rh1 > 1] = 1
    ta1 = rnorm(301, 302, 3)
    y1 = rnorm(301, 0.2, 0.05)

    df_test = data.frame(rh1 = rh1,
    rh2 = c(NA, rh1[-c(1)]),
    ta1 = ta1,
    ta2 = c(NA, ta1[-c(1)]),
    y1 = y1,
    y2 = c(NA, y1[-c(1)]))
    df_test = df_test[-c(1), ] # this function cannot estimate for the first value


    where rh is relative humidity of the air,
    ta is air temperature in K,
    and y is moisture content of an object. 1 means today's value; 2 means yesterday's value.



    I'm trying to estimate y using y2, rh1&2 and ta1&2 by a model below:



    nls(y1 ~
    coef1 ^ 2 * y2 +
    coef1 * (1 - coef1) *
    (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
    (1 - coef1) *
    (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
    data = df_test,
    algorithm = "port",
    start = list(coef1 = 0.7,
    coef2 = 0.15,
    coef3 = 0),
    upper = c(exp(-0.00005), Inf, Inf),
    lower = c(exp(-0.5), Inf, Inf))


    Coef1, 2, and 3 are the parameters to be estimated.



    The initial values were determined by manual calculation for the first row of the data.



    But this script made the error in the title.




    Missing value or an infinity produced when evaluating the model




    I also tried using minpack.lm::nlsLM() function according to the link below:



    nls troubles: Missing value or an infinity produced when evaluating the model



    library(minpack.lm)
    nlsLM(y1 ~
    coef1 ^ 2 * y2 +
    coef1 * (1 - coef1) *
    (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
    (1 - coef1) *
    (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
    data = df_test,
    start = list(coef1 = 0.7,
    coef2 = 0.15,
    coef3 = 0),
    upper = c(exp(-0.00005), Inf, Inf),
    lower = c(exp(-0.5), Inf, Inf))


    but still got the same error.










    share|improve this question

























      1












      1








      1








      I'm trying to use nls(), but the error in the question was made.



      Following is the sample data set resembles the original one:



      rh1 = rnorm(301, 0.75, 0.1)
      rh1[rh1 > 1] = 1
      ta1 = rnorm(301, 302, 3)
      y1 = rnorm(301, 0.2, 0.05)

      df_test = data.frame(rh1 = rh1,
      rh2 = c(NA, rh1[-c(1)]),
      ta1 = ta1,
      ta2 = c(NA, ta1[-c(1)]),
      y1 = y1,
      y2 = c(NA, y1[-c(1)]))
      df_test = df_test[-c(1), ] # this function cannot estimate for the first value


      where rh is relative humidity of the air,
      ta is air temperature in K,
      and y is moisture content of an object. 1 means today's value; 2 means yesterday's value.



      I'm trying to estimate y using y2, rh1&2 and ta1&2 by a model below:



      nls(y1 ~
      coef1 ^ 2 * y2 +
      coef1 * (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
      (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
      data = df_test,
      algorithm = "port",
      start = list(coef1 = 0.7,
      coef2 = 0.15,
      coef3 = 0),
      upper = c(exp(-0.00005), Inf, Inf),
      lower = c(exp(-0.5), Inf, Inf))


      Coef1, 2, and 3 are the parameters to be estimated.



      The initial values were determined by manual calculation for the first row of the data.



      But this script made the error in the title.




      Missing value or an infinity produced when evaluating the model




      I also tried using minpack.lm::nlsLM() function according to the link below:



      nls troubles: Missing value or an infinity produced when evaluating the model



      library(minpack.lm)
      nlsLM(y1 ~
      coef1 ^ 2 * y2 +
      coef1 * (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
      (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
      data = df_test,
      start = list(coef1 = 0.7,
      coef2 = 0.15,
      coef3 = 0),
      upper = c(exp(-0.00005), Inf, Inf),
      lower = c(exp(-0.5), Inf, Inf))


      but still got the same error.










      share|improve this question














      I'm trying to use nls(), but the error in the question was made.



      Following is the sample data set resembles the original one:



      rh1 = rnorm(301, 0.75, 0.1)
      rh1[rh1 > 1] = 1
      ta1 = rnorm(301, 302, 3)
      y1 = rnorm(301, 0.2, 0.05)

      df_test = data.frame(rh1 = rh1,
      rh2 = c(NA, rh1[-c(1)]),
      ta1 = ta1,
      ta2 = c(NA, ta1[-c(1)]),
      y1 = y1,
      y2 = c(NA, y1[-c(1)]))
      df_test = df_test[-c(1), ] # this function cannot estimate for the first value


      where rh is relative humidity of the air,
      ta is air temperature in K,
      and y is moisture content of an object. 1 means today's value; 2 means yesterday's value.



      I'm trying to estimate y using y2, rh1&2 and ta1&2 by a model below:



      nls(y1 ~
      coef1 ^ 2 * y2 +
      coef1 * (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
      (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
      data = df_test,
      algorithm = "port",
      start = list(coef1 = 0.7,
      coef2 = 0.15,
      coef3 = 0),
      upper = c(exp(-0.00005), Inf, Inf),
      lower = c(exp(-0.5), Inf, Inf))


      Coef1, 2, and 3 are the parameters to be estimated.



      The initial values were determined by manual calculation for the first row of the data.



      But this script made the error in the title.




      Missing value or an infinity produced when evaluating the model




      I also tried using minpack.lm::nlsLM() function according to the link below:



      nls troubles: Missing value or an infinity produced when evaluating the model



      library(minpack.lm)
      nlsLM(y1 ~
      coef1 ^ 2 * y2 +
      coef1 * (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
      (1 - coef1) *
      (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
      data = df_test,
      start = list(coef1 = 0.7,
      coef2 = 0.15,
      coef3 = 0),
      upper = c(exp(-0.00005), Inf, Inf),
      lower = c(exp(-0.5), Inf, Inf))


      but still got the same error.







      r nls






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 5:34









      HoonTaekHoonTaek

      348




      348
























          1 Answer
          1






          active

          oldest

          votes


















          2














          There are several issues here.



          First off: your lagged values aren't really lagged. Take a look at df_test and you will se that the 1's and 2's are identical.

          This will give you lagged values:



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 1] <- 1
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          Next:




          Missing value or an infinity produced when evaluating the model




          Means just that, and my eyes immediately fix on the logs in your expression. We all know that taking the log of a negative number is undefined, as is the log of 0, although it is often returned as infinity.



          Let's take a look at those expressions



          ex1 <- with(df_test, log(-8.2 * ta2 * log(rh2) / 18))
          ex2 <- with(df_test, log(-8.3 * ta1 * log(rh1) / 18))


          If you look at ex1 and ex2 you will see that both contain a -Inf. Now there's your culprit. But how can we fix this? Let's see which rows in your data gives rise to this.



          df_test[which(is.infinite(ex1 + ex2)),]
          # rh1 rh2 ta1 ta2 y1 y2
          # 274 1.0000 0.66481 304.5453 300.5972 0.20930 0.17474
          # 275 0.7656 1.00000 304.9603 304.5453 0.20882 0.20930


          Interesting, they are right next to each other, and they both contain a 1. What's log(1)? What happens if you multiply it by something and take the log of the product?



          Let's make sure rh1 and rh2 is always less than 1



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 0.99] <- 0.99
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          But we're still not done. If you run your nls() call now you'll get the error




          Convergence failure: initial par violates constraints




          And the cause is obvious if you look at the values you specify for your coefficients constraints. coef2 and coef3 has lower constraints set to infinity! That doesn't make sense. "initial par violates constraints" usually means that the start values aren't within the constraints, which is definitely the case here. If we change them to negative infinity everything works fine.



          nls(y1 ~
          coef1 ^ 2 * y2 +
          coef1 * (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
          (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
          data = df_test,
          algorithm = "port",
          start = list(coef1 = 0.7,
          coef2 = 0.15,
          coef3 = 0),
          upper = c(exp(-0.00005), Inf, Inf),
          lower = c(exp(-0.5), -Inf, -Inf)
          )
          # Nonlinear regression model
          # model: y1 ~ coef1^2 * y2 + coef1 * (1 - coef1) * (coef2 + coef3 * log(…
          # data: df_test
          # coef1 coef2 coef3
          # 0.6065 0.2569 -0.0170
          # residual sum-of-squares: 1.058

          # Algorithm "port", convergence message:
          # both X-convergence and relative convergence (5)





          share|improve this answer
























          • Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

            – HoonTaek
            Nov 23 '18 at 0:29












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424459%2fnls-in-r-missing-value-or-an-infinity-produced-when-evaluating-the-model%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          There are several issues here.



          First off: your lagged values aren't really lagged. Take a look at df_test and you will se that the 1's and 2's are identical.

          This will give you lagged values:



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 1] <- 1
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          Next:




          Missing value or an infinity produced when evaluating the model




          Means just that, and my eyes immediately fix on the logs in your expression. We all know that taking the log of a negative number is undefined, as is the log of 0, although it is often returned as infinity.



          Let's take a look at those expressions



          ex1 <- with(df_test, log(-8.2 * ta2 * log(rh2) / 18))
          ex2 <- with(df_test, log(-8.3 * ta1 * log(rh1) / 18))


          If you look at ex1 and ex2 you will see that both contain a -Inf. Now there's your culprit. But how can we fix this? Let's see which rows in your data gives rise to this.



          df_test[which(is.infinite(ex1 + ex2)),]
          # rh1 rh2 ta1 ta2 y1 y2
          # 274 1.0000 0.66481 304.5453 300.5972 0.20930 0.17474
          # 275 0.7656 1.00000 304.9603 304.5453 0.20882 0.20930


          Interesting, they are right next to each other, and they both contain a 1. What's log(1)? What happens if you multiply it by something and take the log of the product?



          Let's make sure rh1 and rh2 is always less than 1



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 0.99] <- 0.99
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          But we're still not done. If you run your nls() call now you'll get the error




          Convergence failure: initial par violates constraints




          And the cause is obvious if you look at the values you specify for your coefficients constraints. coef2 and coef3 has lower constraints set to infinity! That doesn't make sense. "initial par violates constraints" usually means that the start values aren't within the constraints, which is definitely the case here. If we change them to negative infinity everything works fine.



          nls(y1 ~
          coef1 ^ 2 * y2 +
          coef1 * (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
          (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
          data = df_test,
          algorithm = "port",
          start = list(coef1 = 0.7,
          coef2 = 0.15,
          coef3 = 0),
          upper = c(exp(-0.00005), Inf, Inf),
          lower = c(exp(-0.5), -Inf, -Inf)
          )
          # Nonlinear regression model
          # model: y1 ~ coef1^2 * y2 + coef1 * (1 - coef1) * (coef2 + coef3 * log(…
          # data: df_test
          # coef1 coef2 coef3
          # 0.6065 0.2569 -0.0170
          # residual sum-of-squares: 1.058

          # Algorithm "port", convergence message:
          # both X-convergence and relative convergence (5)





          share|improve this answer
























          • Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

            – HoonTaek
            Nov 23 '18 at 0:29
















          2














          There are several issues here.



          First off: your lagged values aren't really lagged. Take a look at df_test and you will se that the 1's and 2's are identical.

          This will give you lagged values:



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 1] <- 1
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          Next:




          Missing value or an infinity produced when evaluating the model




          Means just that, and my eyes immediately fix on the logs in your expression. We all know that taking the log of a negative number is undefined, as is the log of 0, although it is often returned as infinity.



          Let's take a look at those expressions



          ex1 <- with(df_test, log(-8.2 * ta2 * log(rh2) / 18))
          ex2 <- with(df_test, log(-8.3 * ta1 * log(rh1) / 18))


          If you look at ex1 and ex2 you will see that both contain a -Inf. Now there's your culprit. But how can we fix this? Let's see which rows in your data gives rise to this.



          df_test[which(is.infinite(ex1 + ex2)),]
          # rh1 rh2 ta1 ta2 y1 y2
          # 274 1.0000 0.66481 304.5453 300.5972 0.20930 0.17474
          # 275 0.7656 1.00000 304.9603 304.5453 0.20882 0.20930


          Interesting, they are right next to each other, and they both contain a 1. What's log(1)? What happens if you multiply it by something and take the log of the product?



          Let's make sure rh1 and rh2 is always less than 1



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 0.99] <- 0.99
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          But we're still not done. If you run your nls() call now you'll get the error




          Convergence failure: initial par violates constraints




          And the cause is obvious if you look at the values you specify for your coefficients constraints. coef2 and coef3 has lower constraints set to infinity! That doesn't make sense. "initial par violates constraints" usually means that the start values aren't within the constraints, which is definitely the case here. If we change them to negative infinity everything works fine.



          nls(y1 ~
          coef1 ^ 2 * y2 +
          coef1 * (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
          (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
          data = df_test,
          algorithm = "port",
          start = list(coef1 = 0.7,
          coef2 = 0.15,
          coef3 = 0),
          upper = c(exp(-0.00005), Inf, Inf),
          lower = c(exp(-0.5), -Inf, -Inf)
          )
          # Nonlinear regression model
          # model: y1 ~ coef1^2 * y2 + coef1 * (1 - coef1) * (coef2 + coef3 * log(…
          # data: df_test
          # coef1 coef2 coef3
          # 0.6065 0.2569 -0.0170
          # residual sum-of-squares: 1.058

          # Algorithm "port", convergence message:
          # both X-convergence and relative convergence (5)





          share|improve this answer
























          • Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

            – HoonTaek
            Nov 23 '18 at 0:29














          2












          2








          2







          There are several issues here.



          First off: your lagged values aren't really lagged. Take a look at df_test and you will se that the 1's and 2's are identical.

          This will give you lagged values:



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 1] <- 1
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          Next:




          Missing value or an infinity produced when evaluating the model




          Means just that, and my eyes immediately fix on the logs in your expression. We all know that taking the log of a negative number is undefined, as is the log of 0, although it is often returned as infinity.



          Let's take a look at those expressions



          ex1 <- with(df_test, log(-8.2 * ta2 * log(rh2) / 18))
          ex2 <- with(df_test, log(-8.3 * ta1 * log(rh1) / 18))


          If you look at ex1 and ex2 you will see that both contain a -Inf. Now there's your culprit. But how can we fix this? Let's see which rows in your data gives rise to this.



          df_test[which(is.infinite(ex1 + ex2)),]
          # rh1 rh2 ta1 ta2 y1 y2
          # 274 1.0000 0.66481 304.5453 300.5972 0.20930 0.17474
          # 275 0.7656 1.00000 304.9603 304.5453 0.20882 0.20930


          Interesting, they are right next to each other, and they both contain a 1. What's log(1)? What happens if you multiply it by something and take the log of the product?



          Let's make sure rh1 and rh2 is always less than 1



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 0.99] <- 0.99
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          But we're still not done. If you run your nls() call now you'll get the error




          Convergence failure: initial par violates constraints




          And the cause is obvious if you look at the values you specify for your coefficients constraints. coef2 and coef3 has lower constraints set to infinity! That doesn't make sense. "initial par violates constraints" usually means that the start values aren't within the constraints, which is definitely the case here. If we change them to negative infinity everything works fine.



          nls(y1 ~
          coef1 ^ 2 * y2 +
          coef1 * (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
          (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
          data = df_test,
          algorithm = "port",
          start = list(coef1 = 0.7,
          coef2 = 0.15,
          coef3 = 0),
          upper = c(exp(-0.00005), Inf, Inf),
          lower = c(exp(-0.5), -Inf, -Inf)
          )
          # Nonlinear regression model
          # model: y1 ~ coef1^2 * y2 + coef1 * (1 - coef1) * (coef2 + coef3 * log(…
          # data: df_test
          # coef1 coef2 coef3
          # 0.6065 0.2569 -0.0170
          # residual sum-of-squares: 1.058

          # Algorithm "port", convergence message:
          # both X-convergence and relative convergence (5)





          share|improve this answer













          There are several issues here.



          First off: your lagged values aren't really lagged. Take a look at df_test and you will se that the 1's and 2's are identical.

          This will give you lagged values:



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 1] <- 1
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          Next:




          Missing value or an infinity produced when evaluating the model




          Means just that, and my eyes immediately fix on the logs in your expression. We all know that taking the log of a negative number is undefined, as is the log of 0, although it is often returned as infinity.



          Let's take a look at those expressions



          ex1 <- with(df_test, log(-8.2 * ta2 * log(rh2) / 18))
          ex2 <- with(df_test, log(-8.3 * ta1 * log(rh1) / 18))


          If you look at ex1 and ex2 you will see that both contain a -Inf. Now there's your culprit. But how can we fix this? Let's see which rows in your data gives rise to this.



          df_test[which(is.infinite(ex1 + ex2)),]
          # rh1 rh2 ta1 ta2 y1 y2
          # 274 1.0000 0.66481 304.5453 300.5972 0.20930 0.17474
          # 275 0.7656 1.00000 304.9603 304.5453 0.20882 0.20930


          Interesting, they are right next to each other, and they both contain a 1. What's log(1)? What happens if you multiply it by something and take the log of the product?



          Let's make sure rh1 and rh2 is always less than 1



          set.seed(1)

          rh1 <- rnorm(301, 0.75, 0.1)
          rh1[rh1 > 0.99] <- 0.99
          ta1 <- rnorm(301, 302, 3)
          y1 <- rnorm(301, 0.2, 0.05)

          df_test <- data.frame(
          rh1 = rh1,
          rh2 = c(NA, head(rh1, -1)),
          ta1 = ta1,
          ta2 = c(NA, head(ta1, -1)),
          y1 = y1,
          y2 = c(NA, head(y1, -1))
          )
          df_test <- df_test[complete.cases(df_test), ]


          But we're still not done. If you run your nls() call now you'll get the error




          Convergence failure: initial par violates constraints




          And the cause is obvious if you look at the values you specify for your coefficients constraints. coef2 and coef3 has lower constraints set to infinity! That doesn't make sense. "initial par violates constraints" usually means that the start values aren't within the constraints, which is definitely the case here. If we change them to negative infinity everything works fine.



          nls(y1 ~
          coef1 ^ 2 * y2 +
          coef1 * (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta2 * log(rh2) / 18)) +
          (1 - coef1) *
          (coef2 + coef3 * log(-8.3 * ta1 * log(rh1) / 18)),
          data = df_test,
          algorithm = "port",
          start = list(coef1 = 0.7,
          coef2 = 0.15,
          coef3 = 0),
          upper = c(exp(-0.00005), Inf, Inf),
          lower = c(exp(-0.5), -Inf, -Inf)
          )
          # Nonlinear regression model
          # model: y1 ~ coef1^2 * y2 + coef1 * (1 - coef1) * (coef2 + coef3 * log(…
          # data: df_test
          # coef1 coef2 coef3
          # 0.6065 0.2569 -0.0170
          # residual sum-of-squares: 1.058

          # Algorithm "port", convergence message:
          # both X-convergence and relative convergence (5)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 12:44









          AkselAAkselA

          4,73421326




          4,73421326













          • Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

            – HoonTaek
            Nov 23 '18 at 0:29



















          • Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

            – HoonTaek
            Nov 23 '18 at 0:29

















          Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

          – HoonTaek
          Nov 23 '18 at 0:29





          Thank you for pointing out my mistakes and kind explanation. I couldn't consider rh 100% would cause the critical problem. Now the problem is solved correctly. Thank you again.

          – HoonTaek
          Nov 23 '18 at 0:29




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424459%2fnls-in-r-missing-value-or-an-infinity-produced-when-evaluating-the-model%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?