In my reproducible data below, I wonder how to find the index values for elements from the one-on-one matched vectors rs
& hat
that satisfy the following condition:
rs < -1.96
OR rs > 1.96
elements whose corresponding hat
elements are hat
> max_hat
elements
Is this possible to plot (using text()
) the index values next to its plotted circle in the plot below?
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]
CodePudding user response:
Both conditions define logical vectors so AND them and get which
indices they correspond to. Then plot.
I have added color to make the text labels more obvious.
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")