0% found this document useful (0 votes)
51 views9 pages

R Notebook

This R Notebook document demonstrates how to execute R code chunks and view the results. The code loads packages, imports data on sea level and temperature, and runs a function to plot the rolling correlation between the two time series variables over different window widths. The output includes the rolling correlation matrix and average filtered correlations.

Uploaded by

Yeiner Asprilla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

R Notebook

This R Notebook document demonstrates how to execute R code chunks and view the results. The code loads packages, imports data on sea level and temperature, and runs a function to plot the rolling correlation between the two time series variables over different window widths. The output includes the rolling correlation matrix and average filtered correlations.

Uploaded by

Yeiner Asprilla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

R Notebook

This is an R Markdown Notebook. When you execute code within the notebook, the
results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing
your cursor inside it and pressing Ctrl+Shift+Enter.
library(dLagM)

## Loading required package: nardl

## Registered S3 method overwritten by 'quantmod':


## method from
## as.zoo.data.frame zoo

## Loading required package: dynlm

## Loading required package: zoo

##
## Attaching package: 'zoo'

## The following objects are masked from 'package:base':


##
## as.Date, as.Date.numeric

data(seaLevelTempSOI)
level.ts <- ts(seaLevelTempSOI[1499:1595,1], start = c
(2005,5), freq = 12)
temp.ts <- ts(seaLevelTempSOI[1499:1595,2], start = c
(2005,5), freq = 12)
rolCorPlot(y = level.ts, x = temp.ts, width = c(3, 5, 7, 11),level =
0.95,SDtest = TRUE)
## $rolCor
## [,1] [,2] [,3] [,4]
## [1,] NA NA NA NA
## [2,] NA NA NA NA
## [3,] -0.80829258 NA NA NA
## [4,] -0.98506943 NA NA NA
## [5,] -0.98353272 -0.96690888 NA NA
## [6,] -0.96599857 -0.96978535 NA NA
## [7,] -0.11769574 -0.91773425 -0.94067583 NA
## [8,] -0.01667711 0.27946127 -0.50733959 NA
## [9,] 0.75682230 0.47907506 -0.27053365 NA
## [10,] 0.14161455 -0.20824115 -0.19414336 NA
## [11,] -0.92468905 -0.52791230 -0.62575332 -0.6460227703
## [12,] -0.21032136 -0.43784909 -0.65645433 -0.6756651342
## [13,] -0.97203686 -0.85995212 -0.59894259 -0.6768798000
## [14,] -0.51696586 -0.45541525 -0.39118475 -0.5696614320
## [15,] 0.49215927 -0.56578789 -0.54542570 -0.5438324633
## [16,] 0.54352910 -0.14778212 -0.36847110 -0.4934474884
## [17,] -0.91790964 0.01754784 -0.30444862 -0.3525666206
## [18,] 0.62492739 0.48821771 0.03292092 -0.1661227182
## [19,] -0.96989747 -0.88998271 -0.13519045 -0.4138037234
## [20,] -0.95095992 -0.95642413 -0.63009061 -0.6663431335
## [21,] 0.38996388 -0.28290321 -0.34989140 -0.4001623062
## [22,] 0.95693605 0.03408647 -0.21978430 -0.2541356704
## [23,] 0.99704117 0.45177753 -0.14000455 -0.2014062349
## [24,] -0.12049201 0.09367821 -0.06321413 -0.2152822178
## [25,] -0.98457093 -0.27389189 -0.15028402 -0.3110461881
## [26,] -0.87407171 -0.67945553 -0.28429576 -0.3582141344
## [27,] -0.15102353 -0.82502366 -0.26395856 -0.3349703416
## [28,] -0.79834640 -0.63803138 -0.47992229 -0.3023047477
## [29,] 0.50717983 -0.48123804 -0.74057981 -0.2768693742
## [30,] 0.82246232 -0.43261631 -0.53344835 -0.2795875032
## [31,] 0.91344905 0.73130097 -0.08906345 -0.1057142201
## [32,] 0.57090172 0.63596638 0.46294892 0.1379236334
## [33,] -0.08167978 0.69740614 0.65944925 0.3657858316
## [34,] 0.92153661 0.87132558 0.72822188 0.3876046134
## [35,] 0.55464765 0.61222284 0.61086945 0.3907775411
## [36,] -0.92851913 0.47270274 0.40927734 0.3707377622
## [37,] -0.99999713 0.17090545 0.38975782 0.3669318285
## [38,] -0.89000253 -0.88171221 0.47103387 0.3930126146
## [39,] 0.84675074 0.52121548 0.21813766 0.3523956080
## [40,] 0.10460699 -0.04790182 -0.43590786 0.3380053997
## [41,] -0.90658794 -0.35434962 -0.39630762 0.2511343331
## [42,] -0.66341972 -0.44039768 -0.56281339 0.1414519850
## [43,] 0.48236092 -0.65383724 -0.31104000 -0.0095184205
## [44,] 0.59048827 0.10175605 -0.25524890 -0.3125820863
## [45,] 0.97254751 0.48289704 -0.39503042 -0.1839955138
## [46,] 0.76848193 0.52696956 0.28875922 -0.0620813730
## [47,] 0.12560305 0.66676757 0.22184009 0.0037671023
## [48,] 0.84198084 0.67706202 0.41662397 0.0848852042
## [49,] 0.88425370 0.86186687 0.64835347 -0.0680198901
## [50,] 0.61620613 0.86274213 0.71060513 0.3631434073
## [51,] 0.81699175 0.82385991 0.87816235 0.3140463196
## [52,] 0.81029344 0.25399633 0.84576210 0.4561843894
## [53,] 0.94952926 0.81975033 0.44407207 0.5459270617
## [54,] 0.99997102 0.83228699 0.38976215 0.5686231798
## [55,] -0.05087468 -0.14913338 -0.01422884 0.5015904466
## [56,] -0.96488259 -0.05669333 0.05135528 0.3976978542
## [57,] -0.92316250 0.14459738 0.07172645 0.1952501675
## [58,] -0.87436633 -0.88008823 -0.52446908 -0.4439782689
## [59,] -0.85074258 -0.89045337 -0.58433128 -0.5375661693
## [60,] 0.64576882 -0.25136351 -0.54263151 -0.3875139076
## [61,] 0.93828969 0.54931219 -0.20165003 -0.1994887754
## [62,] 0.99720249 0.86853665 0.40896109 0.0316292007
## [63,] 0.73404096 0.86383690 0.66873493 0.0615629704
## [64,] 0.82226403 0.14793395 0.57144819 0.1204070095
## [65,] 0.87151335 0.87437025 0.58793496 0.1197330292
## [66,] 0.95646678 0.90864087 0.81056868 0.5371964462
## [67,] -0.32818452 0.10407143 0.26119128 0.1815770361
## [68,] 0.13740221 0.08212997 0.21072960 0.0451990437
## [69,] -0.13836163 0.37438723 0.23552217 0.1319939986
## [70,] -0.59124932 0.10509039 0.12817998 0.0889261235
## [71,] -0.02206775 -0.56843056 0.09058616 0.1680204799
## [72,] -0.71089258 -0.30983837 0.03231995 0.1311559329
## [73,] -0.19599345 0.13573536 -0.23843839 0.1160419506
## [74,] 0.99617561 0.19702672 0.35248150 0.2291406109
## [75,] -0.52126994 0.22314719 0.44793845 0.2389993138
## [76,] 0.71570618 0.49726793 0.27052930 0.2444581775
## [77,] 0.36554297 0.35934415 0.16388087 -0.0004544659
## [78,] 0.21005866 0.72880638 0.41000263 0.2853121487
## [79,] 0.65096876 0.62735253 0.66353341 0.3208067471
## [80,] -0.33695378 0.38910886 0.75127876 0.5674041610
## [81,] -0.76857240 0.15914883 0.60507577 0.5838682425
## [82,] 0.93953053 -0.12705677 0.18506496 0.5226778359
## [83,] 0.97445886 0.62357388 0.55255006 0.6683867988
## [84,] 0.95839373 0.93673165 0.73972568 0.7295404167
## [85,] 0.84197819 0.93938019 0.74981970 0.6910967650
## [86,] 0.94593663 0.95455678 0.90909762 0.6476946207
## [87,] 0.89436650 0.92441088 0.93252098 0.6447573913
## [88,] 0.99146412 0.83665664 0.86583536 0.6975318589
## [89,] 0.92815932 0.63156342 0.80601482 0.7206240907
## [90,] 0.96756174 0.93116385 0.77468079 0.8706483881
## [91,] 0.97971745 0.41218531 0.45563032 0.7626952761
## [92,] -0.15353584 0.02439555 0.11521992 0.4676102588
## [93,] -0.53717908 0.30257941 0.28704408 0.4898742903
## [94,] 0.99965378 0.16072238 0.34881392 0.4455868717
## [95,] -0.84166999 -0.52726397 0.34984953 0.3309362208
## [96,] -0.22847255 -0.08471890 0.14006055 0.2879856432
## [97,] 0.92964433 0.33772599 -0.14219590 0.3507522939
##
## $rolcCor.avr.filtered
## [1] NaN NaN -0.97522080 -0.97522080 -0.97522080
-0.96789196
## [7] -0.65870194 -0.08151847 -0.08151847 -0.08692332 -0.49507248
-0.68109436
## [13] -0.49507248 -0.48330682 -0.29072170 -0.29072170 -0.11654290
-0.38934426
## [19] -0.60221859 -0.60221859 -0.16074826 0.12927564 0.12927564
-0.07632754
## [25] -0.42994826 -0.42994826 -0.54900928 -0.39374402 -0.24787685
-0.10579746
## [31] 0.36249309 0.41024036 0.45193516 0.54212937 0.54212937
0.08104968
## [37] -0.01810051 -0.01810051 -0.01029932 -0.01029932 -0.35152771
-0.35152771
## [43] -0.12300868 0.03110333 0.21910466 0.25449445 0.38053233
0.50513801
## [49] 0.58161354 0.63817420 0.63817420 0.68981968 0.68981968
0.68981968
## [55] 0.07183839 -0.12789713 -0.14313069 -0.68072548 -0.68072548
-0.13393503
## [61] 0.27161577 0.57658236 0.57658236 0.58204394 0.61338790
0.61338790
## [67] 0.11886521 0.11886521 0.11886521 -0.06726321 -0.08297292
-0.08297292
## [73] -0.04566363 0.09720375 0.43199040 0.22207838 0.40854495
0.40854495
## [79] 0.40854495 0.34270950 0.34270950 0.38005414 0.70474240
0.80556871
## [85] 0.84109787 0.84901394 0.84901394 0.84787199 0.84787199
0.77159041
## [91] 0.65255709 0.13557968 0.13557968 0.13557968 0.02871369
0.02871369
## [97] 0.02871369
##
## $rolcCor.avr.raw
## [1] NaN NaN -0.80829258 -0.98506943 -0.97522080
-0.96789196
## [7] -0.65870194 -0.08151847 0.32178790 -0.08692332 -0.68109436
-0.49507248
## [13] -0.77695284 -0.48330682 -0.29072170 -0.11654290 -0.38934426
0.24498583
## [19] -0.60221859 -0.80095445 -0.16074826 0.12927564 0.27685198
-0.07632754
## [25] -0.42994826 -0.54900928 -0.39374402 -0.55465120 -0.24787685
-0.10579746
## [31] 0.36249309 0.45193516 0.41024036 0.72717217 0.54212937
0.08104968
## [37] -0.01810051 -0.22691706 0.48462487 -0.01029932 -0.35152771
-0.38129470
## [43] -0.12300868 0.03110333 0.21910466 0.38053233 0.25449445
0.50513801
## [49] 0.58161354 0.63817420 0.70826508 0.59155906 0.68981968
0.69766083
## [55] 0.07183839 -0.14313069 -0.12789713 -0.68072548 -0.71577335
-0.13393503
## [61] 0.27161577 0.57658236 0.58204394 0.41551330 0.61338790
0.80321819
## [67] 0.05466381 0.11886521 0.15088544 -0.06726321 -0.08297292
-0.21431377
## [73] -0.04566363 0.44370611 0.09720375 0.43199040 0.22207838
0.40854495
## [79] 0.56566536 0.34270950 0.14488011 0.38005414 0.70474240
0.84109787
## [85] 0.80556871 0.86432141 0.84901394 0.84787199 0.77159041
0.88601369
## [91] 0.65255709 0.11342247 0.13557968 0.48869424 -0.17203705
0.02871369
## [97] 0.36898168
##
## $rolCor.sd
## [1] 0.7431643 0.5806556 0.4800409 0.3975792
##
## $rawCor
## [1] 0.1396945
##
## $sdPercentiles
## [,1] [,2]
## [1,] 0.7432499 0.6469011
## [2,] 0.5531200 0.4089674
## [3,] 0.4678070 0.3169165
## [4,] 0.3783545 0.2053271
##
## $test
## Width SDrolCor 95% 5%
## 1 3 0.7431643 0.7432499 0.6469011
## 2 5 0.5806556 0.5531200 0.4089674
## 3 7 0.4800409 0.4678070 0.3169165
## 4 11 0.3975792 0.3783545 0.2053271

