Ignore Inf value and run a lm Regression

前端 未结 2 751
广开言路
广开言路 2021-01-28 08:26

these are my variables:

> dput(y)
c(-22.0713165394207, 14.0880914427811, 10.9650636244176, -1.96648890706268, 
-5.30593850426708, -7.54651916037787, 3.8491474         


        
相关标签:
2条回答
  • 2021-01-28 08:39

    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)),]))
    
    0 讨论(0)
  • We can convert the infinite values to NA and it should work

    x[is.infinite(x)] <- NA
    summary(lm(y ~ x))
    
    0 讨论(0)
提交回复
热议问题