如何在 r 中找到预测模型的 AIC、BIC、R2 和 RMSE?

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

我想做空气质量数据的预测,我应用了auto.arima和神经网络模型。 R2、RMSE、MAPE、AIC 和 BIC 是评估和选择最佳模型的标准。

我用于

arima

no <- ts(NO, start=c(2017,06,01), frequency=365)
model <- auto.arima(no, D=1)
finalmodel <- arima(no, order = c(0, 0, 2), seasonal = list(order = c(0,1,0), period = 365))
Forecastmodel<- forecast(finalmodel, h=365)

在这个模型中我只能有 AIC 和 BIC

对于神经网络:

no <- ts(NO, start=c(2017,06,01), frequency=365)
model <- nnetar(no)
forecast <- forecast(model, h=365)

那么,如何在 r 中找到每个模型的标准?对于预测模型还有其他建议吗?希望我使用了正确的代码

r forecasting evaluation
2个回答
1
投票

这是一个例子。

library(forecast)
# Create example data 
no <- ts(rnorm(100))
# Fit an ARIMA model
model <- auto.arima(no, D=1)
summary(model)
#> Series: no 
#> ARIMA(0,0,0) with zero mean 
#> 
#> sigma^2 estimated as 0.9299:  log likelihood=-138.26
#> AIC=278.52   AICc=278.56   BIC=281.13
#> 
#> Training set error measures:
#>                       ME      RMSE       MAE MPE MAPE      MASE        ACF1
#> Training set -0.09963973 0.9643283 0.7635588 100  100 0.6833166 -0.07970921
# Calculate forecasts
fc <- forecast(model)
# Compare forecasts against a test set
test <- ts(rnorm(20), start=101)
accuracy(fc, test)
#>                       ME      RMSE       MAE MPE MAPE      MASE        ACF1
#> Training set -0.09963973 0.9643283 0.7635588 100  100 0.6833166 -0.07970921
#> Test set     -0.23460384 0.8855929 0.7824913 100  100 0.7002595  0.14526351
#>              Theil's U
#> Training set        NA
#> Test set     0.8087404

reprex 包 (v2.0.0) 于 2021-06-12 创建

请注意,您不必重新估计

auto.arima()
选择的模型。它已经保存在
model
.

鉴于您似乎每天都有数据,您可能会考虑其他建模选项。有关一些建议,请参阅https://otexts.com/fpp2/weekly.html#daily-and-sub-daily-data


0
投票
import tensorflow as tf 

from scipy.stats import norm

  Function for calculating AIC
  def AIC_value(model,Y_pred,Y_test):

    Y_test = np.asarray(Y_test).astype('float32').reshape((-1,1))
    Y_pred_1 = np.asarray(Y_pred).astype('float32').reshape((-1,1))



    nll = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=Y_test, logits=Y_pred_1))

    --Calculate the number of parameters in the model
    num_params = model.count_params()

    --Calculate the AIC value for the model
    aic = 2 * num_params - 2 * nll

    --Print the AIC value
    print("AIC: ", aic.numpy())
© www.soinside.com 2019 - 2024. All rights reserved.