## Finite distributed lag models


dlmFit1 <- dlm(formula = GMSL ~ LandOcean,data = seaLevelTempSOI, q = 7)
summary(dlmFit1)

##
## Call:
## lm(formula = as.formula(model.formula), data = design)
##
## Residuals:
## Min 1Q Median 3Q Max
## -19.8515 -2.5781 0.1053 2.5427 19.9983
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.008943 0.101086 -0.088 0.9295
## LandOcean.t 0.746159 1.153242 0.647 0.5177
## LandOcean.1 -0.176851 1.158004 -0.153 0.8786
## LandOcean.2 -1.618818 1.154572 -1.402 0.1611
## LandOcean.3 -1.977914 1.151842 -1.717 0.0861 .
## LandOcean.4 -2.488326 1.151439 -2.161 0.0308 *
## LandOcean.5 -2.288856 1.154280 -1.983 0.0475 *
## LandOcean.6 -2.248660 1.157732 -1.942 0.0523 .
## LandOcean.7 -2.441134 1.153526 -2.116 0.0345 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.028 on 1579 degrees of freedom
## Multiple R-squared: 0.01724, Adjusted R-squared: 0.01226
## F-statistic: 3.463 on 8 and 1579 DF, p-value: 0.0005765
##
## AIC and BIC values for the model:
## AIC BIC
## 1 8942.719 8996.422

