正弦曲线拟合麻烦R
Sine curve fitting trouble R
我正在尝试按照此示例将正弦曲线拟合到线性数据,
Sine curve fit using lm and nls in R,但我得到了一个看起来很有趣的图表,就像我的代码下面的图表一样。我想要的只是一条适合我的数据的正弦曲线。
Data <- mrns[[1]]
Time <- Data$time
HR <- Data$raw.HR
xc <- cos(2*pi*Time/366)
xs <- sin(2*pi*Time/366)
fit.lm <- lm(HR ~ xc+xs)
pred <- predict(fit.lm, newdata=data.frame(Time=Time))
plot(HR ~ Time, data=Data, xlim=c(0, 2359))
lines(Time, pred, col="blue")
我得到的是下面的图,它与我的数据不太吻合。
这是我的数据输入:
> dput(mrns[[1]])
structure(list(raw.HR = c(47L, 48L, 47L,
47L, 49L, 46L, 47L, 51L, 50L, 52L, 49L, NA, 54L, NA, NA,
NA, 76L, NA, 74L, NA, NA, 73L, 74L, 78L, 66L, 64L, 60L, NA,
64L, NA, 62L, 62L, 65L, 61L, 60L, NA, 69L, 68L, 69L, NA,
NA, 68L, 66L, NA, 77L, 70L, 73L, 72L, NA, 76L, NA, 74L, NA,
75L, 72L, NA, 67L, 69L, 55L, 67L, NA, 69L, 64L, 71L, NA,
64L, 59L, 55L, 54L, 50L, 74L, NA, 65L, 61L, 59L, NA, 68L,
69L, NA, 65L, NA, 133L, 67L, 59L, 56L, 54L, 51L, 52L, 54L,
53L, 52L, 57L, 46L), time = c(14, 44, 114, 144, 214,
244, 314, 344, 414, 444, 514, 544, 546, 614, 617, 629, 631,
644, 647, 659, 702, 714, 729, 744, 759, 814, 829, 844, 847,
859, 902, 914, 929, 944, 959, 1014, 1017, 1029, 1044, 1059,
1102, 1114, 1129, 1144, 1147, 1159, 1214, 1229, 1244, 1248,
1259, 1301, 1314, 1317, 1329, 1344, 1347, 1359, 1544, 1546,
1559, 1602, 1614, 1629, 1644, 1646, 1659, 1714, 1729, 1744,
1759, 1814, 1817, 1829, 1844, 1859, 1902, 1914, 1929, 1931,
1944, 1947, 1959, 2014, 2029, 2044, 2059, 2114, 2144, 2214,
2244, 2314, 2344)), .Names = c("raw.HR", "time"
), row.names = c(42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L,
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L,
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L,
77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L,
90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L), class = "data.frame")
目前我不确定还可以尝试什么。有人有什么建议吗?
开玩笑的。我将周期更改为 2359,这是最大时间间隔,曲线非常适合我的所有绘图。感谢@Dason 提供的信息!
Data <- mrns[[3]]
Time <- Data$time
HR <- Data$raw.HR
xc <- cos(2*pi*Time/2359)
xs <- sin(2*pi*Time/2359)
fit.lm <- lm(HR ~ xc+xs)
pred <- predict(fit.lm, newdata=data.frame(Time=Time))
plot(HR ~ Time, data=Data, xlim=c(0, 2359))
lines(Time, pred, col="blue")
我正在尝试按照此示例将正弦曲线拟合到线性数据, Sine curve fit using lm and nls in R,但我得到了一个看起来很有趣的图表,就像我的代码下面的图表一样。我想要的只是一条适合我的数据的正弦曲线。
Data <- mrns[[1]]
Time <- Data$time
HR <- Data$raw.HR
xc <- cos(2*pi*Time/366)
xs <- sin(2*pi*Time/366)
fit.lm <- lm(HR ~ xc+xs)
pred <- predict(fit.lm, newdata=data.frame(Time=Time))
plot(HR ~ Time, data=Data, xlim=c(0, 2359))
lines(Time, pred, col="blue")
我得到的是下面的图,它与我的数据不太吻合。
> dput(mrns[[1]])
structure(list(raw.HR = c(47L, 48L, 47L,
47L, 49L, 46L, 47L, 51L, 50L, 52L, 49L, NA, 54L, NA, NA,
NA, 76L, NA, 74L, NA, NA, 73L, 74L, 78L, 66L, 64L, 60L, NA,
64L, NA, 62L, 62L, 65L, 61L, 60L, NA, 69L, 68L, 69L, NA,
NA, 68L, 66L, NA, 77L, 70L, 73L, 72L, NA, 76L, NA, 74L, NA,
75L, 72L, NA, 67L, 69L, 55L, 67L, NA, 69L, 64L, 71L, NA,
64L, 59L, 55L, 54L, 50L, 74L, NA, 65L, 61L, 59L, NA, 68L,
69L, NA, 65L, NA, 133L, 67L, 59L, 56L, 54L, 51L, 52L, 54L,
53L, 52L, 57L, 46L), time = c(14, 44, 114, 144, 214,
244, 314, 344, 414, 444, 514, 544, 546, 614, 617, 629, 631,
644, 647, 659, 702, 714, 729, 744, 759, 814, 829, 844, 847,
859, 902, 914, 929, 944, 959, 1014, 1017, 1029, 1044, 1059,
1102, 1114, 1129, 1144, 1147, 1159, 1214, 1229, 1244, 1248,
1259, 1301, 1314, 1317, 1329, 1344, 1347, 1359, 1544, 1546,
1559, 1602, 1614, 1629, 1644, 1646, 1659, 1714, 1729, 1744,
1759, 1814, 1817, 1829, 1844, 1859, 1902, 1914, 1929, 1931,
1944, 1947, 1959, 2014, 2029, 2044, 2059, 2114, 2144, 2214,
2244, 2314, 2344)), .Names = c("raw.HR", "time"
), row.names = c(42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L,
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L,
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L,
77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L,
90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L), class = "data.frame")
目前我不确定还可以尝试什么。有人有什么建议吗?
开玩笑的。我将周期更改为 2359,这是最大时间间隔,曲线非常适合我的所有绘图。感谢@Dason 提供的信息!
Data <- mrns[[3]]
Time <- Data$time
HR <- Data$raw.HR
xc <- cos(2*pi*Time/2359)
xs <- sin(2*pi*Time/2359)
fit.lm <- lm(HR ~ xc+xs)
pred <- predict(fit.lm, newdata=data.frame(Time=Time))
plot(HR ~ Time, data=Data, xlim=c(0, 2359))
lines(Time, pred, col="blue")