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)