remove = list(LandOcean = c(0,1,2))


dlmFit2 <- dlm(formula = GMSL ~ -1 + LandOcean,data = seaLevelTempSOI, q
= 6, remove = remove)
summary(dlmFit2)
##
## Call:
## lm(formula = as.formula(model.formula), data = design)
##
## Residuals:
## Min 1Q Median 3Q Max
## -20.0704 -2.6089 0.0976 2.5265 20.1688
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## LandOcean.3 -2.072 1.109 -1.868 0.0619 .
## LandOcean.4 -2.266 1.116 -2.030 0.0425 *
## LandOcean.5 -2.147 1.116 -1.923 0.0547 .
## LandOcean.6 -2.480 1.108 -2.237 0.0254 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.034 on 1585 degrees of freedom
## Multiple R-squared: 0.01345, Adjusted R-squared: 0.01096
## F-statistic: 5.4 on 4 and 1585 DF, p-value: 0.0002555
##
## AIC and BIC values for the model:
## AIC BIC
## 1 8947.933 8974.788

## Koyck distributed lag models

KoyckFit <- koyckDlm(x = seaLevelTempSOI$LandOcean,y =


seaLevelTempSOI$GMSL)
summary(KoyckFit, diagnostics=TRUE)

##
## Call:
## "Y ~ (Intercept) + Y.1 + X.t"
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.74923 -1.75048 -0.02326 1.68498 11.97262
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.008036 0.070254 0.114 0.909
## Y.1 0.733753 0.017582 41.734 <2e-16 ***
## X.t -2.553163 3.607947 -0.708 0.479
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.805 on 1591 degrees of freedom
## Multiple R-Squared: 0.5297, Adjusted R-squared: 0.5291
## Wald test: 906.6 on 2 and 1591 DF, p-value: < 2.2e-16
##
## Diagnostic tests:
## df1 df2 statistic p-value
## Weak instruments 1 1591 70.3332073 1.088953e-16
## Wu-Hausman 1 1590 0.9925055 3.192824e-01
##
## alpha beta phi
## Geometric coefficients: 0.03018417 -2.553163 0.7337525

