Dear All,
Please I need support
I run a panel estimation (penalized spline fixed effects model), however there exist some form of serial correlation in the residuals (AR1 process). I am therefore using Cochrane-Orcutt style to transform my residuals to correct for serial correlation.
I have done this manually, however I need to iterate to get a consistent value of rho in my code below. I am a bit deficient regarding writing of functions for iteration. Below is my code and some explanation about what exactly I want to do.
Firstly, I estimate the panel model. Variables wrapped in parenthesis are non parametric. After estimating the model, I recovered the residuals and regressed over the lagged residuals (regression of AR1 process) using ols
library(plm)
library( Matrix)
library(nlme)
library(mgcv)
library(splines)
Raw_data <- read.csv("C:/Users/Benny/OneDrive/Documents/R/DataSet.csv", header = TRUE)
PanelS <- pdata.frame(Raw_data, index=c("Country","Year"))
mod1 <- pam(A ~ B + sfe(C) + D, data = PanelS)
res_1 <- ts((mod1$residuals))
res_Lag <- lag(res_1, -1)
lagdata1 <- ts.intersect(res_1, res_Lag)
rho_1 <- coef(lm(res_1 ~ res_Lag -1, data = lagdata1 )) # coefficient of regressing residual over lagged residual using ols
## Tranforming the variables with rho_1
A_bar = PanelS$A - rho_1*(lag( PanelS$A))
B_bar= PanelS$B - rho_1*(lag( PanelS$B))
C_bar= PanelS$C- rho_1*(lag( PanelS$C))
D_bar= PanelS$C- rho_1*(lag( PanelS$D))
PanelS1 <- pdata.frame(data.frame(cbind(PanelS$Country, PanelS$Year, A_bar,B_bar,C_bar, D_bar)), index=c("V1","V2"))
After transforming the variables with rho_1, I proceed to estimate model_2
mod_2 <- pam(A_bar ~ B_bar + sfe(C_bar)+ D_bar,data = PanelS1)
res_2 <- ts((mod_2$residuals))
resLag2 <- lag(res_2, -1)
lagdata2 <- ts.intersect(res_2, resLag2)
rho_2 <- coef(lm(res_2 ~ resLag2 -1, data = lagdata2 )) #coefficient of regressing residual over lagged residual
## Tranforming the variables with rho_2
A_bar = PanelS$A - rho_2*(lag( PanelS$A))
B_bar= PanelS$B - rho_2*(lag( PanelS$B))
C_bar= PanelS$C- rho_2*(lag( PanelS$C))
PanelS2<- pdata.frame(data.frame(cbind(PanelS$Country, PanelS$Year, A_bar,B_bar,C_bar, D_bar)), index=c("V1","V2"))
mod_3 <- pam(A_bar ~ B_bar + sfe(C_bar)+ D_bar,data = PanelS2)
res_3 <- ts((mod_3$residuals))
resLag3 <- lag(res_3, -1)
lagdata3 <- ts.intersect(res_2, resLag2)
rho_3 <- coef(lm(res_3 ~ resLag3 -1, data = lagdata3 ))
rho_3
Now I am doing this until rho_3 converges (or rho_3 is equal to rho_2 )- so iteration until the rho value gets equal to the previous rho_value.
I would appreciate if someone can help me with a function to iterate until convergence.
Below is the panel data
structure(list(Country = c("CountryA", "CountryA", "CountryA",
"CountryA", "CountryA", "CountryA", "CountryA", "CountryA", "CountryA",
"CountryA", "CountryA", "CountryA", "CountryA", "CountryA", "CountryA",
"CountryA", "CountryA", "CountryA", "CountryA", "CountryA", "CountryB",
"CountryB", "CountryB", "CountryB", "CountryB", "CountryB", "CountryB",
"CountryB", "CountryB", "CountryB", "CountryB", "CountryB", "CountryB",
"CountryB", "CountryB", "CountryB", "CountryB", "CountryB", "CountryB",
"CountryB", "CountryC", "CountryC", "CountryC", "CountryC", "CountryC",
"CountryC", "CountryC", "CountryC", "CountryC", "CountryC", "CountryC",
"CountryC", "CountryC", "CountryC", "CountryC", "CountryC", "CountryC",
"CountryC", "CountryC", "CountryC", "CountryD", "CountryD", "CountryD",
"CountryD", "CountryD", "CountryD", "CountryD", "CountryD", "CountryD",
"CountryD", "CountryD", "CountryD", "CountryD", "CountryD", "CountryD",
"CountryD", "CountryD", "CountryD", "CountryD", "CountryD", "CountryE",
"CountryE", "CountryE", "CountryE", "CountryE", "CountryE", "CountryE",
"CountryE", "CountryE", "CountryE", "CountryE", "CountryE", "CountryE",
"CountryE", "CountryE", "CountryE", "CountryE", "CountryE", "CountryE",
"CountryE", "CountryF", "CountryF", "CountryF", "CountryF", "CountryF",
"CountryF", "CountryF", "CountryF", "CountryF", "CountryF", "CountryF",
"CountryF", "CountryF", "CountryF", "CountryF", "CountryF", "CountryF",
"CountryF", "CountryF", "CountryF", "CountryG", "CountryG", "CountryG",
"CountryG", "CountryG", "CountryG", "CountryG", "CountryG", "CountryG",
"CountryG", "CountryG", "CountryG", "CountryG", "CountryG", "CountryG",
"CountryG", "CountryG", "CountryG", "CountryG", "CountryG", "CountryH",
"CountryH", "CountryH", "CountryH", "CountryH", "CountryH", "CountryH",
"CountryH", "CountryH", "CountryH", "CountryH", "CountryH", "CountryH",
"CountryH", "CountryH", "CountryH", "CountryH", "CountryH", "CountryH",
"CountryH", "CountryI", "CountryI", "CountryI", "CountryI", "CountryI",
"CountryI", "CountryI", "CountryI", "CountryI", "CountryI", "CountryI",
"CountryI", "CountryI", "CountryI", "CountryI", "CountryI", "CountryI",
"CountryI", "CountryI", "CountryI", "CountryJ", "CountryJ", "CountryJ",
"CountryJ", "CountryJ", "CountryJ", "CountryJ", "CountryJ", "CountryJ",
"CountryJ", "CountryJ", "CountryJ", "CountryJ", "CountryJ", "CountryJ",
"CountryJ", "CountryJ", "CountryJ", "CountryJ", "CountryJ"),
Year = c(2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L,
2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L,
2016L, 2017L, 2018L, 2019L, 2000L, 2001L, 2002L, 2003L, 2004L,
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L,
2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 2000L, 2001L, 2002L,
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 2000L,
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L,
2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L,
2019L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L,
2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L,
2017L, 2018L, 2019L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L,
2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L,
2015L, 2016L, 2017L, 2018L, 2019L, 2000L, 2001L, 2002L, 2003L,
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L,
2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L, 2000L, 2001L,
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2018L, 2019L,
2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L,
2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L,
2018L, 2019L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L,
2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L,
2016L, 2017L, 2018L, 2019L), A = c(0.051539, 0.064525, 0.014292,
0.018774, 0.035449, 0.021988, 0.02396, 0.011415, 0.010358,
-0.029607, -0.020427, -0.012734, 0.006683, 0.007373, -0.039712,
-0.005499, 0.008682, 0.015326, 0.020524, 0.015101, 0.035355,
0.031157, 0.023387, 0.024198, 0.035353, 0.053873, 0.038743,
0.042338, 0.034935, 0.015377, 0.010599, 0.015154, 0.002919,
0.024291, 0.043819, 0.015901, 0.01897, 0.027767, 0.015992,
0.041976, 0.011223, 0.006144, 0.000778, 0.005873, 0.007194,
-0.022017, -0.023338, -0.037765, -0.049356, 0.026135, 0.035633,
0.015691, -0.006196, -0.00025, 0.001181, -0.001472, -0.009324,
-0.022664, -0.022623, -0.019586, -0.012207, -0.004603, -0.013073,
-0.010771, -0.009882, -0.014417, -0.031812, -0.043885, -0.050883,
-0.039834, -0.020299, -0.000684, 0.011216, 0.005419, 0.000939,
-0.005508, 0.006266, -0.008077, -0.016137, -0.012681, 0.031612,
0.043729, 0.009314, 0.002734, -0.012284, 0.002403, 0.016807,
0.019995, 0.033096, 0.024383, 0.010588, 0.019833, 0.031837,
0.03127, 0.029059, 0.020708, 0.019296, 0.017787, 0.032074,
0.027125, 0.005673, 0.003698, -5.3e-05, 0.001794, -0.011977,
-0.008686, -0.031588, -0.039411, -0.073931, -0.076715, -0.039171,
-0.025797, -0.007637, 0.00345, 0.009101, 0.01674, -0.006968,
-0.019178, -0.02438, -0.039663, 0.078313, 0.06707, 0.062822,
0.050771, 0.041274, 0.043921, 0.046429, 0.039418, 0.034671,
0.017356, 0.001054, 0.00414, 0.00226, 0.00275, 0.00085, 0.00495,
0.001276, -0.001446, -0.005771, -0.007513, 0.053734, 0.038679,
0.017375, 0.01438, 0.018403, 0.032943, 0.025539, 0.032463,
0.032267, 0.034009, 0.018229, 0.008958, 0.010079, 0.00749,
0.000604, 0.001948, 0.011782, 0.013253, 0.007898, 0.007546,
0.018052, -0.001123, -0.012597, -0.042292, -0.058516, -0.022736,
-0.03841, -0.050843, -0.073979, -0.097242, -0.024712, 0.038037,
0.048685, -0.00624, 0.075575, 0.044947, 0.097171, 0.086809,
0.079856, 0.068521, 0.008062, -0.00911, -0.010527, -4.3e-05,
0.002428, 0.004422, 0.008752, 0.019602, 0.01724, 0.01965,
-0.008816, 0.011466, 0.020956, 0.021873, 0.021772, 0.024495,
0.021354, 0.015267, 0.018769, 0.016904), C = c(0.75345, 0.70657,
0.645051, 0.510055, 0.433786, 0.35728, 0.265817, 0.208721,
0.163261, 0.130248, 0.136607, 0.153873, 0.152275, 0.166592,
0.170559, 0.27089, 0.259813, 0.292847, 0.253142, 0.222618,
0.56764082, 0.523543, 0.485083, 0.49081, 0.461501, 0.44156,
0.374122, 0.315494, 0.27346, 0.333132, 0.401818, 0.425879,
0.460709, 0.448942, 0.440456, 0.442703, 0.397737, 0.372338,
0.359446, 0.340254, 0.064305, 0.05107, 0.047682, 0.056584,
0.055981, 0.051134, 0.047025, 0.046318, 0.037655, 0.045041,
0.071989, 0.066074, 0.061057, 0.097641, 0.101621, 0.105545,
0.09996, 0.099131, 0.091119, 0.082012, 0.120817, 0.120871,
0.138383, 0.13023, 0.141247, 0.146088, 0.119133, 0.100396,
0.084592, 0.185873, 0.368416, 0.479167, 0.4367, 0.421837,
0.400428, 0.416259, 0.37072, 0.40398, 0.390126, 0.371126,
0.079576, 0.074647, 0.076712, 0.074295, 0.074504, 0.079053,
0.080224, 0.082991, 0.082006, 0.15357, 0.161465, 0.201522,
0.190049, 0.219974, 0.236873, 0.227428, 0.219862, 0.200938,
0.223426, 0.209529, 0.217219, 0.224867, 0.258694, 0.248207,
0.221093, 0.189452, 0.159052, 0.124236, 0.119492, 0.123362,
0.217807, 0.296186, 0.339882, 0.371345, 0.376212, 0.391509,
0.378059, 0.373931, 0.351043, 0.347354, 0.440547, 0.424547,
0.409236, 0.401795, 0.427482, 0.426416, 0.399297, 0.381117,
0.339041, 0.325607, 0.415314, 0.469047, 0.482712, 0.536225,
0.562292, 0.598259, 0.636417, 0.631764, 0.612668, 0.596271,
0.605061, 0.503479, 0.518971, 0.498057, 0.492731, 0.484527,
0.486885, 0.43596, 0.388967, 0.374978, 0.407324, 0.381025,
0.371731, 0.375149, 0.402248, 0.449982, 0.437387, 0.422554,
0.407331, 0.389125, 0.989067, 1.049344, 1.070812, 1.048631,
1.014561, 1.028734, 1.073949, 1.036117, 1.03103, 1.094155,
1.267447, 1.474942, 1.752192, 1.619444, 1.784347, 1.802256,
1.770079, 1.807951, 1.792139, 1.862386, 0.601394, 0.590658,
0.579365, 0.597035, 0.633089, 0.649877, 0.673465, 0.667047,
0.639942, 0.655222, 0.729901, 0.823816, 0.79801, 0.811354,
0.787169, 0.756694, 0.72207, 0.692768, 0.651024, 0.617801
), B = c(0.147502302, 0.043680673, -0.212478849, -0.266834333,
-0.228099071, -0.199890362, -0.968175801, 1.047500546, 1.273127656,
1.227657506, -0.286068921, -1.356896168, -1.442625298, -0.291748363,
2.029875219, 1.099611751, -1.112127832, -0.894025857, 0.103213651,
0.286801553, 0.756833023, 0.591945192, 0.525259532, 0.466656359,
0.706692697, -2.361722697, -2.777257989, -4.097114222, -4.564987155,
2.317853991, 3.44030537, 3.034469093, 5.845290721, 0.403542521,
0.128582254, 0.817094156, -0.886707561, -2.998573025, -0.491794488,
-0.856367773, 0.023343476, -0.209503364, -0.084839186, -0.146285026,
-0.256672799, -0.093852713, 0.145824486, 0.434606031, 0.966980327,
0.67904687, -0.292659443, -0.487763914, -0.084930583, -0.32722087,
-0.442172133, -0.168366978, -0.186469629, 0.046322287, 0.181126569,
0.303486593, 0.171541123, -0.348150815, -0.407466419, -0.624622679,
-0.354132366, -0.15050691, 0.700892294, 0.67692383, 1.014111655,
0.862019536, 0.395600738, -0.256706715, -0.542246369, -0.539422399,
-0.405088653, -0.247954994, -0.497333992, -0.010723655, 0.393516751,
0.169750037, -0.581903347, -0.730163914, 0.351894514, 0.629568917,
0.882078894, 0.760041333, -0.564317727, -0.57799292, -0.433736512,
0.513350369, 0.55464973, -0.224497194, -0.074326596, -0.123301819,
-0.432013928, -0.25316664, -0.374406673, 0.116449941, 0.308969388,
0.252824183, 2.398228162, -0.033362631, -1.681378615, -3.655293426,
-2.793256764, -3.636310622, 0.149490332, 3.951131246, 7.177449077,
4.831325877, 2.050070679, 1.314471427, -1.687424783, -3.796189127,
-3.329685346, -1.695252718, -3.010416797, -2.414597902, 1.199960369,
4.661041564, 0.531518012, -1.384184059, -0.64216453, -0.13206166,
0.249287935, -0.153010531, -0.987952985, -1.71711917, -0.678751076,
0.890062065, 1.663691535, 1.883735194, 2.171029985, 2.383501603,
1.490313839, -0.732542129, -0.291797363, -1.655272704, -1.613245217,
-1.275038743, -0.789256935, -3.589249982, 0.502475039, 1.840081099,
1.141218417, 3.130100399, 3.94751837, 0.97811035, 0.013586974,
-3.245960526, -2.068241886, -1.82476664, -1.481654499, 0.37039449,
-1.516414277, -1.722381744, 0.683458083, 0.153189319, 3.410781995,
0.067011953, -3.09418792, -4.09753755, -4.682167411, -1.333607727,
2.505605899, -4.332639317, -2.190945016, 4.048457741, 11.60535564,
13.61047901, 5.145259686, -0.712611552, -3.385649938, 7.214394614,
-10.34401695, -1.841542179, -6.437949187, -4.545422837, -0.012548047,
2.881273043, 3.227611639, 10.96399365, 16.38843255, 14.72001327,
-13.84595255, -10.51570643, -13.59695535, -36.70577424, -12.07070647,
12.51742535, 52.88207865, 9.143152612, -7.818895359, -15.57456939,
-21.31957866, -23.55720863, -5.574415019, 5.783084584, 12.02189272,
22.93207708), D = c(0.77780751, 0.793229898, 0.80623893,
0.821155065, 0.836880111, 0.854312944, 0.873660631, 0.890537317,
0.907536298, 0.912375095, 0.929637942, 0.946439284, 0.965000087,
0.97726773, 0.986870808, 1, 1.019208507, 1.037842597, 1.054711181,
1.072171599, 0.534008473, 0.566583199, 0.58762954, 0.601043497,
0.63362178, 0.673913677, 0.719447102, 0.799187909, 0.864173776,
0.899162389, 0.909465125, 0.96350569, 0.978220642, 0.971679886,
0.976158221, 1, 1.025374896, 1.065804414, 1.108567186, 1.166769344,
0.588726028, 0.64526073, 0.733094431, 0.718268082, 0.746291144,
0.799900392, 0.846050389, 0.894179583, 1.015232882, 0.982856394,
1.012948099, 1.041332642, 1.032947106, 1.013566583, 0.980944689,
1, 1.020576612, 1.061740647, 1.117831183, 1.159906251, 0.750587042,
0.769670674, 0.790024355, 0.801712216, 0.817505148, 0.83991247,
0.856517319, 0.878345181, 0.914006005, 0.920044857, 0.949573071,
0.955207703, 0.978810398, 0.985618398, 0.996205139, 1, 1.004364708,
1.017159213, 1.021013703, 1.02682649, 0.825278825, 0.836048671,
0.847570474, 0.858769029, 0.86834942, 0.871868036, 0.875331803,
0.890827568, 0.898928134, 0.915485416, 0.921392822, 0.931246968,
0.945182975, 0.963702812, 0.981800571, 1, 1.013277522, 1.026999204,
1.044176589, 1.067069774, 0.490666665, 0.523850087, 0.54906662,
0.570457925, 0.597126217, 0.632406036, 0.689467717, 0.775073059,
0.828560075, 0.827109078, 0.842215091, 0.887572897, 0.923280339,
0.960610381, 0.988936452, 1, 1.022699304, 1.054533263, 1.098615084,
1.134067127, 0.757140805, 0.809228408, 0.851488047, 0.884918505,
0.889385715, 0.916751643, 0.948479832, 0.960072842, 0.956196673,
0.911566837, 0.884542463, 0.89644222, 0.917048164, 0.929279352,
0.929337342, 1, 1.010128912, 1.026719845, 1.029923385, 1.062349178,
0.786853444, 0.804351028, 0.831286834, 0.859995963, 0.886334727,
0.906191485, 0.937863282, 0.969963165, 1.012104032, 1.038112793,
1.036283847, 1.046222, 1.043339336, 1.02279939, 1.002888566,
1, 0.994233243, 0.998082845, 0.997049083, 0.998951287, 0.740171055,
0.770579402, 0.802054487, 0.833603662, 0.865965514, 0.90147914,
0.937354271, 0.969378485, 0.99123068, 0.992657113, 0.994179737,
0.993983379, 0.992844694, 0.99680058, 0.994574042, 1, 1.003228988,
1.016266499, 1.028341184, 1.04261954, 0.801617134, 0.817716283,
0.834621959, 0.850140657, 0.863935678, 0.880664424, 0.899645623,
0.9226463, 0.944486016, 0.945115307, 0.95522518, 0.964280334,
0.975483583, 0.983073825, 0.988745617, 1, 1.005225593, 1.010468623,
1.020086873, 1.032605559)), class = "data.frame", row.names = c(NA,
-200L))
Thank you