[R] plm Issues

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jul 13 16:27:25 CEST 2009


The dyn and dynlm packages can handle time series in lm and glm.
(dyn can also handle many additional regression functions as well)
In the case of dyn just write dyn$lm instead of lm like this:

> library(dyn)
> a <- ts(c(1, 2, 4))
> dyn$lm(a ~ diff(a))

Call:
lm(formula = dyn(a ~ diff(a)))

Coefficients:
(Intercept)      diff(a)
          0            2


On Mon, Jul 13, 2009 at 10:17 AM, Damien Moore<damienlmoore at gmail.com> wrote:
> Duh! Thanks and good advice. I was using 2.7.2 because it was, until
> recently, the latest version working with RPy (http://rpy.sourceforge.net/).
> Also didn't realize plm was still actively developed.
>
> Interesting that since plm now correctly handles diff and lag operations, it
> actually breaks with the behavior of "lm":
>
>> a=ts(c(1,2,4))
>> lm(a~diff(a))
> Error in model.frame.default(formula = a ~ diff(a), drop.unused.levels =
> TRUE) :
>  variable lengths differ (found for 'diff(a)')
>
> To regress a on its difference, one needs the more laborious:
>> a=ts(c(1,2,4))
>> adata=as.data.frame(cbind(a,diff(a)))
>> colnames(adata)=c("a","diffa")
>> lm(a~diffa,data=adata)
>
> Call:
> lm(formula = a ~ diffa, data = adata)
>
> Coefficients:
> (Intercept)        diffa
>          0            2
>
> >From the R help
> Fitting Linear ModelsUsing time series
>
> Considerable care is needed when using lm with time series.
>
> Unless na.action = NULL, the time series attributes are stripped from the
> variables before the regression is done. (This is necessary as omitting NAs
> would invalidate the time series attributes, and if NAs are omitted in the
> middle of the series the result would no longer be a regular time series.)
>
> Even if the time series attributes are retained, they are not used to line
> up series, so that the time shift of a lagged or differenced regressor would
> be ignored. It is good practice to prepare a data argument by
> ts.intersect<ts.union.html>(...,
> dframe = TRUE), then apply a suitable na.action to that data frame and call
> lm with na.action = NULL so that residuals and fitted values are time
> series.
>
>
> On Sat, Jul 11, 2009 at 10:53 PM, milton ruser <milton.ruser at gmail.com>wrote:
>
>> The first think one need to do when has a so old version, is update it :-)
>> After, if the problem remain, try get help with the colleagues.
>>
>> best
>>
>> milton
>>
>> On Thu, Jul 9, 2009 at 10:58 AM, Damien Moore <damienlmoore at gmail.com>wrote:
>>
>>> Hi List
>>>
>>> I'm having difficulty understanding how plm should work with dynamic
>>> formulas. See the commands and output below on a standard data set. Notice
>>> that the first summary(plm(...)) call returns the same result as the
>>> second
>>> (it shouldn't if it actually uses the lagged variable requested). The
>>> third
>>> call results in error (trying to use diff'ed variable in regression)
>>>
>>> Other info: I'm running R 2.7.2 on WinXP
>>>
>>> cheers
>>>
>>>
>>>
>>> *>data("Gasoline",package="Ecdat")
>>> >Gasoline_plm<-plm.data(Gasoline,c("country","year"))
>>> >pdim(Gasoline_plm)
>>> **Balanced Panel: n=18, T=19, N=342
>>> *
>>> *>summary(plm(lgaspcar~lincomep,data=Gasoline_plm**))
>>> **Oneway (individual) effect Within Model
>>>
>>> Call:
>>> plm(formula = lgaspcar ~ lincomep, data = Gasoline_plm)
>>>
>>> Balanced Panel: n=18, T=19, N=342
>>>
>>> Residuals :
>>>    Min.  1st Qu.   Median  3rd Qu.     Max.
>>> -0.40100 -0.08410 -0.00858  0.08770  0.73400
>>>
>>> Coefficients :
>>>         Estimate Std. Error t-value  Pr(>|t|)
>>> lincomep -0.76183    0.03535 -21.551 < 2.2e-16 ***
>>> ---
>>> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>>>
>>> Total Sum of Squares: 17.061
>>> Residual Sum of Squares: 6.9981
>>> Multiple R-Squared: 0.58981
>>> F-statistic: 464.442 on 323 and 1 DF, p-value: 0.036981
>>>
>>> **> summary(plm(lgaspcar~lag(lincomep),data=Gasoline_plm))
>>> **Oneway (individual) effect Within Model
>>>
>>> Call:
>>> plm(formula = lgaspcar ~ lag(lincomep), data = Gasoline_plm)
>>>
>>> Balanced Panel: n=18, T=19, N=342
>>>
>>> Residuals :
>>>    Min.  1st Qu.   Median  3rd Qu.     Max.
>>> -0.40100 -0.08410 -0.00858  0.08770  0.73400
>>>
>>> Coefficients :
>>>              Estimate Std. Error t-value  Pr(>|t|)
>>> lag(lincomep) -0.76183    0.03535 -21.551 < 2.2e-16 ***
>>> ---
>>> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>>>
>>> Total Sum of Squares: 17.061
>>> Residual Sum of Squares: 6.9981
>>> Multiple R-Squared: 0.58981
>>> F-statistic: 464.442 on 323 and 1 DF, p-value: 0.036981
>>>
>>> *
>>> *>summary(plm(lgaspcar~diff(lincomep),data=Gasoline_plm))*
>>> *Error in model.frame.default(formula = lgaspcar ~ diff(lincomep), data =
>>> mydata,  :
>>>  variable lengths differ (found for 'diff(lincomep)')
>>> *
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>>
>>> ______________________________________________
>>> 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<http://www.r-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>




More information about the R-help mailing list