Regression with plm

The code below runs the time series regression "return_fund ~ return market" with biglm, the function lm however didn't work.

Now I would like to switch to plm to account for fixed effects. Unfortunately plm doesn't work, like lm before.

library(biglm)
library(plm)

union_with_factors = data.table(
  t = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
  excessr  = c(10,23,13,53,43,76,34,12,45,13,42,31,4,53,64),
  FundId = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
  mkt_rf = c(1,1,2,1,3,1,1,2,1,3,1,1,2,1,3)
)

sp <- split(union_with_factors, union_with_factors$FundId)
beta <- sapply(sp, function(tmp){
  fit <- plm(excessr ~ mkt_rf, data = tmp)
  coef(fit)
})```
Does anyone know what I could change that plm work ? Thanks in advance!

I am afraid you have to be a little more specific here. Ideally, provide a reproducible example so we have get an idea of what is not working.

Yes sorry! I added one.

Can you make your dataset reproducible, like in the article?

Isn't the mock code I've written a repex? My original dataset would be too big to copy it in here.

No. Have another look at the article. We are after the code behind your tibble. Not the output from it.

It just makes it easier for others to help you if you make it reproducible.

Actually the table I have created is how my data set looks like. Thats my input not the output. I would like to run the regression "excessr ~ mktrf".
Or have I understood sth. fundamentally wrong? Sorry for the confusion

Did you read the article?

What is the table that you have pasted in there? Do you expect someone to copy and paste it somehow in to R? You try it from your post. It won't work, that is why it needs to be reproducible. It makes it easier for other people to help you. You probably would have got a good answer hours ago.

Also, what libraries have you loaded? What is your first dataset meant to be?

Ok definitely my fault! Now it should be copyable.

1 Like

You were almost there in terms of the reprex. They do take some time getting the hang off. It is, however, much easier to help if you have all the libraries there. Your reprex should have looked like this. Note, I changed your model to lm and that works fine. The output in beta is not the prettiest, but you get all the results from the three FundId values.

library(biglm)
#> Loading required package: DBI
library(plm)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following object is masked from 'package:plm':
#> 
#>     between

union_with_factors = data.table(
  t = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
  excessr  = c(10,23,13,53,43,76,34,12,45,13,42,31,4,53,64),
  FundId = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
  mkt_rf = c(1,1,2,1,3,1,1,2,1,3,1,1,2,1,3)
)

sp <- split(union_with_factors, union_with_factors$FundId)
beta <- sapply(sp, function(tmp){
  fit <- lm(excessr ~ mkt_rf, data = tmp)
  coef(fit)
})

Created on 2020-03-19 by the reprex package (v0.2.1)

If you want fixed effects over FundId you cannot loop over it using sapply and you need a factor or an id number for plm to work. The following does the trick and produces a result.

library(biglm)
#> Loading required package: DBI
library(plm)
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following object is masked from 'package:plm':
#> 
#>     between

union_with_factors = data.table(
  t = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5),
  excessr  = c(10,23,13,53,43,76,34,12,45,13,42,31,4,53,64),
  FundId = c("x","x","x","x","x","y","y","y","y","y","z","z","z","z","z"),
  mkt_rf = c(1,1,2,1,3,1,1,2,1,3,1,1,2,1,3)
)

sp <- split(union_with_factors, union_with_factors$FundId)
beta <- sapply(sp, function(tmp){
  fit <- lm(excessr ~ mkt_rf, data = tmp)
  coef(fit)
})

union_with_factors$my_fund <- factor(union_with_factors$FundId)
plm(excessr ~ mkt_rf, data = union_with_factors, model = "within", index = c("my_fund"))
#> 
#> Model Formula: excessr ~ mkt_rf
#> 
#> Coefficients:
#>  mkt_rf 
#> -4.2292

Created on 2020-03-19 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.