Ignore Inf value and run a lm Regression












0















these are my variables:



> dput(y)
c(-22.0713165394207, 14.0880914427811, 10.9650636244176, -1.96648890706268,
-5.30593850426708, -7.54651916037787, 3.84914747321197, 4.4986386904214,
1.73067625014435, 2.5585960595839, -2.72766183793304, -3.10167452216202,
2.68853838208521, 1.12662203717498, 1.24951279248057, 3.70075666289518,
-6.11243972144607, -6.91019769671849, 6.46767794752582, 8.84874735514293,
2.95606352319898, 3.23883851668917, -2.61692776879569)
> dput(x)
c(`1` = 0.0520523266234464, `2` = Inf, `3` = 0.0520523266234462,
`4` = 0.0520523266234463, `5` = 0.0520523266234463, `6` = 0.0520523266234461,
`7` = 0.0520523266234463, `8` = 0.0520523266234466, `9` = 0.0520523266234465,
`10` = 0.0520523266234465, `11` = 0.0520523266234465, `12` = 0.0520523266234466,
`13` = 0.0520523266234468, `14` = 0.0520523266234466, `15` = 0.0520523266234467,
`16` = 0.0520523266234464, `17` = 0.0520523266234463, `18` = 0.0520523266234465,
`19` = 0.0520523266234466, `20` = 0.0520523266234463, `21` = 0.0520523266234464,
`22` = 0.0520523266234465, `23` = 0.0520523266234464)


When I run my regression: summary(lm(ex.return ~ ex.return.skew))



I have this error message:



Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
NA/NaN/Inf in 'x'


I tried to delete the positon 2 and then run my regression but I can not do this because data is much bigger. So I am looking for a way to ignore the Inf/Na values and run my regression.



How can I do this?



Any help?










share|improve this question























  • What do you mean that you cannot delete that observation?

    – Julius Vainora
    Nov 20 '18 at 17:37











  • Because, in this specific case It would easy to delete the second position and run the regression. But my data is much bigger than this. It wouldnt be functional to do this.

    – Laura
    Nov 20 '18 at 17:41
















0















these are my variables:



> dput(y)
c(-22.0713165394207, 14.0880914427811, 10.9650636244176, -1.96648890706268,
-5.30593850426708, -7.54651916037787, 3.84914747321197, 4.4986386904214,
1.73067625014435, 2.5585960595839, -2.72766183793304, -3.10167452216202,
2.68853838208521, 1.12662203717498, 1.24951279248057, 3.70075666289518,
-6.11243972144607, -6.91019769671849, 6.46767794752582, 8.84874735514293,
2.95606352319898, 3.23883851668917, -2.61692776879569)
> dput(x)
c(`1` = 0.0520523266234464, `2` = Inf, `3` = 0.0520523266234462,
`4` = 0.0520523266234463, `5` = 0.0520523266234463, `6` = 0.0520523266234461,
`7` = 0.0520523266234463, `8` = 0.0520523266234466, `9` = 0.0520523266234465,
`10` = 0.0520523266234465, `11` = 0.0520523266234465, `12` = 0.0520523266234466,
`13` = 0.0520523266234468, `14` = 0.0520523266234466, `15` = 0.0520523266234467,
`16` = 0.0520523266234464, `17` = 0.0520523266234463, `18` = 0.0520523266234465,
`19` = 0.0520523266234466, `20` = 0.0520523266234463, `21` = 0.0520523266234464,
`22` = 0.0520523266234465, `23` = 0.0520523266234464)


When I run my regression: summary(lm(ex.return ~ ex.return.skew))



I have this error message:



Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
NA/NaN/Inf in 'x'


I tried to delete the positon 2 and then run my regression but I can not do this because data is much bigger. So I am looking for a way to ignore the Inf/Na values and run my regression.



How can I do this?



Any help?










share|improve this question























  • What do you mean that you cannot delete that observation?

    – Julius Vainora
    Nov 20 '18 at 17:37











  • Because, in this specific case It would easy to delete the second position and run the regression. But my data is much bigger than this. It wouldnt be functional to do this.

    – Laura
    Nov 20 '18 at 17:41














