我必须根据
kmeans
函数给出的聚类对价格图进行不同的着色。考虑这段代码:
library(tseries)
####
nomiequity <- "^IXIC"
datastart <- "2019-09-18"
nsdq.prices <- get.hist.quote(instrument = nomiequity,
compression = "d",
start = datastart,
end = "2020-12-31",
retclass = "zoo",
quote = "AdjClose")
b<-kmeans(nsdq.prices,3)
c<-b$cluster
d<- merge(nsdq.prices, c)
e<-split(nsdq.prices, c)
plot(nsdq.prices, type="l", col="green", ylim=c(6000, 13000))
lines(e[["2"]], type = "l", col="red")
lines(e[["3"]], type = "l", col="blue")
问题在于簇会合并到
line
中。当簇在时间序列中发生变化时,您可以使用 rle
长度将数字加一。为此,请使用 Map
来 rep
吃连续数字 l
次。然后,您可以 split
处理这些不断增长的数字,但使用 cl
uster 定义 lines
的颜色。对于后者,请使用 lapply
循环分割的 e
。
cl <- kmeans(nsdq.prices, 3)$cluster
l <- rle(as.numeric(cl))$lengths
s <- Map(rep, seq(l), l)
e <- split(cbind(nsdq.prices, cl), unlist(s))
plot(nsdq.prices, type="l", col=7, ylim=c(6000, 13000))
invisible(lapply(e, function(x) lines(x$Adjusted, col=x$cl + 1)))
legend("topleft", leg=c(sprintf("cl %s", 1:3), "missing"), col=c((1:3)+1, 7), lty=1)
如果没有定义日期,就会出现空白。我们可以通过使用“缺失”的颜色覆盖原始图来使用
zoo
插值。
我希望这段代码对你有帮助:
library(tseries)
####
nomiequity <- "^IXIC"
datastart <- "2019-09-18"
nsdq.prices <- get.hist.quote(instrument = nomiequity,
compression = "d",
start = datastart,
end = "2020-12-31",
retclass = "zoo",
quote = "AdjClose")
b<-kmeans(nsdq.prices,3)
c<-b$cluster
d<- merge(nsdq.prices, c)
e<-split(nsdq.prices, c)
e3= e[['3']]
empty <- zoo(order.by=seq.Date(head(index(e3),1),tail(index(e3),1),by="days"))
e3=merge(e3,empty)
e2= e[['2']]
empty <- zoo(order.by=seq.Date(head(index(e2),1),tail(index(e2),1),by="days"))
e2=merge(e2,empty)
plot(nsdq.prices, type="l", col="green", ylim=c(6000, 13000))
lines(e2, type = "l", col="red")
lines(e3, type = "l", col="blue")
我刚刚使用了这个:R:填充时间序列中缺失的日期?
通过这种方法,我找到了集群值发生变化的行,然后选择这些日期并添加 NA 值进行调整。当 NA 值出现时,geom_line 会中断,从而给出所需的结果。
library(ggplot2)
library(dplyr)
library(data.table)
#add date as a column
datum <- index(d)
d <- as.data.table(d)
d$Datum <- datum
#add row number as a column
d$Row <- 1:nrow(d)
#find rows of d where cluster value changes
rows <- which(d$c != dplyr::lag(d$c))
rows <- d[Row %in% rows]
#add NA values for Adjusted at the Dates where values change(which breaks the geom_line)
rows <- rows[, Adjusted := NA]
#merge rows (NA values of Adjusted) with d
d <- rbind(rows, d)
#create a plot
ggplot(d, aes(x = Datum, y = Adjusted, col = as.factor(c))) + geom_line() + scale_color_manual(values = c("green", "red", "blue"))