Hi, I was wondering why the results from lm and dynlm are not the same for what I think is the same model. I have just modified example 4.2 from the Pfaff book, please see below for the code and results. Can anyone tell my what I am doing wrongly? Many thanks, Werner set.seed(123456) e1 <- rnorm(100) e2 <- rnorm(100) y1 <- ts(cumsum(e1)) y2 <- ts(0.6*y1 + e2) lr.reg <- lm(y2 ~ y1) error <- ts(residuals(lr.reg)) error.lagged <- error[-c(99, 100)] dy1 <- diff(y1) dy2 <- diff(y2) diff.dat <- data.frame(embed(cbind(dy1, dy2), 2)) colnames(diff.dat) <- c('dy1', 'dy2', 'dy1.1', 'dy2.1') ecm.reg <- lm(dy2 ~ error.lagged + dy1.1 + dy2.1, data=diff.dat) ecm.dynreg <- dynlm(d(y2) ~ L(error) + L(d(y1),1) + L(d(y2),1)) summary(ecm.reg) summary(ecm.dynreg)> summary(ecm.reg)Call: lm(formula = dy2 ~ error.lagged + dy1.1 + dy2.1, data = diff.dat) Residuals: Min 1Q Median 3Q Max -2.9588 -0.5439 0.1370 0.7114 2.3065 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.003398 0.103611 0.033 0.974 error.lagged -0.968796 0.158554 -6.110 2.24e-08 *** dy1.1 0.808633 0.112042 7.217 1.35e-10 *** dy2.1 -1.058913 0.108375 -9.771 5.64e-16 *** --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 Residual standard error: 1.026 on 94 degrees of freedom Multiple R-Squared: 0.5464, Adjusted R-squared: 0.5319 F-statistic: 37.74 on 3 and 94 DF, p-value: 4.243e-16> summary(ecm.dynreg)Time series regression with "ts" data: Start = 3, End = 100 Call: dynlm(formula = d(y2) ~ L(error) + L(d(y1), 1) + L(d(y2), 1)) Residuals: Min 1Q Median 3Q Max -2.9588 -0.5439 0.1370 0.7114 2.3065 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.003398 0.103611 0.033 0.9739 L(error) -0.968796 0.158554 -6.110 2.24e-08 *** L(d(y1), 1) 0.245649 0.126996 1.934 0.0561 . L(d(y2), 1) -0.090117 0.105938 -0.851 0.3971 --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 Residual standard error: 1.026 on 94 degrees of freedom Multiple R-Squared: 0.5464, Adjusted R-squared: 0.5319 F-statistic: 37.74 on 3 and 94 DF, p-value: 4.243e-16>
On Wed, 15 Oct 2008, Werner Wernersen wrote:> Hi,> I was wondering why the results from lm and dynlm are not the same for > what I think is the same model....because it's not the same model :-) I haven't looked at this in detail, but:> set.seed(123456) > e1 <- rnorm(100) > e2 <- rnorm(100) > y1 <- ts(cumsum(e1)) > y2 <- ts(0.6*y1 + e2) > lr.reg <- lm(y2 ~ y1) > error <- ts(residuals(lr.reg)) > error.lagged <- error[-c(99, 100)]This corresponds to a lag of two, hence you need> dy1 <- diff(y1) > dy2 <- diff(y2) > diff.dat <- data.frame(embed(cbind(dy1, dy2), 2)) > colnames(diff.dat) <- c('dy1', 'dy2', 'dy1.1', 'dy2.1') > ecm.reg <- lm(dy2 ~ error.lagged + dy1.1 + dy2.1, > data=diff.dat) > ecm.dynreg <- dynlm(d(y2) ~ L(error) + L(d(y1),1) + L(d(y2),1))^^^^^^^^ a different lag here. ecm.dynreg <- dynlm(d(y2) ~ L(error, 2) + L(d(y1),1) + L(d(y2),1)) gives the same results as ecm.reg. (Although looking at this briefly, I suspect that the specification of ecm.reg should be changed rather than the other way round.) hth, Z
Pfaff, Bernhard Dr.
2008-Oct-16 08:58 UTC
[R] dynlm and lm: should they give same estimates?
Hello Werner, this is easily clarified. The code in my book contains an error: please replace the line: error.lagged <- error[-c(99, 100)] with error.lagged <- error[-c(1, 100)] I will file this in the errata section on my web-site and will correct the relevant example in the urca and vars packages for their next releases. Best, Bernhard> >Hi, > >I was wondering why the results from lm and dynlm are not the >same for what I think is the same model. >I have just modified example 4.2 from the Pfaff book, please >see below for the code and results. > >Can anyone tell my what I am doing wrongly? > >Many thanks, > Werner > >set.seed(123456) >e1 <- rnorm(100) >e2 <- rnorm(100) >y1 <- ts(cumsum(e1)) >y2 <- ts(0.6*y1 + e2) >lr.reg <- lm(y2 ~ y1) >error <- ts(residuals(lr.reg)) >error.lagged <- error[-c(99, 100)] > >dy1 <- diff(y1) >dy2 <- diff(y2) >diff.dat <- data.frame(embed(cbind(dy1, dy2), 2)) >colnames(diff.dat) <- c('dy1', 'dy2', 'dy1.1', 'dy2.1') >ecm.reg <- lm(dy2 ~ error.lagged + dy1.1 + dy2.1, > data=diff.dat) >ecm.dynreg <- dynlm(d(y2) ~ L(error) + L(d(y1),1) + L(d(y2),1)) >summary(ecm.reg) >summary(ecm.dynreg) > >> summary(ecm.reg) > >Call: >lm(formula = dy2 ~ error.lagged + dy1.1 + dy2.1, data = diff.dat) > >Residuals: > Min 1Q Median 3Q Max >-2.9588 -0.5439 0.1370 0.7114 2.3065 > >Coefficients: > Estimate Std. Error t value Pr(>|t|) >(Intercept) 0.003398 0.103611 0.033 0.974 >error.lagged -0.968796 0.158554 -6.110 2.24e-08 *** >dy1.1 0.808633 0.112042 7.217 1.35e-10 *** >dy2.1 -1.058913 0.108375 -9.771 5.64e-16 *** >--- >Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > >Residual standard error: 1.026 on 94 degrees of freedom >Multiple R-Squared: 0.5464, Adjusted R-squared: 0.5319 >F-statistic: 37.74 on 3 and 94 DF, p-value: 4.243e-16 > >> summary(ecm.dynreg) > >Time series regression with "ts" data: >Start = 3, End = 100 > >Call: >dynlm(formula = d(y2) ~ L(error) + L(d(y1), 1) + L(d(y2), 1)) > >Residuals: > Min 1Q Median 3Q Max >-2.9588 -0.5439 0.1370 0.7114 2.3065 > >Coefficients: > Estimate Std. Error t value Pr(>|t|) >(Intercept) 0.003398 0.103611 0.033 0.9739 >L(error) -0.968796 0.158554 -6.110 2.24e-08 *** >L(d(y1), 1) 0.245649 0.126996 1.934 0.0561 . >L(d(y2), 1) -0.090117 0.105938 -0.851 0.3971 >--- >Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 > >Residual standard error: 1.026 on 94 degrees of freedom >Multiple R-Squared: 0.5464, Adjusted R-squared: 0.5319 >F-statistic: 37.74 on 3 and 94 DF, p-value: 4.243e-16 > >> > > > > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. >***************************************************************** Confidentiality Note: The information contained in this ...{{dropped:10}}