打印出记录,上面只写着”true”。

huangapple go评论74阅读模式
英文:

print out the record it says only true

问题

Sure, here is the translated code portion:

我正在使用R来编写代码,以确定每个系数对模型有显著影响。它运行正常,但由于我有500条记录,很难在控制台上查找我想要的记录。

我的问题是,是否有一种方法可以打印出在TRUE处的记录编号?

model1 <- lm(price~area+bedrooms+bathrooms+stories, data = house)
DFBETAS <- dfbetas(model1)  # 绝对值的dfbetas与2/sqrt(n)进行比较
DFBETAS > 2/sqrt(n) | DFBETAS < -2/sqrt(n)

Please note that the code has been translated, and I've excluded the parts that are not code from your original text.

英文:

I am using R to write down the code of where each coefficient has significant impact on the model. It works fine, but since I have 500 records, it is hard to look at the console and locate the record I want.

My question is, is there a way to print the record number where it says TRUE?

model1 &lt;- lm(price~area+bedrooms+bathrooms+stories, data = house)
DFBETAS &lt;- dfbetas(model1)  # absolute of dfbetas is compared with 2/sqrt(n)
DFBETAS &gt; 2/sqrt(n) | DFBETAS &lt;  -2/sqrt(n)

打印出记录,上面只写着”true”。

答案1

得分: 2

我们没有您的数据,所以这里有一个使用内置数据集的示例。我创建一个线性模型,然后创建一个数据框,对于超过您的阈值的任何值,将其设为TRUE。然后我添加一列来标记行,最后只输出在任何列(在列1到3之间)中有TRUE的行的子集。

fit <- lm(mpg ~ wt + gear, data = mtcars)
df <- as.data.frame(abs(dfbetas(fit)) > 2/sqrt(nrow(mtcars)))
df$row = 1:nrow(mtcars)
df[rowSums(df[,1:3]) > 0,] # 更好的做法,由 @r2evans 建议
# df[which(apply(df[, 1:3], 1, any)), ]  # 我的最初建议

结果

                  (Intercept)    wt  gear row
Chrysler Imperial        TRUE  TRUE FALSE  17
Fiat 128                FALSE  TRUE FALSE  18
Toyota Corolla           TRUE  TRUE FALSE  20
Toyota Corona            TRUE FALSE FALSE  21
Ford Pantera L           TRUE  TRUE  TRUE  29
Maserati Bora            TRUE  TRUE  TRUE  31
英文:

We don't have your data, so here's an example with a built-in dataset. I make a linear model, then make a data frame with TRUE for any values exceeding your threshold. Then I add a column noting the row, and finally output just the subset of rows where any columns (among columns 1:3) are TRUE.

fit &lt;- lm(mpg ~ wt + gear, data = mtcars)
df &lt;- as.data.frame(abs(dfbetas(fit)) &gt; 2/sqrt(nrow(mtcars)))
df$row = 1:nrow(mtcars)
df[rowSums(df[,1:3]) &gt; 0,] # better, suggested by @r2evans
# df[which(apply(df[, 1:3], 1, any)), ]  # my original suggestion

Result

                  (Intercept)    wt  gear row
Chrysler Imperial        TRUE  TRUE FALSE  17
Fiat 128                FALSE  TRUE FALSE  18
Toyota Corolla           TRUE  TRUE FALSE  20
Toyota Corona            TRUE FALSE FALSE  21
Ford Pantera L           TRUE  TRUE  TRUE  29
Maserati Bora            TRUE  TRUE  TRUE  31

答案2

得分: 0

请使用R中的which()函数。

英文:

Try using the which() function in R.

huangapple
  • 本文由 发表于 2023年5月25日 06:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定