Why can't I fit a curve to this plot?












1















I am trying to fit a curve to this plot, but am having trouble.



Code:



library(ggplot2)

data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))


plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")

lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))

lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")


Result:



Curve does not fit data










share|improve this question

























  • Are data$volume and predict(...) ordered how you want them?

    – mickey
    Nov 13 '18 at 14:36











  • Yes, they're ordered.

    – Luís Telles
    Nov 13 '18 at 14:53
















1















I am trying to fit a curve to this plot, but am having trouble.



Code:



library(ggplot2)

data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))


plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")

lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))

lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")


Result:



Curve does not fit data










share|improve this question

























  • Are data$volume and predict(...) ordered how you want them?

    – mickey
    Nov 13 '18 at 14:36











  • Yes, they're ordered.

    – Luís Telles
    Nov 13 '18 at 14:53














1












1








1








I am trying to fit a curve to this plot, but am having trouble.



Code:



library(ggplot2)

data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))


plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")

lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))

lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")


Result:



Curve does not fit data










share|improve this question
















I am trying to fit a curve to this plot, but am having trouble.



Code:



library(ggplot2)

data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))


plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")

lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))

lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")


Result:



Curve does not fit data







r plot curve






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:04









Ben Bolker

133k11223311




133k11223311










asked Nov 13 '18 at 14:30









Luís TellesLuís Telles

576212




576212













  • Are data$volume and predict(...) ordered how you want them?

    – mickey
    Nov 13 '18 at 14:36











  • Yes, they're ordered.

    – Luís Telles
    Nov 13 '18 at 14:53



















  • Are data$volume and predict(...) ordered how you want them?

    – mickey
    Nov 13 '18 at 14:36











  • Yes, they're ordered.

    – Luís Telles
    Nov 13 '18 at 14:53

















Are data$volume and predict(...) ordered how you want them?

– mickey
Nov 13 '18 at 14:36





Are data$volume and predict(...) ordered how you want them?

– mickey
Nov 13 '18 at 14:36













Yes, they're ordered.

– Luís Telles
Nov 13 '18 at 14:53





Yes, they're ordered.

– Luís Telles
Nov 13 '18 at 14:53












2 Answers
2






active

oldest

votes


















1














Use ggplot2: geom_smooth lm method



library(ggplot2)

df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))

# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")

# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()


enter image description hereenter image description here






