1

I'm trying to fit an error correction model with the ecm package in R where I defined my long run and short run equilibrium variables as follows:

xeq<-mydata[c('A','Y')]
xtr<- mydata['U']
xeq<-as.data.frame(xeq)
xtr<-as.data.frame(xtr)

where mydata is a data frame of this kind:

    C     A     Y     U
1   NA    NA    NA    NA
2   NA    NA    NA   0.024
3   NA    NA    NA   0.002
4   NA    NA    NA   0.044
5  9.76  -5.2  173   0.038

However, when I run the following ecm function with C as the target variable

ecm1 <- ecm(mydata$C, xeq, xtr, includeIntercept = TRUE)

I get the following error message:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 91, 85

Why do I get this error even if the arguments actually have the same number of rows (=92). Is it the different amount of NAs in xtr or xeq causing the problem, and if so, what could I do to fix this?

Here the dataset for reproduction in R:

structure(list(C = c(NA, NA, NA, NA, 9.76192185331763, 7.01543928393462, 
6.15294777870576, 7.0921409337696, 9.46031493979471, 10.693108147905, 
13.1666277830036, 16.6211182252697, 18.0268426821637, 17.145607834061, 
17.1887391274879, 18.1073409297478, 17.523870901394, 16.0873056778556, 
15.8135508940265, 17.108241619624, 17.4189883666871, 17.2784174623317, 
17.6619298869731, 18.2967179845755, 17.3357442311411, 15.4458718249791, 
13.8707693828413, 13.0182977451227, 12.6002892909497, 11.9677719807028, 
12.3749563461462, 12.6384754111734, 11.1665802856963, 8.53320933441695, 
5.967439246063, 3.68776298143974, 0.729490689302392, -0.624339739451296, 
-0.975857285370441, 0.693986898388899, -0.567601555170971, -1.99103344911805, 
-2.80374927733694, -2.88268297279998, -3.01288121587993, -5.40108803200214, 
-7.50641385829334, -8.27610226978281, -4.23264654067398, -9.36865209495492, 
-5.85167227388649, -9.52597154857684, -7.00559924754299, -9.60352746037704, 
-6.59938201478927, -9.20668908016107, -8.47980282354013, -10.4108964126173, 
-11.8629275228041, -10.0287174604067, -18.3238303154354, -15.9011855206539, 
-15.9532724908242, -14.8020070306296, -14.8713152139315, -14.5073713460667, 
-14.0069899551305, -12.6349139513127, -12.7135632986697, -13.8308182379224, 
-14.9326580914742, -15.6173089676889, -14.2379949603095, -13.8406318535914, 
-13.5372258154192, -14.8365008110049, -16.1101335478164, -18.8507338624605, 
-18.7741631770463, -16.243278875675, -14.5819395443483, -14.1928496291539, 
-15.3033298417436, -15.1802008406216, -11.5998095045774, -12.8823931334143, 
-12.8735391629816, -11.9302608644667, -10.6963867777389, -13.2979574409471, 
-9.57728980909098, -9.07301116422297), A = c(NA, NA, NA, NA, 
-5.20153912216881, -5.07924451118444, -2.09333037295979, 3.74516675987993, 
5.87755978365845, 5.94349065725073, 6.18819331373968, 4.17442880942838, 
3.1198235843134, 4.29280020951734, 4.27103477707429, 3.53796802208126, 
5.37371202563011, 6.72401090220254, 3.43095077310223, -0.387565520388078, 
-1.15018917077455, -4.33529577197922, -7.6659277759221, -7.57008089829873, 
-1.45849405256799, 1.29601274485669, 4.84946540084445, 5.34435980749135, 
3.05005189230734, 2.11715327782416, 0.215479301828925, -2.16376287416417, 
-1.84693265115202, -0.219735254374265, -0.642547268434268, -1.3103541787109, 
-0.161405096990219, 0.0564896688836969, 0.80591194139128, -1.58517595451374, 
-3.37336093784992, -6.36151950812337, -9.55311724429195, -15.5224230958702, 
-15.393470459958, -10.3512962813924, -6.57616078890991, -5.02469617598635, 
-5.89214868630077, -3.05453405871426, -1.45908218312701, 0.259933743438046, 
1.33100099597394, 0.725772359875387, -2.16931367715731, 0.827652493966391, 
2.52539819938444, -1.40722845253297, 6.67162106968546, 2.59613855360963, 
-1.2523885956072, -0.440327177481203, 0.443946836853359, 0.203124258206477, 
0.750870877971566, 0.756781245963611, 2.67543896136817, 0.0114946384773384, 
-2.78410839115761, -3.78413174151928, -2.71140580594595, -3.01008725502782, 
-1.67922277918079, 0.215240851170084, 2.36522900278644, 4.24331163604759, 
6.42422027157119, 7.86670289578195, 10.0178374470668, 10.1942487065201, 
11.1191910212971, 14.2204706027751, 16.9499660919871, 7.78105027079865, 
0.579619348083327, -1.98044426018, -3.03885403046213, -4.13234669785987, 
-5.66668269768965, -5.33393721105306, -4.87293990319002, -4.16148509722758
), Y = c(NA, NA, NA, NA, 173.354360114913, 178, 176.618619491256, 
188, 183.766370215622, 187.231419875067, 193.087247456462, 192.850944761799, 
194.337281028297, 196.873209954365, 199.725923865044, 201.447520961882, 
204.9568730907, 207.720351726506, 214.078827302918, 213.32025435258, 
212.053632478142, 216.829212754763, 220.563088690461, 227.195676351159, 
234.311971590784, 240.655867480111, 242.910149567472, 246.730319171031, 
246.335519030986, 246.932382648863, 247.80304954829, 249.179698955599, 
248.636041464312, 248.529063516917, 248.991171675469, 250.612881190271, 
254.681120827704, 253.656659810001, 253.297107820783, 251.224930024703, 
247.893636938847, 249.975556269839, 252.59569718955, 254.099571131528, 
256.05278808891, 261.360088233385, 267.402034023258, 267.352997311491, 
268.589873123027, 270.311871875379, 272.58624852624, 278.771084885002, 
277.303298242839, 279.241906182601, 280.62532849455, 277.699374722288, 
278.348273939502, 275.914739278816, 278.567402101032, 278.755671739416, 
276.947085479511, 275.360297685103, 279.342320198295, 284.422141147169, 
285.986282073916, 286.606508816048, 287.365638345368, 289.869481576396, 
291.660409507719, 288.962602977485, 288.080473368628, 286.84220965921, 
289.650238765857, 289.563374660957, 291.837004419765, 294.246065199258, 
295.861037468374, 294.245966107061, 298.119154532571, 298.986928460466, 
3925349, 3966744, 4019691, 4061941, 4077791, 4127153, 4152763, 
4176014, 4260402, NA, NA, NA), U = c(NA, 0.0243894451488191, 
0.0350753527090906, -0.00285310486725987, -0.044253379024236, 
-0.0386027161344519, -0.0713350700777299, -0.0454188381545637, 
-0.0402939039836743, -0.0755418139218382, -0.0677755750374356, 
-0.0597838223698469, -0.103049279618523, -0.0769047640003321, 
-0.118149147340544, -0.206442888023609, -0.198459817750465, -0.18152832001722, 
-0.165383661843024, -0.0957262882507554, -0.0864688802203385, 
-0.0630338535134446, -0.03435044632752, -0.0154821499218539, 
-0.0314268928118927, -0.025983717799717, 0.0282929694353902, 
0.0232653224268324, 0.044754397468965, 0.0622670133089944, 0.0727198768351187, 
0.0466223060669055, 0.0328019906182384, -0.000948104259839933, 
-0.031106224386742, 0.00281978585657927, -0.0270619826610582, 
-0.118153806197, -0.0871157825225317, -0.110129801132847, -0.137825961886963, 
-0.0920659263392154, -0.0374774889057388, -0.00745627398767124, 
0.002470322954361, 0.0651661174076275, 0.00813534165627083, 0.00292734637319447, 
0.0181390199147207, 0.00856956883202309, 0.0970875415883386, 
0.0369958398875549, 0.0618046279711699, 0.109397114073851, 0.0449035706955121, 
0.0481129032277237, 0.0778904767665167, 0.086210643712209, 0.0541780686697457, 
0.0861732414606733, 0.102135223907732, 0.0641783106433247, 0.146233540870658, 
0.131671380938121, 0.127172415036263, 0.0789397210303182, 0.0862353397052738, 
0.133652755398062, 0.120768633688736, 0.11135215349646, 0.131511411531401, 
0.0715666685389633, 0.0193245415746055, 0.0240078731787876, 0.0112420569614065, 
-0.0257257109092351, 0.0769197455342581, 0.162002286382954, 0.133137970827076, 
0.116069109578918, -0.0822301552332654, -0.199748679386703, -0.258286596156111, 
-0.287847641877563, -0.163890896017824, -0.0555361669325279, 
0.0441609818172009, 0.0543262395888311, 0.0986712127242062, 0.189644442456989, 
0.186942873839697, 0.15353035503979)), .Names = c("C", "A", "Y", 
"U"), row.names = c(NA, -92L), class = c("tbl_df", "tbl", "data.frame"
))
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
AllDoe_1
  • 29
  • 4
  • I'm not able to reproduce the issue. Please provide the rest of the data using `dput` (see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Julius Vainora Apr 02 '18 at 13:50
  • `dput(head(mydata)) structure(list(date = c("1995.1Q", "1995.2Q", "1995.3Q", "1995.4Q", "1996.1Q", "1996.2Q"), C = c(NA, NA, NA, NA, 9.76192185331763, 7.01543928393462), A = c(NA, -5.20153912216881, -5.07924451118444, -2.09333037295979, 3.74516675987993, 5.87755978365845), Y = c(NA, NA, NA, NA, 173.354360114913, 178), U = c(0.0243894451488191, 0.0350753527090906, -0.00285310486725987, -0.044253379024236, -0.0386027161344519, -0.0713350700777299)), .Names = c("date", "C", "A", "Y", "U"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))` Hope that works for you. – AllDoe_1 Apr 02 '18 at 14:19
  • The data should be added to the question by clicking "edit", and it is still not complete. Also, now I get an error but a different one than yours. Your have to provide a **reproducible** example of your issue. That is, we must get the same errors as you do, nothing else. – Julius Vainora Apr 02 '18 at 14:33
  • see edited post. – AllDoe_1 Apr 02 '18 at 15:10

1 Answers1

1

Yes, you are right that it's the missing values what causes the problem. Apparently ecm omits them in y, xeq, and xtr separately, which causes problems in this case since the number of missing values varies across those variables.

As to fix the issue, you should get rid of the rows with any missing observations in mydata before assigning values to xeq and xtr:

mydata <- na.omit(mydata)
xeq<-mydata[c('A','Y')]
xtr<- mydata['U']
xeq<-as.data.frame(xeq)
xtr<-as.data.frame(xtr)
(ecm1 <- ecm(mydata$C, xeq, xtr, includeIntercept = TRUE))
#
# Call:
# lm(formula = dy ~ ., data = x)
#
# Coefficients:
# (Intercept)       deltaU        ALag1        YLag1        yLag1  
#  -3.141e-01    1.385e+00    7.359e-02    6.729e-08   -9.557e-03  

Also, as expected, doing that does not affect the fact the the time series has regular intervals since all the missing observations are at the end and the beginning of mydata.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102