0

我在 R 中有以下数据和代码:

x <- runif(1000, -9.99, 9.99)
mx <- mean(x)
stdevs_3 <- mx + c(-3, +3) * sd(x/5) # Statndard Deviation 3-sigma

我在 R 中绘制为线(以及 3 个标准差和平均线):

plot(x, t="l", main="Plot of Data", ylab="X", xlab="")
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")

带有平均线和 SD 线的线图

我想做的事:

在图上的任何地方,只要线超过 3 个 sigma 阈值(蓝线),在其上方或下方,线的颜色应与黑色不同。

我试过这个,但没有奏效:

plot(x, type="l", col= ifelse(x < stdevs_3[[1]],"red", "black"))
abline(h=mx, col="red", lwd=2)
abline(h=stdevs_3, lwd=2, col="blue")

还有其他方法吗?

4

1 回答 1

0

这是所要求的,但由于 x 除以 5,这对我来说似乎毫无意义:

png( )
plot(NA, xlim=c(0,length(x)), ylim=range(x),  main="Plot of Data", ylab="X", xlab="", )
 stdevs_3 <- mx + c(-3, +3) * sd(x/5) 
 abline(h=mx, col="red", lwd=2)
 abline(h=stdevs_3, lwd=2, col="blue")
 segments( 0:999, head(x,-1), 1:1000, tail(x,-1) , col=c("black", "red")[ 
                                       1+(abs(tail(x,-1)) > mx+3*sd(x/5))] )
 dev.off()

在此处输入图像描述

于 2016-03-02T06:27:46.467 回答