在R中测试我的测试数据与正弦模板的拟合程度

aelbi1ox  于 10个月前  发布在  其他
关注(0)|答案(1)|浏览(106)

在我的数据集中,我将数据分为测试数据和训练数据。我使用训练数据拟合正弦曲线。

freq = seq(from=21, to=80, by=0.5)
amp = c(-45.22247,-44.87434,-44.30659, -44.104, -44.08027 ,-44.32827, -44.52562, -44.81116, -45.11858, -45.61112, -45.88737, -45.9517, -46.00517,-45.78666, -45.64515, -45.32656, -45.12009, -44.92572, -44.58918,-44.18919, -43.75679, -43.4801, -43.13948, -42.86826 ,-42.59199,-42.23767, -42.06125, -41.99265 ,-41.91103, -41.96361, -42.10183,-42.47087, -42.86456, -43.17823 ,-43.41674, -43.6427 ,-43.79429,-43.71881, -43.58482 ,-43.53886, -43.29379, -43.00895 ,-42.69728,-42.331 ,-42.11675, -41.84402, -41.63068 ,-41.28213, -40.88509,-40.64461 ,-40.47236 ,-40.49424, -40.73463, -41.02037, -41.49101, -41.95741 ,-42.36682, -42.83582 ,-43.08535, -43.16926, -43.20281,-43.08085, -43.08461, -42.96666, -42.70192, -42.57608 ,-42.30105,-42.01737, -41.58435 ,-41.24757, -40.96 ,-40.68981 ,-40.39803,-40.2478, -40.23024, -40.48872, -40.68104, -41.04532, -41.53119,-41.88053, -42.17658, -42.32618, -42.41549 ,-42.36513, -42.38881,-42.25508, -42.13853, -41.89402, -41.66664, -41.3869, -41.02896, -40.83986, -40.44932 ,-40.14661, -39.84292, -39.66413, -39.60376, -39.63048, -39.75595 ,-39.88888, -40.18054 ,-40.491, -40.62274, -40.8282, -40.92761, -41.1448, -41.0102 ,-41.06486, -40.96643,-40.93895 ,-40.72338, -40.58406, -40.44275 ,-40.28842, -40.23578, -40.04179, -39.93855, -39.94855, -39.90564)
ssp <- data.frame(freq, amp)

A<- (max(ssp$amp)-min(ssp$amp)/2)
C<-((max(ssp$amp)+min(ssp$amp))/2)

res1<- nlsLM(amp ~ A*sin(omega*freq+phi)+C, data=ssp, start=list(A=A,omega=pi/6,phi=1,C=C))
sumres=summary(res1)
co <- coef(res1)
#resid(res1)
#sum(resid(res1)^2)
fit <- function(x, a, b, c, d) {a*sin(b*x+c)+d}
# Plot result
plot(ssp)
curve(fit(x, a=co["A"], b=co["omega"], c=co["phi"], d=co["C"]), add=TRUE ,lwd=2, col="steelblue")

字符串
这为我提供了这个适合。请注意,我知道适合可以更好,但这不是必要的目的,这是不够接近。
x1c 0d1x的数据

sumres

Formula: amp ~ A * sin(omega * freq + phi) + C

Parameters:
       Estimate Std. Error  t value Pr(>|t|)    
A      -1.00030    0.20105   -4.975  2.3e-06 ***
omega   0.53007    0.01196   44.332  < 2e-16 ***
phi    -0.31121    0.64156   -0.485    0.629    
C     -42.19834    0.14390 -293.248  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.552 on 115 degrees of freedom

Number of iterations to convergence: 7 
Achieved convergence tolerance: 1.49e-08


现在,我有测试数据,我想比较这个模型化的正弦波。理论上,我应该能够再次使用nls(非线性最小二乘)函数来获得残差标准误差值,以了解这个模板与其他数据的拟合程度。而不是“开始”任何变量,我只是告诉函数它们是什么。然而,当我这样做时,我收到以下错误:

newssp = similar to data above
res2<- nls(amp ~ co["A"]*sin(co["omega"]*freq+co["phi"])+co["C"], data=newssp)
Error in getInitial.default(func, data, mCall = as.list(match.call(func,  : 
  no 'getInitial' method found for "function" objects


我的理解是,这是因为提供的值可能不是最佳拟合,但这正是我正在寻找的。我想看看我的建模正弦波与原始数据的拟合程度如何。我唯一能想到的是,对于我的系数,并非所有系数都很重要。
有没有更好的方法来测试我的模拟正弦波与原始数据的比较?谢谢!

46scxncf

46scxncf1#

不如将之前拟合的值作为起始值,并告诉nls(通过nls.control())不要进行拟合算法的任何迭代?

res2 <- nls(amp ~ A*sin(omega*freq+phi)+C, data=newdata, 
    start=as.list(co), 
    control = nls.control(maxiter=0, warnOnly = TRUE))

字符串

相关问题