【问题标题】:Finding the index values for a combination of vectors查找向量组合的索引值
【发布时间】:2021-11-25 05:41:28
【问题描述】:

在下面的可重现数据中,我想知道如何从满足以下条件的一对一匹配向量rs 和hat 中找到元素的索引值: p>

rs < -1.96 OR rs > 1.96 元素对应的hat 元素是hat > max_hat 元素

这是否可以在下图中绘制(使用text())其绘制圆圈旁边的索引值?

x <- readRDS(url("https://github.com/rnorouzian/s/raw/main/cooks.rds"))
hat <- readRDS(url("https://github.com/rnorouzian/s/raw/main/hat.rds"))
rs <- readRDS(url("https://github.com/rnorouzian/s/raw/main/rstudent.rds"))$z


plot(hat, rs, cex = 0.2+3*sqrt(x), pch = 19)

max_hat <- boxplot.stats(hat, coef = 2)$stats[5]

【问题讨论】:

  • @akrun,虽然答案并不完整,但它们不适用于 res2?

标签: r dataframe plot dplyr subset


【解决方案1】:

这两个条件都定义了逻辑向量,因此对它们进行 AND 并获得它们对应的 which 索引。然后绘制。
我添加了颜色以使文本标签更明显。

max_hat <- boxplot.stats(hat, coef = 2)$stats[5]

i <- abs(rs) > 1.96  # abs() because +/-1.96 are 
                     # symmetric values
j <- hat > max_hat
k <- which(i & j)

plot(hat, rs, cex = 0.2+3*sqrt(x), pch = 19)
text(hat[k], rs[k], labels = names(k), pos = 2, col = "red")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多