## Autoregressive distributed lag models

formula1 <- GMSL ~ LandOcean + SOI


remove <- list(p = list(SOI = c(2)))
ARDLfit1 <- ardlDlm(formula = formula1, data = seaLevelTempSOI, p = 2, q
= 4, remove = remove)
summary(ARDLfit1)

##
## Time series regression with "ts" data:
## Start = 5, End = 1595
##
## Call:
## dynlm(formula = as.formula(model.text), data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.0820 -1.2365 -0.0346 1.1360 7.5509
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.003920 0.049067 0.080 0.93633
## LandOcean.t 1.129695 0.533647 2.117 0.03442 *
## LandOcean.1 0.291991 0.545341 0.535 0.59243
## LandOcean.2 -1.369952 0.531626 -2.577 0.01006 *
## SOI.t -0.010608 0.006061 -1.750 0.08027 .
## SOI.1 0.002837 0.006078 0.467 0.64071
## GMSL.1 0.951282 0.024389 39.004 < 2e-16 ***
## GMSL.2 0.081073 0.028575 2.837 0.00461 **
## GMSL.3 -0.745129 0.028568 -26.083 < 2e-16 ***
## GMSL.4 0.240132 0.024381 9.849 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.957 on 1581 degrees of freedom
## Multiple R-squared: 0.7718, Adjusted R-squared: 0.7705
## F-statistic: 594 on 9 and 1581 DF, p-value: < 2.2e-16

remove <- list(p = list(LandOcean = c(1), SOI = c(1,2)))


ARDLfit2 <- ardlDlm(formula = formula1, data = seaLevelTempSOI, p = 2, q
= 4, remove = remove)
summary(ARDLfit2)

##
## Time series regression with "ts" data:
## Start = 5, End = 1595
##
## Call:
## dynlm(formula = as.formula(model.text), data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.060 -1.237 -0.030 1.159 7.504
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.003949 0.049043 0.081 0.93583
## LandOcean.t 1.185116 0.521855 2.271 0.02328 *
## LandOcean.2 -1.320628 0.519258 -2.543 0.01108 *
## SOI.t -0.008862 0.004722 -1.877 0.06074 .
## GMSL.1 0.951720 0.024314 39.142 < 2e-16 ***
## GMSL.2 0.080261 0.028509 2.815 0.00493 **
## GMSL.3 -0.744486 0.028536 -26.089 < 2e-16 ***
## GMSL.4 0.239964 0.024349 9.855 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.956 on 1583 degrees of freedom
## Multiple R-squared: 0.7717, Adjusted R-squared: 0.7707
## F-statistic: 764.4 on 7 and 1583 DF, p-value: < 2.2e-16

You might also like