share|improve this answer































    1














    Your main problem is using data$ inside your regression model: you should use just the names of the variables.



    dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
    volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
    lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
    data=dd)


    For a smoother curve, I computed the predicted value at a finer sequence of volume values:



    pframe <- data.frame(volume=seq(0,30,length=51))
    pframe$pressure <- predict(lm_fit,newdata=pframe)


    Now the picture:



    ## png("SO_poly.png")
    par(las=1,bty="l") ## cosmetic
    plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
    ylim=c(-100,500))

    with(pframe, lines(volume, pressure, col="red"))


    This doesn't look great, so I tried some other curve-fits.



    Log-linear fit:



    lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
    data=dd)
    pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
    with(pframe, lines(volume, lpressure, col="purple"))


    Exponential fit:



    glm_fit <- glm(pressure ~ poly(volume,2),
    family=gaussian(link="log"),
    data=dd)
    pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
    with(pframe, lines(volume, gpressure, col="blue"))
    ## dev.off()


    enter image description here



    You could also use ggplot2:



    library(ggplot2)
    ggplot(dd, aes(volume,pressure))+
    geom_point()+
    geom_smooth(method="lm",
    formula=y~poly(x,2))





    share|improve this answer























      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%2f53283261%2fwhy-cant-i-fit-a-curve-to-this-plot%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Use ggplot2: geom_smooth lm method



      library(ggplot2)

      df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
      volume = c(.25, .5, 1, 2, 4, 8, 16, 32))

      # Fit a regression line. Change the method if you want the exponential fitting
      ggplot(data = df, aes(x = pressure, y = volume)) +
      geom_point() +
      geom_smooth(method = "lm")

      # If you just want to connect the dots
      ggplot(data = df, aes(x = pressure, y = volume)) +
      geom_point() +
      geom_line()


      enter image description hereenter image description here






      share|improve this answer




























        1














        Use ggplot2: geom_smooth lm method



        library(ggplot2)

        df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
        volume = c(.25, .5, 1, 2, 4, 8, 16, 32))

        # Fit a regression line. Change the method if you want the exponential fitting
        ggplot(data = df, aes(x = pressure, y = volume)) +
        geom_point() +
        geom_smooth(method = "lm")

        # If you just want to connect the dots
        ggplot(data = df, aes(x = pressure, y = volume)) +
        geom_point() +
        geom_line()


        enter image description hereenter image description here






        share|improve this answer


























          1












          1








          1







          Use ggplot2: geom_smooth lm method



          library(ggplot2)

          df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
          volume = c(.25, .5, 1, 2, 4, 8, 16, 32))

          # Fit a regression line. Change the method if you want the exponential fitting
          ggplot(data = df, aes(x = pressure, y = volume)) +
          geom_point() +
          geom_smooth(method = "lm")

          # If you just want to connect the dots
          ggplot(data = df, aes(x = pressure, y = volume)) +
          geom_point() +
          geom_line()


          enter image description hereenter image description here






          share|improve this answer













          Use ggplot2: geom_smooth lm method



          library(ggplot2)

          df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
          volume = c(.25, .5, 1, 2, 4, 8, 16, 32))

          # Fit a regression line. Change the method if you want the exponential fitting
          ggplot(data = df, aes(x = pressure, y = volume)) +
          geom_point() +
          geom_smooth(method = "lm")

          # If you just want to connect the dots
          ggplot(data = df, aes(x = pressure, y = volume)) +
          geom_point() +
          geom_line()


          enter image description hereenter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 14:41









          kon_ukon_u

          1966




          1966

























              1














              Your main problem is using data$ inside your regression model: you should use just the names of the variables.



              dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
              volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
              lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
              data=dd)


              For a smoother curve, I computed the predicted value at a finer sequence of volume values:



              pframe <- data.frame(volume=seq(0,30,length=51))
              pframe$pressure <- predict(lm_fit,newdata=pframe)


              Now the picture:



              ## png("SO_poly.png")
              par(las=1,bty="l") ## cosmetic
              plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
              ylim=c(-100,500))

              with(pframe, lines(volume, pressure, col="red"))


              This doesn't look great, so I tried some other curve-fits.



              Log-linear fit:



              lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
              data=dd)
              pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
              with(pframe, lines(volume, lpressure, col="purple"))


              Exponential fit:



              glm_fit <- glm(pressure ~ poly(volume,2),
              family=gaussian(link="log"),
              data=dd)
              pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
              with(pframe, lines(volume, gpressure, col="blue"))
              ## dev.off()


              enter image description here



              You could also use ggplot2:



              library(ggplot2)
              ggplot(dd, aes(volume,pressure))+
              geom_point()+
              geom_smooth(method="lm",
              formula=y~poly(x,2))





              share|improve this answer




























                1














                Your main problem is using data$ inside your regression model: you should use just the names of the variables.



                dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
                volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
                lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
                data=dd)


                For a smoother curve, I computed the predicted value at a finer sequence of volume values:



                pframe <- data.frame(volume=seq(0,30,length=51))
                pframe$pressure <- predict(lm_fit,newdata=pframe)


                Now the picture:



                ## png("SO_poly.png")
                par(las=1,bty="l") ## cosmetic
                plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
                ylim=c(-100,500))

                with(pframe, lines(volume, pressure, col="red"))


                This doesn't look great, so I tried some other curve-fits.



                Log-linear fit:



                lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
                data=dd)
                pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
                with(pframe, lines(volume, lpressure, col="purple"))


                Exponential fit:



                glm_fit <- glm(pressure ~ poly(volume,2),
                family=gaussian(link="log"),
                data=dd)
                pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
                with(pframe, lines(volume, gpressure, col="blue"))
                ## dev.off()


                enter image description here



                You could also use ggplot2:



                library(ggplot2)
                ggplot(dd, aes(volume,pressure))+
                geom_point()+
                geom_smooth(method="lm",
                formula=y~poly(x,2))





                share|improve this answer


























                  1












                  1








                  1







                  Your main problem is using data$ inside your regression model: you should use just the names of the variables.



                  dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
                  volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
                  lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
                  data=dd)


                  For a smoother curve, I computed the predicted value at a finer sequence of volume values:



                  pframe <- data.frame(volume=seq(0,30,length=51))
                  pframe$pressure <- predict(lm_fit,newdata=pframe)


                  Now the picture:



                  ## png("SO_poly.png")
                  par(las=1,bty="l") ## cosmetic
                  plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
                  ylim=c(-100,500))

                  with(pframe, lines(volume, pressure, col="red"))


                  This doesn't look great, so I tried some other curve-fits.



                  Log-linear fit:



                  lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
                  data=dd)
                  pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
                  with(pframe, lines(volume, lpressure, col="purple"))


                  Exponential fit:



                  glm_fit <- glm(pressure ~ poly(volume,2),
                  family=gaussian(link="log"),
                  data=dd)
                  pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
                  with(pframe, lines(volume, gpressure, col="blue"))
                  ## dev.off()


                  enter image description here



                  You could also use ggplot2:



                  library(ggplot2)
                  ggplot(dd, aes(volume,pressure))+
                  geom_point()+
                  geom_smooth(method="lm",
                  formula=y~poly(x,2))





                  share|improve this answer













                  Your main problem is using data$ inside your regression model: you should use just the names of the variables.



                  dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
                  volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
                  lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
                  data=dd)


                  For a smoother curve, I computed the predicted value at a finer sequence of volume values:



                  pframe <- data.frame(volume=seq(0,30,length=51))
                  pframe$pressure <- predict(lm_fit,newdata=pframe)


                  Now the picture:



                  ## png("SO_poly.png")
                  par(las=1,bty="l") ## cosmetic
                  plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
                  ylim=c(-100,500))

                  with(pframe, lines(volume, pressure, col="red"))


                  This doesn't look great, so I tried some other curve-fits.



                  Log-linear fit:



                  lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
                  data=dd)
                  pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
                  with(pframe, lines(volume, lpressure, col="purple"))


                  Exponential fit:



                  glm_fit <- glm(pressure ~ poly(volume,2),
                  family=gaussian(link="log"),
                  data=dd)
                  pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
                  with(pframe, lines(volume, gpressure, col="blue"))
                  ## dev.off()


                  enter image description here



                  You could also use ggplot2:



                  library(ggplot2)
                  ggplot(dd, aes(volume,pressure))+
                  geom_point()+
                  geom_smooth(method="lm",
                  formula=y~poly(x,2))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 15:04









                  Ben BolkerBen Bolker

                  133k11223311




                  133k11223311






























                      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%2f53283261%2fwhy-cant-i-fit-a-curve-to-this-plot%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python