In a previous entry the error was that the (data.frame ?) GDP1_2
could not be found and a line later you try to use x
that was not defined.
In the last entry the format of your da
data.frame was not good: I corrected it in the code.
library(NonlinearTSA)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(purrr)
set.seed(123)
da = data.frame(
stringsAsFactors = FALSE,
series1 = c("series2","1","10364.956",
"9036.963","2","11294.906","9847.764","3","11003.601",
"9593.782","4","10298.415","8978.948","5",
"12260.282","10689.453","6","11636.513","10145.603","7",
"9854.315","8591.746","8","11071.253","9652.766","9",
"11784.406","10274.548","10","10209.250","8901.207",
"11","9281.871","8092.646","12","10120.241","8823.601",
"13","10110.924","8815.478","14","9676.897",
"8437.060","15","10644.657","9280.827","16","10345.933",
"9020.377","17","10737.304","9361.604","18",
"11342.680","9889.417","19","11983.218","10447.888","20",
"11772.107","10263.825","21","12522.962","10918.478",
"22","12034.415","10492.525","23","12162.756",
"10604.422","24","11870.582","10349.683","25","11752.067",
"10246.352","26","12058.674","10513.675","27",
"11686.437","10189.131","28","11615.530","10127.309","29",
"11969.968","10436.336","30","11304.754","9856.350",
"31","11563.501","10081.946","32","11379.801",
"9921.782","33","12135.886","10580.995","34","12404.936",
"10815.573","35","12360.136","10776.514","36",
"12066.902","10520.850","37","12963.839","11302.868","38",
"12269.519","10697.506","39","12498.734","10897.354",
"40","12415.221","10824.541","41","12206.651",
"10642.693","42","12563.076","10953.452","43",
"12852.312","11205.631","44","12550.040","10942.086","45",
"12374.617","10789.139","46","12556.925","10948.089",
"47","12951.392","11292.016","48","13541.552",
"11806.563","49","13396.669","11680.243","50","13317.196",
"11610.952","51","13510.226","11779.250","52",
"13217.920","11524.395","53","13868.934","12091.999","54",
"13992.017","12199.312","55","13599.989","11857.512",
"56","14012.198","12216.907","57","13774.808",
"12009.933","58","14568.218","12701.689","59","14770.864",
"12878.371","60","15004.525","13082.094","61",
"15440.562","13462.264","62","16055.198","13998.151",
"63","15608.598","13608.771","64","15557.349",
"13564.089","65","15550.734","13558.321","66","15424.645",
"13448.387","67","15630.701","13628.043","68",
"15738.359","13721.907","69","15883.316","13848.292","70",
"16780.869","14630.847","71","16322.372","14231.095",
"72","16627.296","14496.951","73","16202.553",
"14126.627","74","16491.447","14378.507","75","16124.602",
"14058.664","76","16639.779","14507.834","77",
"17178.750","14977.750","78","17675.692","15411.022","79",
"17552.409","15303.535","80","17504.365",
"15261.647","81","18304.071","15958.892","82","18825.509",
"16413.521","83","18637.946","16249.989","84",
"18542.012","16166.346","85","18615.162","16230.124","86",
"18485.428","16117.012","87","18808.771","16398.927",
"88","18495.652","16125.926","89","18453.289",
"16088.991","90","18866.279","16449.067","91","20298.118",
"17697.454","92","20385.070","17773.265","93",
"21204.286","18487.521","94","20860.450","18187.738","95",
"20185.320","17599.108","96","22377.818","19510.696",
"97","22779.405","19860.831","98","22614.071",
"19716.679","99","22831.421","19906.182","100",
"23431.460","20429.341")
)
# da has not the correct format (look at it) therefore correct
series1 = as.numeric(da$series1[seq(3,length(da$series1),3)])
series2 = as.numeric(da$series1[seq(4,length(da$series1),3)])
da_corrected = data.frame(series1=series1,series2=series2)
# this is the correct format (look at difference with da)
y <- apply(da_corrected,2,Sollis_2004_unit_root,model = 3, max_lags = 6)
names(y)
#> [1] "series1" "series2"
# broom::glance(y[[1]]) # no broom routine available (?)
# print model results with the name of model
purrr::iwalk(y, function(yd,yn) {
cat(paste('\n Result model',yn,":\n\n" ))
print(yd)
}
)
#>
#> Result model series1 :
#>
#> $Model
#>
#> Non linear autoregressive model
#>
#> SETAR model ( 2 regimes)
#> Coefficients:
#> phiL.1 phiH.1 Dphi. 1
#> -0.5009353 -0.5353598 0.0110481
#>
#> Threshold:
#> -Variable: Z(t) = + (1) X(t)
#> -Value: 0 (fixed)
#> Proportion of points in low regime: 46.94% High regime: 53.06%
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1307.019 -275.034 19.989 277.564 1775.622
#>
#> Fit:
#> residuals variance = 248558, AIC = 1248, MAPE = 114.6%
#>
#> Coefficient(s):
#>
#> Estimate Std. Error t value Pr(>|t|)
#> phiL.1 -0.500935 0.130864 -3.8279 0.0002290 ***
#> phiH.1 -0.535360 0.139976 -3.8246 0.0002316 ***
#> Dphi. 1 0.011048 0.100733 0.1097 0.9128924
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Threshold
#> Variable: Z(t) = + (1) X(t)
#>
#> Value: 0 (fixed)
#>
#> $`Selected lag`
#> [1] 1
#>
#> $`p1=p2=0 Statistic`
#> [1] 12.73111
#>
#>
#> Result model series2 :
#>
#> $Model
#>
#> Non linear autoregressive model
#>
#> SETAR model ( 2 regimes)
#> Coefficients:
#> phiL.1 phiH.1 Dphi. 1
#> -0.50116068 -0.53675454 0.01133583
#>
#> Threshold:
#> -Variable: Z(t) = + (1) X(t)
#> -Value: 0 (fixed)
#> Proportion of points in low regime: 46.94% High regime: 53.06%
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1137.958 -239.844 16.838 242.025 1548.452
#>
#> Fit:
#> residuals variance = 188820, AIC = 1221, MAPE = 114.8%
#>
#> Coefficient(s):
#>
#> Estimate Std. Error t value Pr(>|t|)
#> phiL.1 -0.501161 0.130811 -3.8312 0.0002263 ***
#> phiH.1 -0.536755 0.140081 -3.8317 0.0002259 ***
#> Dphi. 1 0.011336 0.100710 0.1126 0.9106127
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Threshold
#> Variable: Z(t) = + (1) X(t)
#>
#> Value: 0 (fixed)
#>
#> $`Selected lag`
#> [1] 1
#>
#> $`p1=p2=0 Statistic`
#> [1] 12.76459
# example for extracting (only) the coefficients for one (first) model
df1 <- as.data.frame(y[[1]]$Model$coef) %>%
mutate(var=rownames(.))
df1 <- cbind(data.frame(modelname=names(y)[1]),df1 )
print(df1)
#> modelname Estimate Std. Error t value Pr(>|t|) var
#> 1 series1 -0.5009353 0.1308643 -3.8278998 0.0002289815 phiL.1
#> 2 series1 -0.5353598 0.1399763 -3.8246472 0.0002316216 phiH.1
#> 3 series1 0.0110481 0.1007333 0.1096767 0.9128923554 Dphi. 1
# example for extracting (only) the coefficients for all models
df_all <- purrr::imap_dfr(y, function(yd,yn) {
df <- as.data.frame(yd$Model$coef) %>%
mutate(var=rownames(.))
cbind(data.frame(modelname=yn),df )
}
)
print(df_all)
#> modelname Estimate Std. Error t value Pr(>|t|) var
#> 1 series1 -0.50093527 0.1308643 -3.8278998 0.0002289815 phiL.1
#> 2 series1 -0.53535980 0.1399763 -3.8246472 0.0002316216 phiH.1
#> 3 series1 0.01104810 0.1007333 0.1096767 0.9128923554 Dphi. 1
#> 4 series2 -0.50116068 0.1308108 -3.8311882 0.0002263414 phiL.1
#> 5 series2 -0.53675454 0.1400813 -3.8317367 0.0002259039 phiH.1
#> 6 series2 0.01133583 0.1007100 0.1125591 0.9106127080 Dphi. 1
# write coefficients for all models to csv file (to be read in xls file)
write.csv(df_all,'output.csv') # see ?write.csv for more arguments if necessary
Created on 2020-07-19 by the reprex package (v0.3.0)