statistics - Running many multiple OLS regressions at once in R -
i want run time series regressions (fama-french 3 factor new factor). have following tables.
table01 date port_01 port_02 --------- port_18 01/1965 0.85 0.97 1.86 02/1965 8.96 7.2 0.98 03/1965 8.98 7.9 8.86 table 02 date market smb hml wxo 01/1965 0.85 0.97 0.86 0.87 02/1965 8.96 7.2 0.98 0.79 03/1965 8.98 7.9 8.86 0.86
i have run 18 regressions , store intercepts in vector. this
port_1=inter(1)+beta1(market)+beta2(smb)+beta3(hml)+beta3(wxo)+e port_2=inter(2)+beta1(market)+beta2(smb)+beta3(hml)+beta3(wxo)+e port_18=inter(18)+beta1(market)+beta2(smb)+beta3(hml)+beta3(wxo)+e
i want these 18 intercepts stored in vector. can individually. if there way coding me lot of time.
consider vapply()
, variant of lapply()
allows specification of output here being atomic numeric vector (length of 1). however, first, need merge tables date
field , create list of port formulas (assuming that's needed underlying data). below runs linear regression, lm
, adjust actual model might require adjusting intercept extraction:
data <- merge(table_01, table_02, by="date") ports <- colnames(table_01)[2:ncol(table_01)] formulas <- paste(ports, "~ market + smb + hml + wxo") intercepts <- vapply(formulas, function(i) { output <- lm(i, data) coef(output)["(intercept)"] }, numeric(1))
Comments
Post a Comment