我有一个大约有160个数据点的矩阵。我有兴趣发现该系列的任何部分(不必是整个系列)都有趋势(线性和增加/减少)。我只对找出线性趋势而非非线性趋势感兴趣。
我首先在表格的基本数据集上进行测试:
A<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,17,18,17,17,18,17,17,18,19,19,19,19,19,19,19,19,19,19,19)
这里只是绘制数据,我认为趋势是从1:17向上,我想为此引入假人(一旦我知道趋势起点和终点,我就能做到)。有什么方法可以知道吗? IN R?
答案 0 :(得分:1)
A<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,17,18,17,17,18,17,17,18,19,19,19,19,19,19,19,19,19,19,19)
x <- seq_along(A)
plot(A ~ x)
fit0 <- lm(A ~ x)
library(segmented)
fit1 <- segmented(fit0, seg.Z = ~ x, psi = list(x = c(10, 40)))
summary(fit1)
#Estimated Break-Point(s):
# Est. St.Err
#psi1.x 18.00 0.220
#psi2.x 34.04 0.246
lines(x, predict(fit1), col = "red")
答案 1 :(得分:0)
检查lm
功能。此功能可帮助您创建线性回归。
A<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,17,18,17,17,18,17,17,18,19,19,19,19,19,19,19,19,19,19,19)
x = c(1:length(A))
plot(x,A)
reg = lm(A~x)
abline(reg,col="red")
如果您想在 startPoint 开始线性回归,请使用“A [ startPoint :length(A)]
了解趋势:print(reg)
答案 2 :(得分:0)
分解功能可以提供帮助。假人可能不够灵敏。您可能只想减去趋势分量。尝试:
A<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,17,18,17,17,18,17,17,18,19,19,19,19,19,19,19,19,19,19,19)
# convert to ts first
my.ts <- ts(A, start = c(2000, 1), freq = 12) # .. if you have months
my.trend <- decompose(my.ts)$trend
print(max(diff(my.trend), na.rm = TRUE))
# use a certain percentage of max difference to find start and end of trend
tolerance <- 0.1 # say 10 % ..
trend.start <- which.max(my.trend >= tolerance * max(my.trend, na.rm=T))
trend.end <- which.max(my.trend >= (1-tolerance) * max(my.trend, na.rm=T))
# plot shows that it - almost - fits
plot(my.ts, col = "blue", type = "o")
abline(v = time(my.ts)[trend.start], col = "gray")
abline(v = time(my.ts)[trend.end], col = "gray")
lines(my.trend)
# but what do you do, if you have a ts like this ..
B <- c(rep(0,50), seq(1,30,0.5), rep(30,50), seq(30,1,-1)) + rnorm (189,0,1)
my.ts <- ts(B, start = c(2000, 01), freq = 12)
plot(my.ts)
my.trend <- decompose(my.ts)$trend
plot(my.trend)