I would like to loop over a dataframe data_all_long
in long format that has time-series tuples identified by a stockID
.
With unique()
I stored all the different IDs in the data frame "uniqueIDs".
"Numberofrows" is the length of the list containing the time series tuples from above.
Ideally the tmp variable should store all the data for one specific ID out of the long list temporarily to calculate the regression for one specific ID and store it into a vector. The overall outcome should be a vector with all the regression coefficients for the different IDs.
for(i in uniqueIDs){
for(j in 1:numberofrows){
tmp <- rbind(tmp,filter(data_all_long, stockId == i))
}
beta[,i] <- lm(mrf ~ stockreturn, data = tmp)
}
Does anyone here have any ideas?