【发布时间】:2021-09-01 16:32:38
【问题描述】:
我正在尝试使用以下代码将正弦曲线拟合到数据中。我不知道为什么它变得时髦,线条似乎从 17 到 9 的观察然后从 9 到 20 再回到 12。我还希望正弦曲线延伸到整个 24 小时。如果你能在 ggplot2 中做到这一点,那就太棒了。谢谢!
Test_case <- structure(list(TEST_RESULT = c(1.31, 65.44, 8.59, 9.91), Time = c(17,
9, 20, 12)), row.names = c(NA, -4L), class = c("tbl_df", "tbl",
"data.frame"))
Time <- test_case$Time
test_result <- test_case$TEST_RESULT
xc<-cos(2*pi*Time/24)
xs<-sin(2*pi*Time/24)
fit.lm <- lm(test_result ~ xc+xs)
fit <- fitted(fit.lm)
# find predictions for original time series
pred <- predict(fit.lm, newdata=data.frame(Time=Time))
plot(test_result ~ Time, data= test_case, xlim=c(1, 24), ylim=c(0,500))
lines(Time, pred, col="blue")
【问题讨论】: