将自回归模型拟合到潮汐深度数据
此示例展示了如何为 ThingSpeak™通道中的数据拟合自回归 (AR) 模型并计算回归参数及其不确定性。自回归模型用于表示自然界中与时间相关的过程。
从 Ockway Bay 实时潮汐计读取数据
ThingSpeak通道50289 包含有关奥克威湾潮汐深度的数据。数据每 5 分钟收集一次。通道的字段 1 包含潮汐深度数据。
% Read the data using the |thingSpeakRead| function from channel 50289 on a particular day, for example, July 01, 2016.
startDate = datetime('July 1, 2016 12:01:00 AM'); endDate = datetime('July 2, 2016 12:01:00 AM'); dateRange = startDate:endDate; [data,timestamps] = thingSpeakRead(50289,'DateRange',dateRange,'Fields',1);
将 AR 模型拟合到数据
使用 iddata
函数创建潮汐深度数据的 iddata 对象。使用 detrend
确保数据的均值为零。
sampleTime = 5; IDdata = iddata(data,[],sampleTime,'OutputName',{'Tidal Depth'},'TimeUnit','minutes') IDdata = detrend(IDdata,0);
IDdata = Time domain data set with 288 samples. Sample time: 5 minutes Outputs Unit (if specified) Tidal Depth
使模型适合数据
由于潮汐深度随时间变化,因此请使用 ar
函数对数据拟合离散时间自回归模型。
modelOrder = 8; sys = ar(IDdata,modelOrder)
sys = Discrete-time AR model: A(z)y(t) = e(t) A(z) = 1 - 1.154 z^-1 - 0.1668 z^-2 + 0.2144 z^-3 + 0.2974 z^-4 - 0.4227 z^-5 + 0.1509 z^-6 - 0.1612 z^-7 + 0.2491 z^-8 Sample time: 5 minutes Parameterization: Polynomial orders: na=8 Number of free coefficients: 8 Use "polydata", "getpvec", "getcov" for parameters and their uncertainties. Status: Estimated using AR ('fb/now') on time domain data "IDdata". Fit to estimation data: 98.5% FPE: 0.04741, MSE: 0.04485
显示参数
使用 getpvec
函数显示估计参数及其不确定性。
[Parameters,Uncertainties] = getpvec(sys)
Parameters = -1.1543 -0.1668 0.2144 0.2974 -0.4227 0.1509 -0.1612 0.2491 Uncertainties = 0.0580 0.0918 0.0932 0.0918 0.0921 0.0970 0.0962 0.0647
输出显示估计的 AR 模型参数和估计参数的一个标准差值。
将参数写入 ThingSpeak
使用 thingSpeakWrite
函数将值数组写入 ThingSpeak,每个字段一个值。将数据转置为 8 x 1。更改 channelID
和 writeAPIKey
以将数据发送到您的通道。
channelID=17504; writeAPIKey='23ZLGOBBU9TWHG2H'; response = thingSpeakWrite(channelID,'Values',Parameters','WriteKey',writeAPIKey)
response = struct with fields: Field1: '-1.154266029802091' Field2: '-0.1668388400729965' Field3: '0.2143807521019717' Field4: '0.2973816840220466' Field5: '-0.4226981725238166' Field6: '0.1509427726183032' Field7: '-0.1612303290788889' Field8: '0.2490548535561231' Latitude: [] Longitude: [] ChannelID: 17504 Created: 10-Jan-2019 15:10:41 LastEntryID: 20736 Altitude: []
另请参阅
函数
thingSpeakRead
|iddata
(System Identification Toolbox) |detrend
(System Identification Toolbox) |ar
(System Identification Toolbox) |getpvec
(System Identification Toolbox)