从 R 中的 lm 之前的公式中删除 Inf 值
Remove Inf values from formula before lm in R
假设我使用 mtcars 数据集设置了任意公式:
data(mtcars)
myFormula <- as.formula("mpg ~ cyl + I(disp / hp) + I(wt^2) + I((qsec + vs) / gear)")
我想在 lm
函数中使用该公式,但在此之前,我想删除包含 Inf
、NaN
和 NA
的潜在行.例如,如果 disp / hp
导致任何 Inf values
我想删除包含它的行。我知道我可以通过先生成新变量,删除 Inf
然后使用公式删除 运行 lm 来做到这一点,但我想使用公式术语来做到这一点,因为它是闪亮应用程序的一部分,公式是输入。
我的尝试:
formulaTerms <- terms(myFormula)
formulaTerms <- gsub("I", "", labels(formulaTerms))
formulaTermsRatio <- formulaTerms[grep("/", formulaTerms)]
mtcarsDT <- setDT(mtcars)
mtcarsDT <- mtcarsDT[, formulaTermsRatio[1] := sym(formulaTermsRatio[1])]
使用drop.terms
。假设每个项都由模型矩阵中的单个列表示(即没有 > 2 水平的因子),我们计算模型矩阵 mm
并找到坏列的列号 wx
。然后使用 drop.terms
从 terms 对象中删除这些列并从修改后的 terms 对象中提取公式。
mtcars[1, 3] <- Inf
# is.na is TRUE for NA or NaN; is.infinite is TRUE for Inf or -Inf
is.bad <- function(x) any(is.na(x) | is.infinite(x))
fo_terms <- terms(myFormula) # myFormula is taken from question
mm <- model.matrix(myFormula, mtcars)
wx <- which(apply(mm[, -1], 2, is.bad))
fo_terms2 <- drop.terms(fo_terms, wx, keep.response = TRUE)
fo2 <- formula(fo_terms2)
myFormula
## mpg ~ cyl + I(disp/hp) + I(wt^2) + I((qsec + vs)/gear)
fo2
## mpg ~ cyl + I(wt^2) + I((qsec + vs)/gear)
更新
如果您想从公式中删除坏行而不是项,则:
lm(myFormula, mtcars, subset = !apply(mm, 1, is.bad))
请注意,lm
将自动删除带有 NA 和 NaN 的行(取决于 na.action
参数),因此在这种情况下,您可以简化 is.bad
以仅检查 Inf
和 -Inf
。
另一种方法是用 NA 替换 Inf
和 -Inf
。
mtcars[is.infinite(mtcars)] <- NA
然后正常执行lm
。
您可以从要回归的数据中删除这些值。 Inf 将发生在 hp==0 或 gear==0 的地方。
data(mtcars)
df <- mtcars
myFormula <- as.formula("mpg ~ cyl + I(disp / hp) + I(wt^2) + I((qsec + vs) / gear)")
df <- df[!(df$hp==0 | df$gear==0),]
lm(myFormula,df)
> lm(myFormula,df)
Call:
lm(formula = myFormula, data = df)
Coefficients:
(Intercept) cyl I(disp/hp) I(wt^2) I((qsec + vs)/gear)
35.5847 -1.9639 1.0707 -0.3671 -0.1699
假设我使用 mtcars 数据集设置了任意公式:
data(mtcars)
myFormula <- as.formula("mpg ~ cyl + I(disp / hp) + I(wt^2) + I((qsec + vs) / gear)")
我想在 lm
函数中使用该公式,但在此之前,我想删除包含 Inf
、NaN
和 NA
的潜在行.例如,如果 disp / hp
导致任何 Inf values
我想删除包含它的行。我知道我可以通过先生成新变量,删除 Inf
然后使用公式删除 运行 lm 来做到这一点,但我想使用公式术语来做到这一点,因为它是闪亮应用程序的一部分,公式是输入。
我的尝试:
formulaTerms <- terms(myFormula)
formulaTerms <- gsub("I", "", labels(formulaTerms))
formulaTermsRatio <- formulaTerms[grep("/", formulaTerms)]
mtcarsDT <- setDT(mtcars)
mtcarsDT <- mtcarsDT[, formulaTermsRatio[1] := sym(formulaTermsRatio[1])]
使用drop.terms
。假设每个项都由模型矩阵中的单个列表示(即没有 > 2 水平的因子),我们计算模型矩阵 mm
并找到坏列的列号 wx
。然后使用 drop.terms
从 terms 对象中删除这些列并从修改后的 terms 对象中提取公式。
mtcars[1, 3] <- Inf
# is.na is TRUE for NA or NaN; is.infinite is TRUE for Inf or -Inf
is.bad <- function(x) any(is.na(x) | is.infinite(x))
fo_terms <- terms(myFormula) # myFormula is taken from question
mm <- model.matrix(myFormula, mtcars)
wx <- which(apply(mm[, -1], 2, is.bad))
fo_terms2 <- drop.terms(fo_terms, wx, keep.response = TRUE)
fo2 <- formula(fo_terms2)
myFormula
## mpg ~ cyl + I(disp/hp) + I(wt^2) + I((qsec + vs)/gear)
fo2
## mpg ~ cyl + I(wt^2) + I((qsec + vs)/gear)
更新
如果您想从公式中删除坏行而不是项,则:
lm(myFormula, mtcars, subset = !apply(mm, 1, is.bad))
请注意,lm
将自动删除带有 NA 和 NaN 的行(取决于 na.action
参数),因此在这种情况下,您可以简化 is.bad
以仅检查 Inf
和 -Inf
。
另一种方法是用 NA 替换 Inf
和 -Inf
。
mtcars[is.infinite(mtcars)] <- NA
然后正常执行lm
。
您可以从要回归的数据中删除这些值。 Inf 将发生在 hp==0 或 gear==0 的地方。
data(mtcars)
df <- mtcars
myFormula <- as.formula("mpg ~ cyl + I(disp / hp) + I(wt^2) + I((qsec + vs) / gear)")
df <- df[!(df$hp==0 | df$gear==0),]
lm(myFormula,df)
> lm(myFormula,df)
Call:
lm(formula = myFormula, data = df)
Coefficients:
(Intercept) cyl I(disp/hp) I(wt^2) I((qsec + vs)/gear)
35.5847 -1.9639 1.0707 -0.3671 -0.1699