对 R 中的数据进行正弦拟合

问题描述 投票:0回答:1

我有 45 年的每日温度数据,是从 csv 文件中解析出来的。我已经能够将数据转换为年而不是天,并绘制了年最高气温、年最低气温和年平均气温,如下所示:

data <- read.csv(path)

L <- length(data[,1]) # Num of days
N <- length(data[,1][seq(1, L, by = 365)]) # Num of years
t <- seq(0, N-1, 1) # Years elapsed
TMAX<-as.numeric(data[seq(1,L,by=365),2]) # Yearly max temp
TMIN<-as.numeric(data[seq(1,L,by=365),3]) # Yearly min temp
TMEAN<-as.numeric(data[seq(1,L,by=365),4]) # Yearly mean temp

# Temperature plots indexed by year
par(mfrow=c(3,1), mar=c(0,0,2,0), oma=c(5,0,5,0), pin=c(4, 1), bg=NA)

plot(x = t, TMAX, col=c('red'),type='o', pch=16, xaxt="n", xlab="", main = "Temperature in Mesa, AZ (C°)", cex=1, ylim = c(min(TMIN)-5, max(TMAX)+5))
plot(x = t, TMEAN, col=c('purple'),type='o', pch=16, xaxt="n", xlab="", cex=1, ylim = c(min(TMIN)-5, max(TMAX)+5))
plot(x = t, TMIN, col=c('blue'),type='o', pch=16, cex=1, ylim = c(min(TMIN)-5, max(TMAX)+5))

mtext(side=1, line=3, "Year", font=2,cex=1)

我的目标是使用函数 y = A*sin(omega * t + phi) + C 获得平均温度数据的最佳拟合,其中 A 是年振幅,omega 是频率,phi 是相移, c 是垂直位移。

我不成功的尝试是:

for (i in 0:45){
  A <- (max(TMAX[i]) - min(TMIN[i]))/2
  C <- (max(TMAX[i]) + min(TMIN[i]))/2
}
t <- seq(0, N-1, 1)
omega <- 2*pi
phi <- 1/4
res1 <- nls(y ~ A*sin(omega*t + phi)+C, data = data, start=list(A=A,omega=omega,phi=0,C=C), control = list(maxiter = 5000))

如有任何帮助,我们将不胜感激

编辑:运行

dput(data[1:10, ])
,我得到以下信息:

structure(list(daily.time = c("1/1/1980", "1/2/1980", "1/3/1980", 
"1/4/1980", "1/5/1980", "1/6/1980", "1/7/1980", "1/8/1980", "1/9/1980", 
"1/10/1980"), max.temperature = c(16.9, 17.2, 16.9, 17.5, 18.3, 
16.3, 17.4, 17.7, 13.8, 16.2), minimum.temperature = c(4.5, 1.7, 
0.9, 1.5, 0.1, 3.9, 3.3, 7.6, 6.5, 10.6), mean.temperature = c(10, 
9.2, 8.5, 9.2, 9.3, 10.1, 10.7, 12, 9.7, 13), max_diff = c(0, 
0.3, -0.3, 0.6, 0.8, -2, 1.1, 0.3, -3.9, 2.4), min_diff = c(0, 
-2.8, -0.8, 0.6, -1.4, 3.8, -0.6, 4.3, -1.1, 4.1), mean_diff = c(0, 
-0.8, -0.7, 0.7, 0.1, 0.8, 0.6, 1.3, -2.3, 3.3)), row.names = c(NA, 
10L), class = "data.frame")```
and the error messages I get are:

Warning messages:
1: In max(TMAX[i]) : no non-missing arguments to max; returning -Inf
2: In min(TMIN[i]) : no non-missing arguments to min; returning Inf
3: In max(TMAX[i]) : no non-missing arguments to max; returning -Inf
4: In min(TMIN[i]) : no non-missing arguments to min; returning Inf
Error in nlsModel(formula, mf, start, wts, scaleOffset = scOff, nDcentral = nDcntr) : 
  singular gradient matrix at initial parameter estimates
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
r curve-fitting data-fitting
1个回答
0
投票

使用

plinear
算法。 它不需要线性输入的参数(A、C)的起始值,并且需要一个矩阵 RHS,其列乘以相应的线性参数。 我们还提供了更好的起始值并定义了问题代码中未设置的 y 。 如果使用不同或扩展的数据,并且数据显着不同,则可能需要其他起始值。

t <- seq(0, length = nrow(data))
y <- data$mean.temperature

fo <- y ~ cbind(A = sin(omega*t + phi), C = 1)
st <- list(omega = 0.5, phi = 0.5)
nls(fo, start = st, alg = "plinear")

给予

Nonlinear regression model
  model: y ~ cbind(A = sin(omega * t + phi), C = 1)
   data: parent.frame()
  omega     phi  .lin.A  .lin.C 
 0.3744  0.7023 -1.7187 10.7743 
 residual sum-of-squares: 6.063

Number of iterations to convergence: 9 
Achieved convergence tolerance: 7.553e-06
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.