Search code examples
rvariableslinear-regressionlm

How to express a variable on a different data frame in a linear regression, lm()


I have a problem in R with the expression of the linear regression.

When I enter the function with "hard values" it works perfectly fine: LM <- lm(`589499102` ~ `Value-Weighted Return`, data = strategic_return, subset = 3:255)

Now I would like to exchange the hard value of 589499102 through a variable that is on another sheet. I tried several different ways like the following but none of them worked LM <- lm(OtherSheet$Col1[1] ~ `Value-Weighted Return`, data = strategic_return, subset = 3:255) or LM <- lm(`OtherSheet$Col1[1]` ~ `Value-Weighted Return`, data = strategic_return, subset = 3:255)

It even does not work, when I create a variable: newvariable <- "589499102" LM <- lm(`newvariable` ~ `Value-Weighted Return`, data = strategic_return, subset = 3:255)

The goal, in the end, would be to integrate this into a for loop. However, this will be a smaller problem when this issue is solved.

This is how the data looks at the moment:

Sheet 1: strategic_dateandcusip. This sheet actually gives the information for the regression itself. Which column needs to be regressed over which row.

strategic_cusip announcement_date ... ... ... row_start_regression row_end_regression
589499102 2010-02-21 ... ... ... 3 255
705573103 2010-03-14 ... ... ... 18 270
46069S109 2010-03-21 ... ... ... 23 275
... ... ... ... ... ... ...

Sheet 2: strategic_return . This sheet contains the "raw data" over which the regression is actually done.

Date 002824100 00430U103 589499102 ... ... Value-Weighted Return
2009-01-05 -0.01717700 0.0022422 -0.038760 ... ... 0.016764
2009-01-06 -0.03267477 -0.0044743 0.016129 ... ... 0.033647
2009-01-07 -0.00549882 -0.1146067 0.031746 ... ... -0.022271
2009-01-08 0.01105845 0.0329949 0.0076923 ... ... 0.011896
2009-01-09 -0.00058594 -0.0491400 0.046512 ... ... -0.018748
2009-01-12 -0.02169240 -0.0051680 -0.022222 ... ... -0.026589
... ... ... ... ... ... ...
... ... ... ... ... ... ...
2020-12-31 0.03292349 -0.0031726 -0.1029381 ... ... 0.0213947

The goal is to regress each column of sheet strategic_return (002824100, 00430U103, 589499102) with the column "Value-Weighted Return" over the time period which is noted in the sheet strategic_dateandcusip$row_start_regression to strategic_dateandcusip$row_end_regression.

The field in the first sheet strategic_dateandcusip[1,1] is the same as the header of the fourth column of the second sheet strategic_return = 589499102. And I tried to link these sheets like that.

I have dput() the illustrated tables above. I am sorry for the long codes. I really tried to minimize it to the necessary rows and columns only.

Sheet 1: strategic_dateandcusip:

structure(list(strategic_cusip = structure(2:1, .Label = c("46069S109", 
"589499102"), class = "factor"), strategic_cusip_new = c("589499102", 
"46069S109"), strategic_date = structure(c(14661, 14689), class = "Date"), 
    strategic_dateweekday = structure(c(14662, 14690), class = "Date"), 
    row_of_eventdate = c(286, 306), row_start_regression = c(3, 
    23), row_end_regression = c(255, 275)), row.names = c(1L, 
3L), class = "data.frame")


Sheet 2: strategic_return 

structure(list(Date = structure(c(14246, 14249, 14250, 14251, 
14252, 14253, 14256, 14257, 14258, 14259, 14260, 14264, 14265, 
14266, 14267, 14270, 14271, 14272, 14273, 14274, 14277, 14278, 
14279, 14280, 14281, 14284, 14285, 14286, 14287, 14288, 14292, 
14293, 14294, 14295, 14298, 14299, 14300, 14301, 14302, 14305, 
14306, 14307, 14308, 14309, 14312, 14313, 14314, 14315, 14316, 
14319, 14320, 14321, 14322, 14323, 14326, 14327, 14328, 14329, 
14330, 14333, 14334, 14335, 14336, 14337, 14340, 14341, 14342, 
14343, 14347, 14348, 14349, 14350, 14351, 14354, 14355, 14356, 
14357, 14358, 14361, 14362, 14363, 14364, 14365, 14368, 14369, 
14370, 14371, 14372, 14375, 14376, 14377, 14378, 14379, 14382, 
14383, 14384, 14385, 14386, 14390, 14391, 14392, 14393, 14396, 
14397, 14398, 14399, 14400, 14403, 14404, 14405, 14406, 14407, 
14410, 14411, 14412, 14413, 14414, 14417, 14418, 14419, 14420, 
14421, 14424, 14425, 14426, 14427, 14431, 14432, 14433, 14434, 
14435, 14438, 14439, 14440, 14441, 14442, 14445, 14446, 14447, 
14448, 14449, 14452, 14453, 14454, 14455, 14456, 14459, 14460, 
14461, 14462, 14463, 14466, 14467, 14468, 14469, 14470, 14473, 
14474, 14475, 14476, 14477, 14480, 14481, 14482, 14483, 14484, 
14487, 14488, 14489, 14490, 14491, 14495, 14496, 14497, 14498, 
14501, 14502, 14503, 14504, 14505, 14508, 14509, 14510, 14511, 
14512, 14515, 14516, 14517, 14518, 14519, 14522, 14523, 14524, 
14525, 14526, 14529, 14530, 14531, 14532, 14533, 14536, 14537, 
14538, 14539, 14540, 14543, 14544, 14545, 14546, 14547, 14550, 
14551, 14552, 14553, 14554, 14557, 14558, 14559, 14560, 14561, 
14564, 14565, 14566, 14567, 14568, 14571, 14572, 14573, 14575, 
14578, 14579, 14580, 14581, 14582, 14585, 14586, 14587, 14588, 
14589, 14592, 14593, 14594, 14595, 14596, 14599, 14600, 14601, 
14602, 14606, 14607, 14608, 14609, 14613, 14614, 14615, 14616, 
14617, 14620, 14621, 14622, 14623, 14624, 14628, 14629, 14630, 
14631, 14634, 14635, 14636, 14637, 14638, 14641, 14642, 14643, 
14644, 14645, 14648, 14649, 14650, 14651, 14652, 14656, 14657, 
14658, 14659, 14662, 14663, 14664, 14665, 14666, 14669, 14670, 
14671, 14672, 14673, 14676, 14677, 14678, 14679, 14680, 14683, 
14684, 14685, 14686, 14687, 14690, 14691, 14692, 14693, 14694, 
14697, 14698, 14699), class = "Date"), `46069S109` = c(NA, -0.0161453077699294, 
0.0451282051282051, -0.0441609421000981, -0.00924024640657083, 
-0.0300518134715027, -0.0128205128205127, 0.0183982683982684, 
-0.0478214665249734, -0.00334821428571441, 0.0615901455767078, 
-0.0748945147679326, 0.0273660205245154, -0.0310765815760266, 
0.0412371134020618, 0.0231023102310232, 0.0462365591397849, 0.0441932168550873, 
-0.0738188976377953, -0.0106269925611052, 0.0236305048335122, 
0.00734522560335785, 0.0114583333333335, 0.0895983522142121, 
0.0396975425330813, -0.0136363636363637, -0.0433179723502303, 
0.0269749518304431, 0.0196998123827391, 0.0423183072677094, -0.0714916151809356, 
-0.0123574144486691, -0.0673724735322426, 0.00309597523219826, 
-0.0277777777777779, 0.0433862433862434, 0.039553752535497, -0.00682926829268295, 
-0.00687622789783893, -0.0484668644906034, 0.0135135135135136, 
0.0400000000000001, -0.0128205128205129, -0.00199800199800196, 
-0.00800800800800801, 0.0938446014127144, 0.0535055350553506, 
0.0551663747810859, -0.012448132780083, -0.0596638655462186, 
0.0446827524575514, 0.0325064157399487, -0.0124275062137531, 
-0.0478187919463087, 0.0616740088105728, -0.0240663900414939, 
-0.0297619047619047, 0.0359333917616126, -0.00676818950930627, 
-0.0340715502555367, 0.0141093474426808, 0.0452173913043478, 
0.0482529118136439, 0.0341269841269841, -0.000767459708365295, 
-0.0291858678955452, 0.0300632911392404, 0.0537634408602151, 
-0.0160349854227406, -0.0162962962962963, -0.0376506024096386, 
0.0344287949921754, -0.00756429652042371, -0.0282012195121951, 
-0.032156862745098, 0.0210696920583468, -0.0753968253968253, 
0.0266094420600859, -0.0175585284280937, 0, -0.000851063829787216, 
-0.0119250425894379, 0.0129310344827587, 0.0561702127659575, 
0.0370668815471393, -0.00932400932400926, -0.0894117647058824, 
-0.0189491817398793, 0.0500438981562775, -0.0418060200668896, 
-0.0261780104712042, 0.0331541218637992, -0.00954032957502163, 
0.0253940455341507, 0.0136635354397949, 0.0269587194608256, -0.029532403609516, 
0.00929839391377848, 0.0209380234505863, 0.014766201804758, -0.000808407437348406, 
-0.00889967637540449, 0.0612244897959184, -0.0523076923076923, 
-0.0219155844155844, 0.0232365145228215, -0.0121654501216545, 
-0.00410509031198678, 0.0643033800494641, 0.0224632068164213, 
0.0106060606060607, -0.0239880059970015, -0.021505376344086, 
-0.0243328100470958, 0.00804505229283987, -0.0167597765363128, 
0.0194805194805195, -0.0334394904458599, 0.0172981878088961, 
0.00242914979757094, 0.0290791599353796, 0.0125588697017269, 
-0.0116279069767442, -0.0141176470588235, 0.00636435958631663, 
-0.0284584980237155, -0.0227827502034174, -0.0166527893422148, 
-0.00677392040643523, 0.0213128729752771, -0.00584307178631054, 
0.0411418975650714, 0.0169354838709677, 0.0721649484536083, 0.0273668639053255, 
0, 0.0165586753059754, -0.0212464589235127, 0.0332850940665701, 
-0.0378151260504201, -0.00291120815138289, 0.0189781021897811, 
0.0100286532951289, -0.0113475177304965, 0.0121951219512195, 
0.0184266477675407, 0.0020876826722339, -0.00624999999999999, 
-0.0146750524109015, 0.00425531914893621, -0.0112994350282486, 
-0.0214285714285715, -0.0124087591240876, 0.0184774575018477, 
0.0275761973875182, -0.0261299435028249, -0.0246555474981871, 
0.0200743494423793, -0.000728862973760917, 0.0138584974471189, 
0.00791366906474816, -0.0121341898643826, 0.0130057803468208, 
0.00570613409415122, 0.00283687943262418, 0.0544554455445544, 
-0.00804828973843065, 0.0135226504394862, 0.00933955970647102, 
0.0264375413086582, 0.0148100450740503, 0.0139593908629442, 0.00813516896120144, 
0.0384854127870888, -0.0346682606096833, -0.00866873065015462, 
0.0143660212367268, -0.0104679802955664, -0.0286247666459241, 
0.0153747597693786, 0.00126182965299682, -0.00378071833648385, 
0.00632511068943704, -0.0251414204902577, -0.0174081237911025, 
0.0137795275590551, -0.0187702265372168, 0.0098944591029024, 
-0.0718484650555192, -0.0225193525686137, 0.0129589632829373, 
0.0106609808102346, -0.00773558368495086, -0.00992204110559878, 
0.0257695060844667, -0.000697836706210732, 0.00628491620111731, 
0.0263705759888965, -0.0344827586206896, -0.0133053221288515, 
-0.00709723207948897, -0.00214438884917794, -0.00429799426934101, 
-0.0122302158273381, -0.023306627822287, -0.00372856077554069, 
-0.0142215568862275, -0.0258162490508732, 0.0171473109898675, 
-0.0383141762452107, -0.00796812749003995, 0.0224899598393575, 
0.0109976433621366, 0.0334110334110335, -0.0165413533834587, 
0.032874617737003, -0.00518134715025909, 0.0156250000000001, 
-0.0256410256410256, 0.0105263157894736, 0.0238095238095238, 
-0.0174418604651163, -0.00887573964497036, -0.0402985074626866, 
-0.0139968895800933, 0.00236593059936917, 0.022816679779701, 
0.00923076923076917, -0.0152439024390243, 0, 0.0510835913312694, 
0.0382916053019145, 0.0113475177304965, 0.0140252454417953, 0.00276625172890727, 
-0.0103448275862069, -0.0111498257839721, -0.0169133192389006, 
0.0064516129032258, 0.00427350427350431, -0.00354609929078007, 
0.0213523131672597, -0.0167247386759582, 0.00425230333097098, 
0.0374029640084685, 0.0102040816326531, -0.00134680134680132, 
0.00741739716790286, -0.00870147255689418, 0, 0.0209318028359216, 
0.0145502645502646, -0.014993481095176, -0.00264725347452013, 
-0.026542800265428, -0.00477164280845264, -0.00410958904109592, 
-0.00687757909215954, -0.0186980609418282, 0.00705716302046575, 
0.00560616678346181, -0.0174216027874564, 0.000709219858156013, 
0.00496102055279945, -0.00423131170662909, -0.0325779036827195, 
0.0175695461200586, -0.00215827338129505, 0.0259552992069215, 
-0.0189739985945186, -0.0351002865329513, 0.0408314773570898, 
0.0178316690442225, -0.00911002102312537, -0.0438472418670439, 
0.0244082840236686, 0.00505415162454876, 0.00574712643678161, 
-0.0021428571428571, 0.0114531138153185, 0.00778485491861284, 
0.0337078651685394, -0.000679347826086942, 0.00815771583956487, 
0.00134861766689141, 0.00740740740740749, -0.0173796791443851, 
0.0108843537414966, -0.0067294751009421, 0.00542005420054201, 
0.0431266846361186, 0.00645994832041341, -0.0198973042362003, 
-0.00130975769482643, 0.00786885245901634, 0.00325309043591416, 
-0.0116731517509727, 0.0190288713910761, -0.0225370251126851, 
-0.0111989459815547, -0.00532978014656896, 0.0274614869390489, 
0.0234680573663624, -0.0171974522292993, -0.00648088139987036, 
-0.00913242009132424, 0, -0.0296247531270572, 0.0020352781546811, 
0.00135409614082597, -0.00135226504394859, 0.00947867298578203, 
-0.01140174379611), `589499102` = c(NA, -0.0387596899224807, 
0.0161290322580645, 0.0317460317460318, -0.0076923076923077, 
0.0465116279069768, -0.0222222222222222, 0, -0.0530303030303031, 
-0.04, 0.0583333333333334, 0.015748031496063, 0.0387596899224807, 
0.156716417910448, 0.0193548387096774, 0.0379746835443037, 0.0121951219512195, 
0.0120481927710843, -0.0119047619047619, -0.0361445783132529, 
-0.0937500000000001, 0.0206896551724138, -0.0338513513513514, 
0.021050423106511, 0.0136301369863014, -0.0404757078180959, 0.119718309859155, 
0.031446540880503, 0.0792682926829269, -0.0677966101694916, -0.127272727272727, 
-0.0556249999999999, 7.35348187366637e-05, -0.110220588235294, 
0.0742913808776135, -7.69230769230684e-05, 0.0308485268097546, 
-0.0597014925373135, 0.0873015873015874, -0.0437956204379562, 
0.0152671755725191, -0.0149624060150376, -0.00770933516525456, 
0.0076923076923077, -0.00763358778625955, -0.00776923076923077, 
0.0155826033025816, -0.00763358778625955, 0.0306923076923077, 
-0.0297783416672886, 0, 0, -0.00776923076923077, 0.0388402201721064, 
0.0671641791044775, -0.034965034965035, 0.0217391304347826, -0.00709219858156029, 
-0.0142857142857143, 0.00724637681159421, -0.0216546762589927, 
0.0147804985660709, -0.0144927536231883, -0.036764705882353, 
-0.00763358778625955, -0.0230769230769231, 0, 0.0235433070866142, 
7.69289945380329e-05, 0.0153076923076923, 0.00765209485567088, 
-0.0451127819548873, 0.0392913385826772, -0.0378058943859384, 
0.0866141732283464, 0.0289855072463768, -0.00704225352112677, 
0.0851063829787235, 0.0326797385620915, -0.0443037974683545, 
0.0264900662251656, 0.2, -0.021505376344086, -0.033021978021978, 
0.0171032445025286, 0, -0.0223463687150838, 0.0114285714285714, 
0.0169491525423729, 0.583333333333333, -0.0491228070175439, 0.0848708487084871, 
0.0238095238095238, 0.0232558139534885, 0, -0.0746753246753247, 
0.0456140350877193, -0.0805369127516778, 0.0474452554744525, 
-0.0383275261324043, 0.101449275362319, 0.0328947368421053, -0.0955414012738854, 
-0.0316901408450704, -0.050909090909091, 0.0459770114942529, 
0.0622710622710622, 0.0344827586206897, -0.00999999999999993, 
0.0437710437710437, 0.0741935483870968, 0.165165165165165, -0.103092783505155, 
0.158045977011494, 0.0446650124069478, -0.0736342042755345, 0.0358974358974359, 
-0.153465346534653, 0.0701754385964913, 0.0437158469945354, 0.0523560209424083, 
0.0696517412935324, 0.00465116279069778, -0.00462962962962974, 
0.0139534883720931, 0.0229357798165137, 0.0493273542600896, -0.00213675213675209, 
-0.0877944325481799, 0, -0.0516431924882629, 0.00247524752475242, 
-0.0395061728395061, 0.0359897172236504, 0.00496277915632744, 
-0.0172839506172839, 0.0150753768844221, -0.0544554455445545, 
-0.0183246073298429, 0, -0.00799999999999995, 0, -0.00806451612903232, 
-0.00813008130081296, 0.0136612021857923, 0.0161725067385445, 
0.0716180371352785, 0.0247524752475247, -0.0120772946859903, 
-0.0464547677261614, -0.0179487179487179, 0.0417754569190601, 
-0.0300751879699248, -0.0232558139534884, 0.0211640211640212, 
-0.0751295336787565, 0.0420168067226892, 0.0188172043010752, 
-0.00527704485488127, -0.00795755968169756, 0.0080213903743315, 
-0.0159151193633952, -0.00808625336927218, -0.0217391304347826, 
-0.0277777777777778, 0.00571428571428572, -0.0909090909090909, 
-0.0593750000000001, 0.102990033222591, -0.00602409638554217, 
0.0545454545454546, 0.00574712643678161, 0.0114285714285714, 
-0.00282485875706221, -0.00849858356940504, 0.0257142857142857, 
-0.0417827298050139, 0.0174418604651163, 0.0371428571428571, 
0.0909090909090909, 0.00252525252525258, 0.00251889168765738, 
0.035175879396985, -0.00970873786407768, 0.00980392156862746, 
0.0315533980582524, 0, -0.0329411764705882, -0.0608272506082726, 
-0.0259067357512954, 0.0372340425531915, 0.0282051282051282, 
-0.0124688279301745, 0.0151515151515151, 0.0447761194029852, 
-0.0166666666666667, -0.0145278450363195, 0.0221130221130221, 
0.00721153846153852, -0.0357995226730311, -0.0247524752475248, 
-0.0177664974619289, 0.00258397932816532, 0.0412371134020619, 
-0.0569306930693069, 0.005249343832021, 0.0339425587467363, -0.0656565656565656, 
0.0270270270270269, -0.0578947368421052, 0.00837988826815637, 
0.0193905817174516, -0.00815217391304355, 0.0602739726027398, 
0.00516795865633075, 0.00771208226221075, -0.0331632653061224, 
-0.00791556728232196, -0.0425531914893616, -0.133333333333333, 
0.0288461538461538, 0.0218068535825545, -0.0335365853658536, 
-0.00946372239747628, -0.0222929936305733, 0.0228013029315962, 
-0.00955414012738861, 0.0192926045016077, -0.0189274447949527, 
-0.0192926045016077, -0.0163934426229508, 0.0166666666666666, 
0.00327868852459024, 0.0588235294117648, 0.0308641975308641, 
-0.0179640718562874, -0.0243902439024389, 0, 0.00312499999999993, 
0.00311526479750786, -0.00621118012422361, 0.00625000000000001, 
0, 0.0248447204968943, 0.0272727272727274, 0.00884955752212384, 
-0.00292397660818707, 0.00293255131964803, 0.00584795321637427, 
-0.00872093023255808, -0.00293255131964816, -0.011764705882353, 
0.0148809523809525, 0, -0.0205278592375367, 0.0149700598802396, 
-0.0029498525073747, -0.0266272189349112, -0.0729483282674773, 
0.019672131147541, 0, -0.00643086816720258, 0.0194174757281554, 
-0.0285714285714285, -0.0522875816993464, -0.096551724137931, 
0.0343511450381679, -0.022140221402214, -0.0150943396226415, 
0.0459770114942529, -0.0476190476190476, 0.0115384615384615, 
-0.0114068441064638, -0.0423076923076923, -0.0843373493975905, 
-0.00438596491228061, 0, 0.026431718061674, 0.0300429184549356, 
0.0458333333333333, -0.0438247011952191, -0.0208333333333333, 
0.0468085106382978, -0.00406504065040642, 0, 0.00408163265306114, 
-0.0447154471544715, -0.0297872340425533, -0.013157894736842, 
-0.0444444444444445, -0.0697674418604651, 0.01, 0.123762376237624, 
-0.00881057268722468, -0.031111111111111, 0.00917431192660551, 
-0.0181818181818182, 0.00925925925925927, -0.0504587155963304, 
0.0772946859903382, 0.00448430493273553, -0.0357142857142857, 
0.00925925925925927, 0.0275229357798165, 0.00892857142857124, 
-0.0221238938053097, -0.0226244343891402, 0.0370370370370371, 
-0.0267857142857143, 0, -0.00917431192660551, -0.0277777777777778, 
-0.0142857142857144), `589889104` = c(NA, -0.0427206295671725, 
-0.0240751614797416, -0.00722021660649825, 0.00969696969696971, 
-0.0276110444177672, 0.00493827160493839, 0.0196560196560197, 
-0.0180722891566265, 0.0110429447852761, -0.0109223300970874, 
-0.0368098159509203, 0.0585987261146498, -0.0108303249097473, 
-0.0310218978102191, 0.0156936597614564, -0.00988875154511744, 
0.0268414481897628, -0.0534954407294832, -0.0115606936416185, 
0.0337881741390513, 0.0169704588309239, 0.0203955500618048, 0.0157480314960629, 
-0.10077519379845, -0.0616710875331565, -0.031095406360424, 0.0029175784099197, 
-0.0152727272727273, 0.0288035450516987, -0.0495333811916726, 
-0.0385196374622356, -0.0125687352710134, -0.0461416070007956, 
-0.02418682235196, 0.0188034188034189, -0.0578859060402684, 0.0195903829029385, 
-0.0270742358078602, -0.059245960502693, -0.00477099236641228, 
0.0239693192713327, -0.0646067415730337, -0.00500500500500508, 
-0.0251509054325956, 0.0288957688338494, -0.0100300902708126, 
0.0851063829787236, 0.103641456582633, 0.00761421319796953, 0.0268681780016793, 
0.017170891251022, -0.0136655948553055, -0.026079869600652, 0.0736401673640168, 
-0.0436477007014809, 0.0105949470252649, 0.00725806451612902, 
-0.00880704563650916, -0.00161550888529898, -0.012135922330097, 
0.0212940212940213, 0.0360866078588612, -0.0108359133126935, 
-0.0211267605633803, -0.0207833733013589, 0.0326530612244898, 
0.0490118577075098, 0.0195930670685757, 0.00591278640059128, 
0.0301249081557678, -0.00784593437945788, 0.0287562904385335, 
-0.0489168413696716, 0.0514327700220427, -0.0174703004891684, 
-0.0661450924608819, -0.00380807311500386, -0.00688073394495412, 
0.0300230946882218, 0.190582959641256, -0.0263653483992467, -0.0490006447453256, 
0.00542372881355933, -0.0209035738368173, -0.0110192837465565, 
-0.00208913649025065, 0.0293091416608514, -0.0338983050847458, 
0, -0.0105263157894737, 0.00141843971631203, 0.024787535410765, 
0.0366275051831375, -0.02, -0.0081632653061224, -0.0281207133058985, 
-0.00917431192660556, 0.0413105413105413, -0.0362517099863201, 
-0.0326472675656493, 0.00440205429200284, 0.0467494521548576, 
0.0404745289602233, 0.00737759892689466, 0.0798934753661784, 
0.00678175092478418, -0.0085731781996324, -0.00370599135268698, 
-0.00619962802231853, 0.00374298190892069, -0.00870105655686765, 
-0.012539184952978, -0.0158730158730159, 0.0245161290322581, 
0.00503778337531487, 0.0338345864661654, -0.0242424242424242, 
-0.013664596273292, -0.0132241813602016, 0.00638162093171663, 
0.0348763474952442, -0.0159313725490197, 0.0149439601494397, 
0.00613496932515324, -0.021341463414634, 0.0068535825545171, 
-0.00742574257425749, 0.0317955112219452, 0.0132930513595165, 
0.0125223613595707, 0.0229681978798587, 0.0241796200345422, -0.0112422709387296, 
0.0375213189312109, 0.00493150684931506, -0.0169029443838603, 
-0.0188574597892401, -0.00960994912379885, 0.000570776255707852, 
0.00798630918425559, 0.0147142048670061, -0.0133853876185164, 
0.080271339739966, -0.0198848770277341, -0.02349172450614, -0.0267905959540732, 
0.0241573033707865, -0.0318156884256721, -0.0260623229461755, 
-0.0104712041884817, 0.0135214579659022, 0.00116009280742478, 
0.0405561993047508, -0.00835189309576849, -0.0162829870859067, 
-0.0159817351598174, 0.0156612529002322, 0.0148486579097657, 
-0.00281373100731574, 0.0141083521444695, -0.00723427935447963, 
0.0207399103139014, 0.0164744645799012, -0.00378173960021611, 
-0.0157266811279828, -0.00550964187327812, -0.0171745152354572, 
-0.00394588500563679, 0.0141482739105829, -0.00167410714285721, 
-0.000558971492453972, 0.0212527964205818, 0.0032858707557502, 
-0.0141921397379914, 0.0282392026578074, 0.0048465266558966, 
0.0155412647374062, 0.00316622691292888, -0.0168332456601789, 
-0.0251471375066882, -0.00933040614709101, -0.0171745152354572, 
-0.020293122886133, -0.005753739930955, 0.0150462962962962, -0.0102622576966933, 
-0.00172811059907841, -0.0334679746105019, -0.0185074626865671, 
-0.0133819951338201, -0.00123304562268801, -0.00308641975308646, 
-0.0154798761609906, 0.0012578616352201, -0.0125628140703517, 
0.0693384223918574, -0.0011897679952409, 0.0017867778439548, 
-0.00297265160523191, 0.00417412045319024, -0.0071258907363421, 
0.0149521531100478, 0.009428403064231, -0.0064214827787507, -0.00998824911868379, 
-0.00890207715133544, 0.0179640718562875, 0.0317647058823529, 
-0.0319270239452679, -0.00530035335689045, -0.00473653049141515, 
-0.0130874479476501, 0.0367691380349608, -0.00581395348837197, 
0.00467836257309931, -0.00465657741559944, -0.00467836257309952, 
-0.00411280846063456, -0.00766961651917398, 0.0326991676575506, 
-0.00345423143350618, -0.0132871172732525, -0.00468384074941442, 
0.00882352941176462, 0.0151603498542275, -0.0057438253877083, 
-0.00519930675909878, -0.019163763066202, -0.0242747187685021, 
0.0182038834951457, 0.0250297973778306, -0.00232558139534879, 
0.0273892773892773, 0.00170164492342604, -0.00113250283125705, 
0.00113378684807254, 0.0101925254813137, 0.00504484304932735, 
0.0100390407138873, 0.011043622308117, 0.0212998361551065, -0.00320855614973255, 
0.0150214592274679, 0.00369978858350934, 0, 0.0152711953659822, 
-0.00570539419087134, 0.0135628586332811, 0.0190427174472466, 
0.00353535353535355, -0.031706089582285, 0.0265072765072766, 
-0.0318987341772151, -0.0271966527196652, -0.00376344086021507, 
0.00215866162978948, 0.0360796984383413, 0.0103950103950105, 
0.00154320987654309, -0.00410888546481758, -0.0268179473955647, 
0.0196078431372548, -0.0119542619542618, -0.0268279852709101, 
-0.0205405405405405, -0.0187637969094923, -0.00731158605174368, 
0.0186968838526913, -0.0122358175750834, 0.00394144144144126, 
-0.0112170499158721, 0.00283607487237667, -0.0147058823529411, 
-0.0195177956371988, 0.00409836065573772, -0.0186588921282799, 
0.00772430184194905, -0.00117924528301884, 0.0070838252656433, 
-0.0169988276670574, -0.00298151460942163, 0.00239234449760782, 
0.0405727923627685, -0.173738532110092, 0.029146426092991, 0.00134861766689141, 
0.0181818181818182, -0.0112433862433862, -0.0153846153846153, 
0.00475543478260859, -0.0182555780933063, 0.00757575757575766, 
0.00546821599453179, 0.0319510537049625, 0.00527009222661397, 
-0.00196592398427257, -0.010505581089954, -0.0311877903118779, 
-0.0383561643835617, 0.0334757834757835, 0.00551343900758098, 
0.0274160383824538, 0.00733822548365573, 0.00860927152317886, 
0.00328299409061057, 0.00654450261780114, -0.0156046814044213, 
-0.0204755614266843, 0.0114632501685772, 0.0233333333333333, 
0.0130293159609121, -0.0192926045016078)), row.names = c(NA, 
313L), class = "data.frame")```


Solution

  • This is how I would approach this:

    library(data.table)
    setDT(strategic_return)
    
    #create column with row numbers
    strategic_return[, rowID := .I]
    
    #create a column with x values,
    #since data doesn't contain these
    strategic_return[, x := rnorm(.N)]
    
    #reshape to long format
    DF <- melt(strategic_return, id.vars = c("Date", "rowID", "x"))
    
    #select data
    DF <- DF[strategic_dateandcusip, .SD, on = c("variable == strategic_cusip", 
                                           "rowID >= row_start_regression",
                                           "rowID <= row_end_regression")]
    
    library(nlme)
    fit <- lmList(value ~ x | variable, data = DF, pool = FALSE)
    summary(fit)
    
    library(lmtest)
    lapply(fit, coeftest, cov. = vcovHC, type = "HC1")