How to fix “object 'i' not found” in R for

2019-08-23 02:35发布

I have to run thousands of models with thousands of dependent variables (one model with one denpent variables. The depent variables stand for different genes). So I selected "foreach" and "doParallel" packages to increase my analysis speed. And I am using "lqmm" package, but I kept facing the error. I am thinking there might be way(s) to deal with this issue by editing some statements in foreach function. I have searched all related questions in this aspect, but still get stuck. I am showing you my small example dataset, which has 10 dependent variables, id, and AGE. And how I analyzed the data via "lqmm" package.

data structure

data <- structure(list(cg10988531 = c(0.630153325796179, 0.770775130608884, 0.757996617859709, 0.737234613436505,0.742740307295303, 0.731307142647318, 
                              0.626026428615932, 0.594504765421005, 0.779113238010263, 0.713868766042849,0.706444422115073, 0.669625341597452, 
                              0.601698927610885, 0.574355877044004, 0.638079269569724, 0.765244105735255, 0.688512738015497, 0.730034983382431,
                              0.707581103853214, 0.763488945560203, 0.693422945711283, 0.698944907195166, 0.662794336656439, 0.653029167475804, 
                              0.609613861391179, 0.664066366967186, 0.632892686244689, 0.699939942574019, 0.715693384583515, 0.74974525367546, 
                              0.624733216153732, 0.688377236771211, 0.750696109217667, 0.697237629091028,0.65091369395634, 0.706668886011513, 
                              0.684333901460328, 0.813683309599456, 0.745748967041623, 0.639381929619178, 0.654721895573134, 0.666463400582323,
                              0.70667491376427, 0.746367235659818, 0.6632832067251, 0.680279510606564, 0.754000431106847, 0.767376305276565, 
                              0.667158917606397, 0.756439420207059,0.740032992532744), 
               cg13938959 = c(0.458607545675712, 0.557098217896965,0.586313882437927, 0.746117629791949, 0.696079819944921, 0.666509774328786, 
                              0.693363151855891, 0.67089837487101, 0.753839024463411, 0.63977560248978, 0.566910700769526, 0.625258301600419, 
                              0.629071963230184, 0.710982856857208, 0.697674730808512, 0.618047142180818, 0.627754222216683, 0.625909176325358, 
                              0.632685134986694, 0.678910841652927, 0.682434115674639, 0.713744571236278, 0.773109601323697, 0.736397966300478, 
                              0.677534986185499, 0.706543519713892, 0.712717606108532, 0.53327854172464, 0.538268403378311, 0.47519306007491, 
                              0.615367957269029, 0.636335187709495, 0.711919314307419, 0.599063358291259, 0.55619331479486, 0.663151168112034, 
                              0.628456584923881, 0.666088239127848, 0.654254541945952, 0.578132824156803, 0.606554704946875, 0.337423563256121, 
                              0.693042943171903, 0.689686711153191, 0.651141276711133, 0.660955634401375, 0.614220968171896, 0.614220968171896, 
                              0.541175441205358, 0.595377047420423, 0.616999063128963), 
               cg12445832 = c(0.301446318961791, 0.405504627984548, 0.351258572202212, 0.25666730433032, 0.430751142201698, 0.412838629728172, 
                              0.465921298852488, 0.445062907354711, 0.532085943486953, 0.361946514048243,0.361049462038908, 0.346688422388766, 
                              0.350700718054665, 0.394340901173898, 0.358870594964594, 0.343630108438937, 0.296279234775227, 0.305462713739599, 
                              0.38766508953414, 0.367065755578129, 0.333919368626454, 0.441162190342006, 0.491284178477365, 0.38255389269514, 
                              0.458166253984738, 0.47901431461763, 0.452596880873569, 0.306674062618139, 0.356995660929631, 0.260657296978482, 
                              0.393622347592022, 0.385605568267487, 0.34013686053546, 0.312252888960046, 0.35208832007973, 0.356070835312708, 
                              0.383080114190242, 0.41317716362835, 0.40060379208126, 0.381017559185138, 0.376804381526138, 0.212111583668084, 
                              0.337434996625902, 0.368275347560357, 0.409776694675723, 0.41630527528389, 0.364390439594444, 0.364390439594444, 
                              0.345339957409291, 0.405446535493646, 0.399822625294935), 
               cg23999112 = c(0.48867870216977, 0.58497407271918,   0.589100777111143, 0.5628638520904, 0.659555993688058, 0.646205895594478, 
                              0.659539460202759, 0.645572535257309, 0.70192669199497, 0.647862184898744, .534177998606812, 0.60969457970853, 
                              0.66830132324488, 0.713644208758589, 0.71418023682956, 0.577079890643466, 0.54377028096491, 0.626979524653954, 
                              0.636256226325841, 0.607319235417044, 0.601796739301619, 0.715432444026478, 0.71211097471943, 0.66828871899325, 
                              0.644851796104693, 0.707480135424217, 0.678961567424962, 0.51720747267316, 0.545988177053836, 0.467868716026543, 
                              0.60959328639641, 0.642437494047478, 0.673981868734163, 0.576407682967781, 0.558901518320624, 0.626123516238721, 
                              0.594064350078027, 0.616168877786708, 0.637450464033479, 0.601066833781401, 0.620786633516949, 0.548522187481177, 
                              0.638166406311283, 0.64984642665887, 0.625468985724768, 0.618947322610633, 0.599560980371383, 0.599560980371383, 
                              0.564092270452233, 0.59988902736034, 0.635161844972895), 
               cg04195702 = c(0.763102672217557, 0.865848631852404, 0.873438720601516, 0.874733307511421, 0.855922583572087, 0.840863173836431, 
                              0.807150569349922, 0.793950783347439, 0.874553383856645, 0.860915024511776, 0.830139823118752, 0.883880899348958, 
                              0.773473144601942, 0.756333766294769, 0.810577394992373, 0.832662474258448, 0.858072101327281, 0.869641660618307, 
                              0.853689875691372, 0.864828331580798, 0.833482902098146, 0.847415544052399, 0.844011917363475, 0.732787808963489,
                              0.775260047292982, 0.810103601451938, 0.797262683704226, 0.838726844458184, 0.831604173558953, 0.877614941534157, 
                              0.779215352934515, 0.8541530991143, 0.81128087194988, 0.860027854432974, 0.789627785524284, 0.848705839667419, 
                              0.854675301944011, 0.853577200724298,0.88177462127997, 0.813830117888789, 0.805570011195677, 0.792543661926182, 
                              0.826203366330992, 0.82599714658786, 0.81021956732635, 0.800002513248565, 0.873561838258328, 0.85619757768318, 
                              0.794564485624937, 0.872680181751787, 0.87946581874139), 
               cg13371836 = c(0.0291186102805069, 0.0238233961286981, 0.0265859919325266, 0.0183402573715675, 
                              0.0321910695742471, 0.0335832266715426, 0.0647434965847987, 0.0653128907914646, 
                              0.0322189120600243, 0.0203016241299304, 0.0768094534711965, 0.0202424090965888, 
                              0.0289203629887202, 0.0468028328030381, 0.0242929659173314, 0.0519840049215626, 
                              0.0488586956521739, 0.0180108383806184, 0.0319065469444687, 0.0326999862315847, 
                              0.0222795497185741, 0.0455423237850016, 0.0333980582524272, 0.0339228022445162, 
                              0.0234676007005254, 0.0450824996751981, 0.0690699975265892, 0.0500758725341426, 
                              0.071586892709519, 0.0220390611001613,  0.0192378295029004, 0.0354765791787015, 
                              0.030931004796458, 0.0351541077108698,  0.0239475029827851, 0.0357189757058437, 
                              0.0285275899301017, 0.0280098616627859, 0.0364431486880466, 0.0615384615384615, 
                              0.0364058869093726, 0.0590674328944497, 0.0425652667423383, 0.0389362166885103, 
                              0.0521330027338038, 0.0457806182339898, 0.0263431542461005, 0.0330086244338276, 
                              0.0284252975312285, 0.0306351183063512, 0.0288387196536261), 
               cg04407431 = c(0.026238686506411, 0.0173179161446973,  0.0418749064527286, 0.0166560505678465, 
                              0.0178980594455554, 0.0416984049098105, 0.0311635759194727, 0.0347261001694128, 
                              0.0293909070277559, 0.0193171110662548, 0.031389852462566, 0.0261491547280799, 
                              0.0258117181458199, 0.0465242397423142, 0.0218338310795177, 0.0277771770307244, 
                              0.0280971430186672, 0.0103760234787818, 0.0212683772991847, 0.0209662278275642, 
                              0.0196114804575482, 0.0190108334096533, 0.0256653359921233, 0.0274245060604575, 
                              0.0173220017110573, 0.0210277220810826, 0.0286147260864303, 0.0202240377814423, 
                              0.0227532455395287, 0.0178633594304782, 0.0224181095847365, 0.0201870335251303,
                              0.0194393843096241, 0.0185589219497916, 0.0264609092409277, 0.0165793088178078, 
                              0.0208786631603221, 0.0244406242167872, 0.0218096542857304, 0.0285192911352428, 
                              0.0170304212445144, 0.0206186175557356, 0.0218617487458218, 0.0288665679398945, 
                              0.0198857999109443, 0.018095428397374,  0.0304948291674977, 0.0211101434186806, 
                              0.023007756373759, 0.0159391050978858, 0.0193149461878329), 
               cg13565718 = c(0.729050125912539, 0.805016326427036,  0.846734261549655, 0.853247138884453, 
                              0.813342549943543, 0.834371270304804,  0.858238578557851, 0.787663564048398, 
                              0.848570967554931, 0.882847404647568, .801409846882799, 0.837188145677318, 
                              0.780504006354445, 0.741897603534226, 0.819686412059789, 0.818657128759334, 
                              0.746648711507093, 0.862540524277664, 0.866175621605476, 0.823859736155615, 
                              0.761461849568792, 0.822525505589669, 0.834678623548743, 0.721457684777424, 
                              0.741989864697983, 0.815204713331137,0.789234942465454, 0.844031332087611, 
                              0.817242294541099, 0.848423226934653, 0.728273906578194, 0.823075394021172, 
                              0.805866383430445, 0.839615222517935, 0.71685569945197, 0.820429595571164, 
                              0.840106007799023, 0.818099714213087,  0.777425553091675, 0.813546614965661, 
                              0.828294862459864, 0.787729828727773, 0.75282308456123, 0.803864943938219, 
                              0.834572174277308, 0.812955016067581, 0.795251022387645, 0.763886659757821, 
                              0.774915842474854, 0.818396531990409, 0.830110061586548), 
               cg01097950 = c(0.683129148404905, 0.791000064217921,  0.760295373667655, 0.819426628745148, 
                              0.752263023336887, 0.731700710496854, 0.697323180667231, 0.721317005854915, 
                              0.834407051044966, 0.822429888816758, 0.718009343249145, 0.80794652118361, 
                              0.646865230819883, 0.663794205604566, 0.715046762040978, 0.74808965931323, 
                              0.770445009433441, 0.806086611920311, 0.796477508544596, 0.755238593211042, 
                              0.780145785762232, 0.698002875347674, 0.733917180663015, 0.683123051645205, 
                              0.677755620675888, 0.71588617330788,  0.690923626281446, 0.753981457087644, 
                              0.757025921847667, 0.809459493581119, 0.679910458035062, 0.723489024812393, 
                              0.756350418609128, 0.766332234156038, 0.704092968070338, 0.763813564494105, 
                              0.765173569742141, 0.790883066082925, 0.791565835160566, 0.683795242620325, 
                              0.71751952796292, 0.704347616408368, 0.755572748164316, 0.753804556793614, 
                              0.762774500268684, 0.741686403998303, 0.762630534955273, 0.762869319303126, 
                              0.676179564347382, 0.770671276725573, 0.74944955060519), 
               cg08900511 = c(0.686376875653975, 0.789024511780223, 0.823737803125299, 0.82181170933402, 
                              0.817690805147867, 0.807925756678069, 0.782381183272376, 0.769842697570726, 
                              0.781236956749778, 0.824122844210498, 0.799240100662282, 0.795510390339352, 
                              0.738198496826981, 0.752385547071581, 0.770505066661016, 0.820657679313902, 
                              0.757692611716765, 0.795989585734436, 0.834438700339506, 0.788554253456023, 
                              0.78742251213339, 0.834202351579177,  0.803214162513637, 0.7247476993985, 
                              0.698360167660503, 0.793533405050319, 0.793261906127238, 0.762287525381365, 
                              0.799383235679556, 0.828065943079498, 0.747745316477367, 0.783783615168347, 
                              0.816949826208582, 0.767062186814432, 0.742211777271421, 0.817136138854601, 
                              0.807432209770604, 0.830861162212809, 0.827261332268775, 0.791354271553353, 
                              0.805097631407063, 0.776604170725837, 0.780539811065328, 0.802100500115258, 
                              0.782316691245256, 0.787020228721485, 0.780982638040595, 0.796511801615241, 
                              0.737236165563412, 0.807935705658302, 0.790120005778588), 
               id = c(78, 80, 80, 82, 88, 88, 97, 97, 97, 100, 100, 100, 101, 101, 101, 103, 103, 104, 104, 113, 113, 114, 
                      119, 121, 127, 139, 139, 148, 148, 148, 149, 152, 156, 159, 164, 171, 171, 174, 174, 175, 175, 182, 
                      185, 185, 186, 186, 188, 188, 189, 192, 192), AGE = c(83, 75, 71, 77, 82, 79, 71, 67, 78, 73, 77, 80, 
                      79, 85, 82, 78, 76, 71, 76, 74, 81, 72, 81, 78, 88, 74, 72, 74, 77, 83, 77, 62, 77, 69, 75, 68, 72, 
                      79, 82, 81, 84, 78, 90, 86, 74, 80, 83, 86, 79, 86, 82)), 
               .Names = c("cg10988531", "cg13938959", "cg12445832", "cg23999112", "cg04195702", "cg13371836", "cg04407431", 
                          "cg13565718", "cg01097950", "cg08900511", "id", "AGE"), 
               row.names = c(66L, 368L, 1184L, 595L, 698L, 699L, 1128L, 1131L, 1265L, 371L, 1107L, 1264L, 11L, 147L, 882L, 
                             1064L, 1066L, 662L, 663L, 610L, 1268L, 295L, 880L, 57L, 140L, 1000L, 1008L, 999L, 1006L, 1266L, 
                             78L, 228L, 291L, 232L, 91L, 795L, 801L, 1226L,  1233L, 156L, 160L, 648L, 329L, 337L, 1002L, 1005L, 433L, 435L, 
                              138L, 407L, 416L), class = "data.frame")                                                   