0












0








0








these are my variables:



> dput(y)
c(-22.0713165394207, 14.0880914427811, 10.9650636244176, -1.96648890706268,
-5.30593850426708, -7.54651916037787, 3.84914747321197, 4.4986386904214,
1.73067625014435, 2.5585960595839, -2.72766183793304, -3.10167452216202,
2.68853838208521, 1.12662203717498, 1.24951279248057, 3.70075666289518,
-6.11243972144607, -6.91019769671849, 6.46767794752582, 8.84874735514293,
2.95606352319898, 3.23883851668917, -2.61692776879569)
> dput(x)
c(`1` = 0.0520523266234464, `2` = Inf, `3` = 0.0520523266234462,
`4` = 0.0520523266234463, `5` = 0.0520523266234463, `6` = 0.0520523266234461,
`7` = 0.0520523266234463, `8` = 0.0520523266234466, `9` = 0.0520523266234465,
`10` = 0.0520523266234465, `11` = 0.0520523266234465, `12` = 0.0520523266234466,
`13` = 0.0520523266234468, `14` = 0.0520523266234466, `15` = 0.0520523266234467,
`16` = 0.0520523266234464, `17` = 0.0520523266234463, `18` = 0.0520523266234465,
`19` = 0.0520523266234466, `20` = 0.0520523266234463, `21` = 0.0520523266234464,
`22` = 0.0520523266234465, `23` = 0.0520523266234464)


When I run my regression: summary(lm(ex.return ~ ex.return.skew))



I have this error message:



Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
NA/NaN/Inf in 'x'


I tried to delete the positon 2 and then run my regression but I can not do this because data is much bigger. So I am looking for a way to ignore the Inf/Na values and run my regression.



How can I do this?



Any help?










share|improve this question














these are my variables:



> dput(y)
c(-22.0713165394207, 14.0880914427811, 10.9650636244176, -1.96648890706268,
-5.30593850426708, -7.54651916037787, 3.84914747321197, 4.4986386904214,
1.73067625014435, 2.5585960595839, -2.72766183793304, -3.10167452216202,
2.68853838208521, 1.12662203717498, 1.24951279248057, 3.70075666289518,
-6.11243972144607, -6.91019769671849, 6.46767794752582, 8.84874735514293,
2.95606352319898, 3.23883851668917, -2.61692776879569)
> dput(x)
c(`1` = 0.0520523266234464, `2` = Inf, `3` = 0.0520523266234462,
`4` = 0.0520523266234463, `5` = 0.0520523266234463, `6` = 0.0520523266234461,
`7` = 0.0520523266234463, `8` = 0.0520523266234466, `9` = 0.0520523266234465,
`10` = 0.0520523266234465, `11` = 0.0520523266234465, `12` = 0.0520523266234466,
`13` = 0.0520523266234468, `14` = 0.0520523266234466, `15` = 0.0520523266234467,
`16` = 0.0520523266234464, `17` = 0.0520523266234463, `18` = 0.0520523266234465,
`19` = 0.0520523266234466, `20` = 0.0520523266234463, `21` = 0.0520523266234464,
`22` = 0.0520523266234465, `23` = 0.0520523266234464)


When I run my regression: summary(lm(ex.return ~ ex.return.skew))



I have this error message:



Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
NA/NaN/Inf in 'x'


I tried to delete the positon 2 and then run my regression but I can not do this because data is much bigger. So I am looking for a way to ignore the Inf/Na values and run my regression.



How can I do this?



Any help?







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 17:33









LauraLaura

35319




35319













  • What do you mean that you cannot delete that observation?

    – Julius Vainora
    Nov 20 '18 at 17:37











  • Because, in this specific case It would easy to delete the second position and run the regression. But my data is much bigger than this. It wouldnt be functional to do this.

    – Laura
    Nov 20 '18 at 17:41



















  • What do you mean that you cannot delete that observation?

    – Julius Vainora
    Nov 20 '18 at 17:37











  • Because, in this specific case It would easy to delete the second position and run the regression. But my data is much bigger than this. It wouldnt be functional to do this.

    – Laura
    Nov 20 '18 at 17:41

















