在a * sin(b * x + c)形式的R中拟合谐波模型

时间:2015-12-23 21:43:28

标签: r

在我的时间序列建模课中,我们被教导通过在不同频率创建几个SINE和COSINE术语来模拟时间序列中的谐波模式,然后保留那些重要的。

但是,现在我想找到a*sin(b*x+c)形式的最佳拟合模型,并且不知道从哪里开始。

1 个答案:

答案 0 :(得分:3)

虽然我支持boshek的评论,但我只是玩得很​​开心:

# As you dont provide any data, here some simulated data:
set.seed(1)
x <- seq(0, 10, length = 100)
y <- sin(2*x + 5) + rnorm(100, sd=0.5)
y_clean <- sin(2*x + 5)

# Plot of the noised data (y) and the noiseless line (y_clean) which you want to approximate through your model:
plot(x, y, ylim = c(-2,3))
lines(x,y_clean, col = "green")

# Model estimation and adding of the fitted values to the previous plot:
model <- nls(y~a*sin(b*x+c), start = list(a=1,b=1.5, c=1),control = list(maxiter = 500))
lines(x, fitted(model), col = "red")

legend("topright", col=c("green", "red"), legend = c("\"true\" curve", "fitted line"), bty = "n", lty = 1)

enter image description here