英文:
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 <- lm(price~area+bedrooms+bathrooms+stories, data = house)
DFBETAS <- dfbetas(model1) # absolute of dfbetas is compared with 2/sqrt(n)
DFBETAS > 2/sqrt(n) | DFBETAS < -2/sqrt(n)
答案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 <- 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,] # 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论