What do you mean that you cannot delete that observation?

– Julius Vainora
Nov 20 '18 at 17:37





What do you mean that you cannot delete that observation?

– Julius Vainora
Nov 20 '18 at 17:37













Because, in this specific case It would easy to delete the second position and run the regression. But my data is much bigger than this. It wouldnt be functional to do this.

– Laura
Nov 20 '18 at 17:41





Because, in this specific case It would easy to delete the second position and run the regression. But my data is much bigger than this. It wouldnt be functional to do this.

– Laura
Nov 20 '18 at 17:41












2 Answers
2






active

oldest

votes


















1














We can convert the infinite values to NA and it should work



x[is.infinite(x)] <- NA
summary(lm(y ~ x))





share|improve this answer
























  • Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

    – Laura
    Nov 20 '18 at 17:45













  • @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

    – akrun
    Nov 20 '18 at 17:58













  • @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

    – rookie
    Nov 21 '18 at 7:52



















0














You need to remove the infinite items in both x and y as follows:



summary(lm(ex.return[is.finite(df$ex.return)] ~ ex.return.skew[is.finite(df$ex.return)]))


However, what is better is to put them into a data.frame and add that data.frame in the data argument of lm which filters out the rows of the data.frame.



df <- data.frame(ex.return, ex.return.skew)
summary(lm(ex.return ~ ex.return.skew, df[is.finite(df$ex.return),]))


Note that is.finite() works for NA values as well as -Inf/Inf



is.finite(c(NA, Inf, 10))
[1] FALSE FALSE TRUE


If there is the possibility of Inf/-Inf and NA in any row of any column in your data.frame (i.e. not just in ex.return) then you might need to do something like:



