R - plm regression with time in posix-format

0 votes

I have little experience with panel data in R, and am trying to run a simple panel regression with the plm-package. When converting my dataframe to a pdata.frame, however, my time index-variable is transformed to a factor variable. This means that if I want to regress a dependent variable as a function of time, the regression generates a long list of dummy-variables for time and calculates individual coefficients for each. I just want the average effect per time unit (ie. average monthly increase/decrease in points).

Example dataframe:

ID    Date        Points
1     1/11/2014   2
1     1/12/2014   4
1     1/1/2015    6
1     1/2/2015    8
2     1/11/2014   1
2     1/12/2014   2
2     1/1/2015    3
2     1/2/2015    4

Say the example dataframe structure is ID = int, Date = POSIXct, Points = int. I then convert it to a pdata.frame with index ID and Date:

panel <- pdata.frame(dataframe, c("ID", "Date"))

And run a plm fixed effects regression:

fixed <- plm(Points ~ Date, data=panel, model="within")
summary(fixed)

The resulting coefficients are then broken down by each month as dummies. I want to treat my time-variable as a continuous variable, so I get only one coefficient for Date. How can I do this? Is there a way to avoid formatting the time index-variable as a factor in panel dataframes?

Mar 26, 2022 in Machine Learning by Dev
• 6,000 points
598 views

1 answer to this question.

0 votes

To use in your model, I believe you should establish a distinct clock or time counter from panel$Date. Consider the following scenario:

library(dplyr)
dataframe <- dataframe %>%
    group_by(ID) %>%
    mutate(clock = seq_along(ID))
panel <- pdata.frame(dataframe, c("ID", "Date"))

That gives the this data:

             ID       Date Points clock
1-2014-11-01  1 2014-11-01      2     1
1-2014-12-01  1 2014-12-01      4     2
1-2015-01-01  1 2015-01-01      6     3
1-2015-02-01  1 2015-02-01      8     4
2-2014-11-01  2 2014-11-01      1     1
2-2014-12-01  2 2014-12-01      2     2
2-2015-01-01  2 2015-01-01      3     3
2-2015-02-01  2 2015-02-01      4     4

The output is:

fixed <- plm(Points ~ clock, data=panel, model="within")
summary(fixed)
Oneway (individual) effect Within Model

Call:
plm(formula = points ~ clock, data = panel, model = "within")

Balanced Panel: n=2, T=4, N=8

Residuals :
   Min. 1st Qu.  Median 3rd Qu.    Max. 
 -0.750  -0.375   0.000   0.375   0.750 

Coefficients :
      Estimate Std. Error t-value Pr(>|t|)   
clock  1.50000    0.22361  6.7082 0.001114 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    25
Residual Sum of Squares: 2.5
R-Squared      :  0.9 
      Adj. R-Squared :  0.5625 
F-statistic: 45 on 1 and 5 DF, p-value: 0.0011144
answered Apr 4, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer

Plotting logistic regression in R with the Smarket dataset

The first, third, and fourth methods of ...READ MORE

answered Apr 12, 2022 in Machine Learning by Dev
• 6,000 points
999 views
0 votes
1 answer

Can we change the sigmoid with tanh in Logistic regression transforms??

Hi@Deepanshu, Yes, you can use tanh instead of ...READ MORE

answered May 12, 2020 in Machine Learning by MD
• 95,460 points
2,730 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,044 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,213 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,905 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,146 views
0 votes
1 answer
0 votes
1 answer

Plot logistic regression curve in R

The Code looks something like this: fit = ...READ MORE

answered Apr 4, 2022 in Machine Learning by Nandini
• 5,480 points
2,425 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP