Regression not working for variables with numbers as column names

Hi all,

I have the below dataset containing dates, returns for a several stocks (the stocks are classified by numbers), and other independent variables.


I want to execute multiple regressions on each stock, but my problem is that the regression function does not work when the column name is a number.

> reg.test <- lm(formula= "10026 ~ mktrf + hml + smb", data= retDailyMerged ,na.action = na.omit)

I get the following error message: "Error in terms.formula(formula, data = data) : invalid term in model formula"

I am certain that the problem lies with the column name being a number, because when I substitute the column name as a letter it works just perfectly fine.

data:

structure(list(date = structure(c(17533, 17534, 17535, 17536, 
17539, 17540, 17541, 17542, 17543, 17547, 17548, 17549, 17550, 
17553, 17554, 17555, 17556, 17557, 17560, 17561, 17562, 17563, 
17564, 17567, 17568, 17569, 17570, 17571, 17574, 17575, 17576, 
17577, 17578, 17582, 17583, 17584, 17585, 17588, 17589, 17590, 
17591, 17592, 17595, 17596, 17597, 17598, 17599, 17602, 17603, 
17604, 17605, 17606, 17609, 17610, 17611, 17612, 17613, 17616, 
17617, 17618, 17619, 17623, 17624, 17625, 17626, 17627, 17630, 
17631, 17632, 17633, 17634, 17637, 17638, 17639, 17640, 17641, 
17644, 17645, 17646, 17647, 17648, 17651, 17652, 17653, 17654, 
17655, 17658, 17659, 17660, 17661, 17662, 17665, 17666, 17667, 
17668, 17669, 17672, 17673, 17674, 17675), tzone = "UTC", tclass = "Date", class = "Date"), 
    `10026` = c(NA, -0.00998786767581905, 0.0138127158236847, 
    -0.00955052427703185, 0.000741739716790146, -0.00720975675493563, 
    -0.0127595357676122, 0.00776838940132052, -0.000613957295859224, 
    0.00109215017064845, 0.0367516705304787, -0.01025971719829, 
    0.0219947490202173, -0.026332899869961, 0.00313856427378956, 
    -0.0123818399680468, 0.0172553248854137, -0.0145109992048768, 
    0.00551334633228007, -0.0706117686392511, -0.00395719088012159, 
    0.00794560820572077, -0.0183459236309247, -0.0273031829464154, 
    0.0200389507626124, -0.00860856449120728, -0.0190738445098826, 
    0.0130135431641067, 0.00261416087833322, 0.00275618275058243, 
    0.0278582571874304, 0.00303555941023403, 0.00230587980977082, 
    -0.00359453604640292, -0.0191198398903435, -0.00970945200441331, 
    0.00586793433855748, 0.0125535371437011, -0.0070011668611436, 
    -0.0134400705052877, 0.00126554008784319, 0.0202230483271375, 
    0.0107127240926979, 0.0162232316677482, 0.00276713495104319, 
    -0.002334960730206, 0.00567375886524824, -0.00486600846262342, 
    -0.00070873786407788, -0.0147507279448781, -0.025552366339334, 
    0.00967646624316743, -0.00424317799400087, -0.00330614943795471, 
    -0.013563246351172, -0.000896801606874886, -0.0126403141361257, 
    0.0240133341423632, -0.000739680480816629, -0.0034053893988748, 
    0.0144109344822463, -0.0167691857059168, 0.0236091457510985, 
    0.0130238649592549, 0.00933706816059776, -0.0145876325339785, 
    0.0022385181975737, 0.00943886515158621, 0, 0.000142683787103248, 
    -0.00413938053097351, 0.0205676523267631, 0.0109542869180534, 
    0.000138987289018733, -0.0133342583975098, -0.00985436687165719, 
    -7.10883628349812e-05, -0.00277264325323467, 0.00128323946674258, 
    0.000142399430402396, -0.00989535132056663, -0.0120074777106702, 
    -0.0388617276762973, -0.0182479731772566, -0.00647840505938613, 
    0.0301194666884437, 0.0177091183119822, 0.0033320992225101, 
    0.00177129151291533, 0.000441947808903276, 0.00942562592047125, 
    -0.00809746133644595, 0.00544245054056058, 0.0141174007667761, 
    0.0119734564339298, 0.00285103349964344, 0.00106616915422886, 
    -0.0255591746141871, 0.00582877959927153, 0.0057225642883012
    ), `10028` = c(NA, 0.0102061855670104, -0.0205122971731809, 
    -0.0103146488851844, 0.0631645436361723, -0.0690167343301317, 
    0.0316953839608594, -0.00824742268041234, 0.0343035343035343, 
    -0.0733668341708542, 0.0734273318872016, -0.000303122158229807, 
    0.0309278350515465, -0.0287254901960784, 0.00938730190774195, 
    0.0501, -0.0191410341872202, 0, -0.0679611650485438, 0, 0.046875, 
    -0.0348258706467661, -0.020618556701031, -0.0313684210526315, 
    0.000869376222560447, 0.0206297502714439, -0.0106382978723403, 
    -0.0322580645161291, 0.0222222222222221, 0, 0.0158695652173912, 
    -0.00481489407233038, 0.02128803354478, -0.0209495736393304, 
    0.0103225806451612, 0.00234142188165176, 0.00934380972605653, 
    -0.000525983589312107, -0.000105252078728579, -0.0210526315789473, 
    -0.0053763440860215, 0.0810810810810809, -0.053, -0.0390707497360083, 
    0.0219780219780219, -0.0108602150537634, 0.000108707468203129, 
    0, 0.0080434782608696, -0.0187621306879447, 0.000109890109890154, 
    -0.000109878035380762, 0, -0.0307692307692308, -0.0258503401360545, 
    0.0416666666666667, -0.0100558659217878, -0.0244920993227992, 
    -0.0396852944579429, 0.013975903614458, -0.0137832699619772, 
    -0.0240963855421685, 0.0246913580246912, 0.0110843373493976, 
    -0.0238322211630123, -0.00769042968750011, 0.017099274203469, 
    0.0293904208998548, 0.0222065562213607, -0.0219540229885058, 
    0, 0.00434833705488313, 0.0063187456119822, 0.00813953488372099, 
    -0.00807381776239913, 0, -0.000116279069767411, -0.0232585184323759, 
    0.0121443028932016, -0.0295259381249265, 0.0060606060606061, 
    0.0120481927710843, -0.0119047619047619, -0.00614457831325299, 
    -0.0180627954903624, -0.0246913580246914, -0.0126582278481012, 
    0.0384615384615385, -0.0234567901234568, -0.00126422250316061, 
    -0.0341772151898735, -0.0252948885976408, -0.0181524808390481, 
    -0.0153382634894549, 0.0739916550764952, -0.0282310282310283, 
    0.0126599147121536, -0.0130280300039479, -0.0253333333333333, 
    0.0292749658002736), `10032` = c(NA, 0.00130975769482644, 
    0.0122629169391759, 0.00492650621870472, 0.0466929197138954, 
    -0.00906019656019663, -0.0119324345265769, 0.0123902132998746, 
    0.0204492641363285, -0.0318809776833157, 0.00925199937274579, 
    -0.0658794282162835, 0.0073186959414504, -0.00974240422721273, 
    0.00533600133400025, -0.0106153590976945, 0.0149203688181057, 
    0.0122233234225306, -0.0119125326370758, -0.00990916597853009, 
    -0.00333611342785656, 0.0123849372384937, -0.0110762109439577, 
    -0.027415580073554, 0.0134066689584049, 0.00729308005427409, 
    -0.0313184037716787, -0.00104293412132805, 0.010266225856969, 
    -0.0072338959696866, 0.0152671755725191, 0.0105946684894054, 
    0.000338180588434156, 0.0270453008789724, -0.00971033574720204, 
    0.00515206913744382, 0.00793650793650791, 0.0150918635170605, 
    -0.0105042016806723, -0.0148619957537155, -0.00513925729442977, 
    0.0236627228795201, 0.00455803353410378, 0.0134500081024145, 
    0.00959385992964501, -0.0023756731073804, 0.0204794411811398, 
    0.0132233976353453, -0.0102871180715493, -0.00853242320819103, 
    0.00688468158347666, 0.0226884226884227, -0.0132198753988756, 
    -0.00615953187557727, -0.00185931205453993, -0.0276311704439616, 
    -0.0370370370370371, -0.00116047745358094, -0.0293775933609959, 
    0.000341997264021954, 0.0210256410256409, -0.0165745856353591, 
    0.0156622403813413, 0.00134093194770379, 0.00150652828925346, 
    -0.0207253886010362, -0.00273084144051894, 0.0255005990073593, 
    0.00901201602136181, 0.00628514720476359, 0.0175871137409598, 
    0.00242287191083834, 0.00032226877215602, 0.00982603092783507, 
    -0.0012761205933961, -0.00511100463184799, 0.000481618237277326, 
    -0.00706033376123227, -0.00646412411118302, -0.110279765777489, 
    0.0157221206581353, -0.0129589632829373, 0.0255288110867979, 
    0.00782361308677104, -0.000705716302046522, 0.027542372881356, 
    -0.0020618556701032, 0.00447658402203865, -0.00308536167295181, 
    0.0110041265474552, 0.00544217687074822, -0.00541271989174563, 
    -0.00935374149659862, 0.0116738197424893, 0.00916341422026123, 
    0.00504456028249556, -0.00150577212648495, -0.0102211796246648, 
    -0.0135432537667174, -0.00858074480864934), `10044` = c(NA, 
    0.0256410256410258, 0.00249999999999995, -0.00249376558603487, 
    -0.0083333333333333, 0.000840336134453779, 0.00923593618807717, 
    0.0391014975041597, -0.0120096076861489, -0.00729335494327388, 
    0.00489795918367353, -0.00649878147847283, 0.017170891251022, 
    -0.00401929260450151, 0.00322841000807106, -0.00876910699919553, 
    0.0136352568785003, -0.00712627111858444, 0, 0.0024193548387097, 
    -0.00321802091713586, -0.00968523002421318, 0.00407497962510184, 
    -0.0089285714285714, -0.000819000819000992, 0.00491803278688518, 
    -0.00407830342577475, 0.0141195741195741, -0.00180901925313359, 
    0.00970873786407767, 0.00881410256410242, -0.0015885623510723, 
    0.000795544948289484, -0.0174880763116058, 0.00404530744336573, 
    -0.0112812248186946, -0.00488997555012216, 0.00654381654381653, 
    -0.00681047038625204, -0.000507938588586221, 0.00327868852459035, 
    -0.000816993464052285, -0.00899427636958305, -0.00165016501650161, 
    -0.00165289256198342, 0.0124172185430464, -0.00368765331152898, 
    -0.0102503918784725, -0.0074626865671642, -0.0158730158730159, 
    -0.00169779286926997, 0.056122448979592, -0.0177133655394526, 
    -0.012295081967213, 0, -0.0032365145228217, 0.00907501456997761, 
    -0.0053877887788778, 0.00956473408711944, -0.0115036976170912, 
    0, 0.00332502078137997, -0.00248550124275071, 0.00249169435215957, 
    0.0149130074565038, -0.0204081632653061, 0.00916666666666655, 
    0, -0.000825763831544202, 0.000371900826446225, 0.00293279358916121, 
    -0.00329489291598029, 0.00543801652892562, -0.00458662808857624, 
    -0.00328654004954576, 0.000480522278007101, -0.00960582974494861, 
    -0.0142140468227426, 0.000848176420695568, -0.0144067796610169, 
    0, -0.00343938091143603, 0.00690250215703192, -0.00783204798628967, 
    -0.00592472319624138, 0.00955690703735868, -0.00413080895008611, 
    -0.00535776011061173, -0.00086880973066894, 0.0057304347826086, 
    -0.00310395213515591, -0.000867302688638327, 7.81249999999289e-05, 
    0.00426181982310414, 0.00172860847018153, -0.00431406384814503, 
    -0.00519930675909863, -0.00958188153310113, 0.00143359718557612, 
    0.00108902804247202), `10051` = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), `10065` = c(NA, 0.00794701986754975, 0.00854139290407363, 
    0.00195439739413694, 0.00260078023407018, 0.00324254215304798, 
    0, 0.00646412411118291, 0.00642260757867685, 0, 0.00382897255902992, 
    -0.000635727908455119, 0.00381679389312972, 0.00633713561470217, 
    0.0100755667506296, -0.00311720698254359, -0.00250156347717334, 
    0.00752351097178683, -0.00560049782202865, -0.00876095118898623, 
    0.0037878787878789, -0.00125786163522013, -0.0214105793450883, 
    -0.0444015444015444, 0.0154882154882154, 0.00265251989389914, 
    -0.0337301587301587, 0.00889801505817944, 0.0128900949796471, 
    0.00200937709310112, 0.0140374331550801, 0.007910349373764, 
    0.00327011118378029, -0.00651890482398954, -0.00262467191601057, 
    0.00131578947368438, 0.0151116951379762, 0.00970873786407767, 
    -0.00641025641025639, -0.0109677419354839, -0.0123939986953685, 
    -0.00198150594451796, 0.00727994705493051, 0.00131406044678051, 
    0.000485564304461894, 0.00476146752889006, 0.0182767624020888, 
    0.00448717948717947, -0.00957243139757502, -0.009020618556701, 
    -0.0013003901170352, 0, -0.0110677083333334, 0.00197498354180392, 
    0.000657030223390365, -0.0262639527248851, -0.0215778826702629, 
    0.0248104755341143, -0.0161398789509077, -0.00273410799726592, 
    0.0123372172721041, -0.022342586323629, 0.0110803324099724, 
    0.0123287671232877, 0.00744248985115026, -0.0188045668233715, 
    0.00136892539356603, 0.0143540669856459, -0.00202156334231807, 
    0.00405131667792014, -0.00268997982515129, 0.00674308833445725, 
    0.0107166778298728, 0.00331345261762772, -0.00330250990752978, 
    -0.0119284294234592, 0.00201207243460755, -0.0120481927710843, 
    -0.00135501355013545, 0.0135685210312075, 0.000669344042838027, 
    -0.00468227424749157, 0.000672043010752743, -0.00402955003357963, 
    -0.00202292650033709, 0.0128378378378378, 0.000667111407605114, 
    0, 0.00733333333333319, 0.00992720052945062, 0.00262123197903019, 
    0.00130718954248366, -0.00391644908616195, 0, -0.00196592398427253, 
    -0.00328299409061072, 0.00856389986824779, -0.000653167864141047, 
    0.000653594771241828, 0.000653167864141047), `10104` = c(NA, 
    0.023161055114733, 0.00985118423810527, 0.00601909506019083, 
    0.0105219723540333, 0.00163331972233571, -0.00529963310232384, 
    0.00307377049180335, 0.0114402451481102, 0.00161583518481123, 
    0.0137124420246018, -0.0007957032027055, 0.00696794744176787, 
    0.00257018584420732, 0.00808519029777166, 0.00645539906103298, 
    0.00291545189504361, 0.0222868217054264, -0.0153554502369668, 
    -0.0188679245283019, 0.0123626373626373, 0.000775344058926208, 
    -0.0290528762347473, -0.0406941950927588, 0.0272405905593678, 
    -0.0107287449392712, -0.041538776345406, 0.0190008539709647, 
    0.00879949717159012, 0.00706126687435105, 0.0197978964734997, 
    0.0240647118301314, 0.00138230647709325, -0.0161703805955433, 
    -0.00922028462617763, 0.00323690066761073, 0.018350473885864, 
    0.0132673267326733, -0.00859878835255046, -0.0011827321111767, 
    -0.0189461219656601, 0.0122711728022531, 0.0192766295707472, 
    0.00409436537336716, 0.00427184466019415, 0.00792730085073479, 
    0.0161135622482256, -0.00132150273739851, -0.0060491493383743, 
    -0.00494484594903, 0.0009556574923546, -0.00190949016612552, 
    -0.00612205854218484, -0.0943214629451397, -0.000637619553666147, 
    -0.0240323266695024, -0.0239703639137068, 0.0377316365260103, 
    -0.0236660929432012, -0.00881445570736017, 0.0171187194308582, 
    -0.0161748633879781, -0.00266607420573195, 0.0138115393183336, 
    0.00988793671720511, -0.0245865970409052, 0.00156145438322564, 
    0.0204899777282852, -0.00458315146224353, 0.00591975443981596, 
    0.00435919790758499, -0.000651041666666741, 0.0132464712269273, 
    0.00900128589798554, -0.00552251486830924, -0.0126014523707818, 
    -0.00930131948950896, -0.00589519650655013, 0.00483197891500109, 
    0.00459016393442635, -0.0093559617058312, 0.00307489567318253, 
    0.00613093934749287, -0.00957562568008719, -0.00988793671720489, 
    0.0144252108300045, 0.0078757383504704, -0.00282179292381168, 
    0.0139312146277755, 0.00751395448690428, -0.00234391647134025, 
    0, -0.000213583938487805, -0.00170903653065591, -0.00406591054996785, 
    -0.00472711645895996, 0.0192141623488773, -0.0177928404998942, 
    0.0114298037524261, -0.00938166311300637), mktrf = c(0.0085, 
    0.0059, 0.0042, 0.0066, 0.0019, 0.0015, -7e-04, 0.0087, 0.0066, 
    -0.0049, 0.0095, -0.0018, 0.0057, 0.0077, 0.0022, -0.0013, 
    7e-04, 0.0111, -0.0062, -0.0106, -7e-04, 3e-04, -0.0213, 
    -0.0403, 0.0167, -0.0037, -0.0368, 0.0136, 0.0136, 0.0031, 
    0.0152, 0.0121, 3e-04, -0.0062, -0.0043, 1e-04, 0.0154, 0.0112, 
    -0.0125, -0.011, -0.0118, 0.007, 0.0106, 0.0036, 5e-04, 0.0037, 
    0.017, -9e-04, -0.0067, -0.0053, -0.0018, 0.0025, -0.0138, 
    0.0015, -6e-04, -0.0254, -0.0209, 0.0267, -0.0187, -0.0035, 
    0.0141, -0.0229, 0.0124, 0.0117, 0.0075, -0.0219, 0.003, 
    0.0177, -0.0049, 0.0084, -0.0037, 0.0082, 0.0109, 0.0013, 
    -0.0049, -0.0081, -5e-04, -0.013, 0.001, 0.0096, 1e-04, -0.008, 
    0.0024, -0.0063, -0.0025, 0.013, 0.0042, 7e-04, 0.0089, 0.0084, 
    0.0019, 5e-04, -0.0055, 0.005, 2e-04, -0.0023, 0.0072, -0.0042, 
    0.0029, -0.0016), smb = c(0.0036, -0.0039, -0.0026, -0.0034, 
    -0.0016, -0.0036, 7e-04, 0.0116, -0.003, -0.0102, -2e-04, 
    -0.005, 0.0094, -0.0034, 0.0014, -0.008, 0.0017, -0.0062, 
    -2e-04, 0.0011, -0.0072, 0.001, 3e-04, 0.0078, -0.0051, 0.0051, 
    0.0083, -0.0062, -0.0043, 5e-04, 0.0026, -0.0014, 0.0036, 
    -0.0036, 0.0078, 4e-04, -0.0036, -0.0049, -0.002, -0.004, 
    0.0101, 0.0116, -0.0039, 0.0067, 0.008, -0.0049, -0.0027, 
    0.0046, 9e-04, 0.0023, -0.0041, 0.0046, 0.0034, -7e-04, 0.0075, 
    0.005, 0.002, -0.0069, -0.0024, 0.0011, -0.0029, -0.0014, 
    -5e-04, 0.0032, 3e-04, 0.004, -0.0015, 0.004, 0.0092, -0.0022, 
    2e-04, 8e-04, 0.0016, 0.0024, -0.0046, 6e-04, -8e-04, 0.0052, 
    -0.0034, -0.0041, -0.0027, 3e-04, 0.0022, 0.0126, -0.005, 
    3e-04, 0.0042, 0.0048, -0.0019, -0.0043, 0.0011, -0.0035, 
    0.0068, 0.007, 0.009, 0.0056, -0.0021, -0.0075, 5e-04, 0.0034
    ), hml = c(-0.0022, -0.0021, 0.0024, -0.0026, 7e-04, -3e-04, 
    0.0058, 0.0029, 0.0017, -2e-04, -0.0015, -0.0025, -7e-04, 
    -0.0014, -0.0033, 0.0039, -0.0049, -0.005, -0.0026, -0.0017, 
    6e-04, 0.0051, -0.0027, -0.0056, -0.0023, 0.0027, 0.0047, 
    0.0011, -0.0029, -0.0023, 0.0046, -0.0065, 0.0013, -0.0026, 
    0.002, -0.0039, -0.0014, -7e-04, 1e-04, -0.0031, -4e-04, 
    -0.005, 0.0025, 0.001, -0.0045, -0.0033, 0.0028, -6e-04, 
    -1e-04, -0.0054, 0.0031, 0.002, 0.0041, -0.0038, 0.0045, 
    -0.0046, -0.002, 2e-04, 0.0039, 0.0065, -0.0021, 0.0039, 
    0.0017, -0.0031, 0.0048, -6e-04, -0.005, -0.0012, -0.0031, 
    0.0019, -0.0025, -4e-04, -0.0118, 2e-04, 0.0108, 0.0062, 
    0.002, 0.0098, 1e-04, -0.0085, 7e-04, -8e-04, -0.0053, -0.0028, 
    -0.0017, -0.0023, -0.0036, 0.0026, 0.0024, -4e-04, -0.0037, 
    0.0011, 0.0041, -0.0024, 0.0025, -0.0058, 0.0043, 0.0059, 
    -0.0071, -0.0031), rf = c(5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 
    5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 
    5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 5e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 
    7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 
    7e-05, 7e-05, 7e-05, 7e-05, 7e-05, 6e-05, 6e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 6e-05, 
    6e-05, 6e-05, 6e-05, 6e-05, 6e-05), umd = c(-0.0069, 0.0018, 
    -0.003, 0.0062, 0.0014, 0.0048, -0.0023, -0.0032, 3e-04, 
    -0.0019, 0.0102, 0.0042, 0.0057, -0.0038, 1e-04, -0.0019, 
    0.006, 0.0059, -0.001, 0.0034, 0.0033, -0.0022, 0.0016, -0.0017, 
    0.0076, 0.0018, -0.0113, 0.0063, 0.0053, 0.0066, 0.0017, 
    0.0036, -0.0016, 0.0035, 0.0078, 0.0015, 9e-04, 0.001, 0.0028, 
    0.0056, -0.008, -0.0017, -0.0046, 0.0036, 0.008, -0.0013, 
    0.006, -0.0052, -0.001, 0.0034, 0.0031, -0.0047, 0.0046, 
    0.0139, -0.0033, -0.0097, -0.0035, 0.0132, -0.0178, -0.0114, 
    0.0059, -0.001, -0.0014, 0.0012, -0.0036, -0.0055, 0.0058, 
    0.0037, -0.0055, 0.0124, -0.0079, -1e-04, 0.0107, 3e-04, 
    0.007, 2e-04, -0.0035, -0.0108, -0.0056, 0.0092, 0.003, 0.0028, 
    0.0036, 0.0041, 0.0086, -0.0044, 0.0029, 0.0044, 0.003, -0.002, 
    0.0017, -0.0038, -6e-04, -2e-04, 0.0021, 0.0064, 0.0025, 
    -0.0021, 0.0043, 0.0013)), class = c("data.table", "data.frame"
), row.names = c(NA, -100L), .internal.selfref = <pointer: 0x7f878680d6e0>, sorted = "date")

Try quoting that part within backticks, like follows:

reg.test <- lm(formula= "`10026` ~ mktrf + hml + smb", data= retDailyMerged ,na.action = na.omit)
1 Like

cool, that was a quick fix.

How would you loop the dependent variables now so that I don't have to run the regression for each stock separately?

I tried creating a list with stock names/numbers and looping it, but it doesn't work.

# creating a list with dependent variables
depVarList <- names(retDailyMerged[,2:7670])
# looping with lapply
model <- lapply(depVarList, function(x) {
  lm(substitute(i~ mktrf + hml + smb , list(i = as.name(x))), data = retDailyMerged)})
lapply(model, summary)
# alternative: looping with for
reg.result.retDaily <- 
  for (i in depVarList) 
  {lm(formula= "i ~ mktrf + hml + smb", data= retDailyMerged ,na.action = na.omit)}

First try the simple way:

depVarList <- names(x = retDailyMerged)[2:7670]

lapply(X = depVarList,
       FUN = function(t) lm(formula = paste0("`", t, "` ~ mktrf + hml + smb"),
                            data = retDailyMerged))

You'll face problems for those columns which has all NA values. For example, the 6th column of your shared dataset.

If it doesn't solve your problem, please ask a new question with a MINIMAL REPRODUCIBLE EXAMPLE. Please include only a small part of your dataset sufficient to reproduce your problem. And, verify that the output of dput actually works. For data.table, it also contains the pointer address. Please skip that part next time.

Thank you! I will try around a little bit and make sure I paste a smaller dataset next time. :relaxed: Thanks for taking the time to answer!

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