summary(lm(ex.return ~ ex.return.skew, df[is.finite(rowSums(df)),]))





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%2f53398479%2fignore-inf-value-and-run-a-lm-regression%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














    We can convert the infinite values to NA and it should work



    x[is.infinite(x)] <- NA
    summary(lm(y ~ x))





    share|improve this answer
























    • Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

      – Laura
      Nov 20 '18 at 17:45













    • @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

      – akrun
      Nov 20 '18 at 17:58













    • @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

      – rookie
      Nov 21 '18 at 7:52
















    1














    We can convert the infinite values to NA and it should work



    x[is.infinite(x)] <- NA
    summary(lm(y ~ x))





    share|improve this answer
























    • Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

      – Laura
      Nov 20 '18 at 17:45













    • @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

      – akrun
      Nov 20 '18 at 17:58













    • @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

      – rookie
      Nov 21 '18 at 7:52














    1












    1








    1







    We can convert the infinite values to NA and it should work



    x[is.infinite(x)] <- NA
    summary(lm(y ~ x))





    share|improve this answer













    We can convert the infinite values to NA and it should work



    x[is.infinite(x)] <- NA
    summary(lm(y ~ x))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 17:37









    akrunakrun

    403k13196269




    403k13196269













    • Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

      – Laura
      Nov 20 '18 at 17:45













    • @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

      – akrun
      Nov 20 '18 at 17:58













    • @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

      – rookie
      Nov 21 '18 at 7:52



















    • Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

      – Laura
      Nov 20 '18 at 17:45













    • @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

      – akrun
      Nov 20 '18 at 17:58













    • @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

      – rookie
      Nov 21 '18 at 7:52

















    Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

    – Laura
    Nov 20 '18 at 17:45







    Thanks @akrun but I have NA value for the angular coefficient of my regression. I though it would work, but it didnt. I tried `na.action=na.omit´ but didnt work.

    – Laura
    Nov 20 '18 at 17:45















    @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

    – akrun
    Nov 20 '18 at 17:58







    @Laura Would it make sense to create a logical index and then ignore those values. i.e. i1 <- is.infinite(x); i2 <- is.infinite(y); i3 <- i1|i2; x1 <- x[!i3]; y1 <- y[!i3]; summary(lm(y1 ~ x1))

    – akrun
    Nov 20 '18 at 17:58















    @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

    – rookie
    Nov 21 '18 at 7:52





    @akrun this answer doesn’t work because you can’t have NA values in a regression as you get NA coefficients as mentioned. See my solution for the correct answer. The lm function always works best with data.frames rather than separate vectors - see my answer.

    – rookie
    Nov 21 '18 at 7:52













    0














    You need to remove the infinite items in both x and y as follows:



    summary(lm(ex.return[is.finite(df$ex.return)] ~ ex.return.skew[is.finite(df$ex.return)]))


    However, what is better is to put them into a data.frame and add that data.frame in the data argument of lm which filters out the rows of the data.frame.



    df <- data.frame(ex.return, ex.return.skew)
    summary(lm(ex.return ~ ex.return.skew, df[is.finite(df$ex.return),]))


    Note that is.finite() works for NA values as well as -Inf/Inf



    is.finite(c(NA, Inf, 10))
    [1] FALSE FALSE TRUE


    If there is the possibility of Inf/-Inf and NA in any row of any column in your data.frame (i.e. not just in ex.return) then you might need to do something like:



    summary(lm(ex.return ~ ex.return.skew, df[is.finite(rowSums(df)),]))





    share|improve this answer






























      0














      You need to remove the infinite items in both x and y as follows:



      summary(lm(ex.return[is.finite(df$ex.return)] ~ ex.return.skew[is.finite(df$ex.return)]))


      However, what is better is to put them into a data.frame and add that data.frame in the data argument of lm which filters out the rows of the data.frame.



      df <- data.frame(ex.return, ex.return.skew)
      summary(lm(ex.return ~ ex.return.skew, df[is.finite(df$ex.return),]))


      Note that is.finite() works for NA values as well as -Inf/Inf



      is.finite(c(NA, Inf, 10))
      [1] FALSE FALSE TRUE


      If there is the possibility of Inf/-Inf and NA in any row of any column in your data.frame (i.e. not just in ex.return) then you might need to do something like:



      summary(lm(ex.return ~ ex.return.skew, df[is.finite(rowSums(df)),]))





      share|improve this answer




























        0












        0








        0







        You need to remove the infinite items in both x and y as follows:



        summary(lm(ex.return[is.finite(df$ex.return)] ~ ex.return.skew[is.finite(df$ex.return)]))


        However, what is better is to put them into a data.frame and add that data.frame in the data argument of lm which filters out the rows of the data.frame.



        df <- data.frame(ex.return, ex.return.skew)
        summary(lm(ex.return ~ ex.return.skew, df[is.finite(df$ex.return),]))


        Note that is.finite() works for NA values as well as -Inf/Inf



        is.finite(c(NA, Inf, 10))
        [1] FALSE FALSE TRUE


        If there is the possibility of Inf/-Inf and NA in any row of any column in your data.frame (i.e. not just in ex.return) then you might need to do something like:



        summary(lm(ex.return ~ ex.return.skew, df[is.finite(rowSums(df)),]))





        share|improve this answer















        You need to remove the infinite items in both x and y as follows:



        summary(lm(ex.return[is.finite(df$ex.return)] ~ ex.return.skew[is.finite(df$ex.return)]))


        However, what is better is to put them into a data.frame and add that data.frame in the data argument of lm which filters out the rows of the data.frame.



        df <- data.frame(ex.return, ex.return.skew)
        summary(lm(ex.return ~ ex.return.skew, df[is.finite(df$ex.return),]))


        Note that is.finite() works for NA values as well as -Inf/Inf



        is.finite(c(NA, Inf, 10))
        [1] FALSE FALSE TRUE


        If there is the possibility of Inf/-Inf and NA in any row of any column in your data.frame (i.e. not just in ex.return) then you might need to do something like:



        summary(lm(ex.return ~ ex.return.skew, df[is.finite(rowSums(df)),]))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 13:59

























        answered Nov 20 '18 at 18:01









        rookierookie

        863




        863






























            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%2f53398479%2fignore-inf-value-and-run-a-lm-regression%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

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$