load packages

library(foreign)
library(MASS)
library(lqmm)
library(foreach)
library(doParallel)
library(tidyverse)

lqmm package

cl <- makeCluster(5)
registerDoParallel(cl)
results <- foreach (i=1:10,.combine='rbind') %dopar% {
  library(lqmm)
  model <- lqmm(data[,i] ~ AGE)), random=~1, group=id, data=data)
exposure="AGE"
pvalue=summary(model)$tTable[2.5]
want <- cbind(exposure, pvalue)
}
stopCluster(cl)

Then, there is the error: Error in {: task 1 failed - "object 'i' not found" Since my data were repeated measured, "lqmm" is better than "MASS" package.

I tried to put "lqmm" in the foreach function directly. See below, please.

cl <- makeCluster(5)
registerDoParallel(cl)
results <- foreach (i=1:10,.combine='rbind',.package='lqmm') %dopar% {
  model <- lqmm(data[,i] ~ AGE)), random=~1, group=id, data=data)
exposure="AGE"
pvalue=summary(model)$tTable[2.5]
want <- cbind(exposure, pvalue)
}
stopCluster(cl)

But I still got the same error.

What I expect to get is the same dataset which has 10 rows, and 2 columns. The column names are "exposure" and "pvalue".

Can someone help me with that?

Thanks a lot!

0条回答
登录 后发表回答