title: SOLVED- lmer: Error in qr.default(X, tol = tol, LAPACK = FALSE) : NA/NaN/Inf in foreign function (arg 1)
body: <p>I am not very proficient in R and stumbled upon a problem I cannot seem to fix.</p>
  <p>I have some data in a dataframe (&quot;trials&quot;) from an economic exchange game for which I want to make some predictions. I load the data into <code>R</code> via:</p>
  <pre><code>trials &lt;- read.table(&quot;TrialsCG2.txt&quot;, sep=&quot;\t&quot;, header=T)
  </code></pre>
  <p><strong>Example data</strong> via <code>dput(head(trials,18))</code> for the first 18 rows, which includes some <code>Inf</code>values</p>
  <pre><code>structure(list(SubjectID = structure(c(86L, 86L, 86L, 86L, 86L, 
  86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L, 86L
  ), .Label = c(&quot;01HPG&quot;, &quot;01IVW&quot;, &quot;02BCH&quot;, &quot;02BLB&quot;, &quot;02LIM&quot;, &quot;02LRH&quot;, 
  &quot;02TBH&quot;, &quot;03AAG&quot;, &quot;03bss&quot;, &quot;04BAR&quot;, &quot;04BSR&quot;, &quot;05BEH&quot;, &quot;05BEL&quot;, 
  &quot;05FAF&quot;, &quot;05KBF&quot;, &quot;05rgf&quot;, &quot;05SBK&quot;, &quot;05ssw&quot;, &quot;06ANG&quot;, &quot;06HHG&quot;, 
  &quot;06HSP&quot;, &quot;06IEF&quot;, &quot;06IMA&quot;, &quot;06SBW&quot;, &quot;07BDI&quot;, &quot;07BRC&quot;, &quot;07BUS&quot;, 
  &quot;07FBU&quot;, &quot;07SAA&quot;, &quot;08FAV&quot;, &quot;08FIF&quot;, &quot;08IBA&quot;, &quot;08KMM&quot;, &quot;08WHH&quot;, 
  &quot;09HIC&quot;, &quot;09kaa&quot;, &quot;09KUR&quot;, &quot;09OIR&quot;, &quot;10GKC&quot;, &quot;10IMK&quot;, &quot;11IIA&quot;, 
  &quot;11SEB&quot;, &quot;11SSM&quot;, &quot;11WBG&quot;, &quot;12IAE&quot;, &quot;12IDG&quot;, &quot;12IMG&quot;, &quot;12LNC&quot;, 
  &quot;12SDM&quot;, &quot;12SMS&quot;, &quot;12WCO&quot;, &quot;13DOR&quot;, &quot;13GBR&quot;, &quot;13HKA&quot;, &quot;13WPA&quot;, 
  &quot;13ZMS&quot;, &quot;13ZRE&quot;, &quot;14HBG&quot;, &quot;14IMS&quot;, &quot;15DMC&quot;, &quot;15IHG&quot;, &quot;15KTR&quot;, 
  &quot;15SAC&quot;, &quot;15ser&quot;, &quot;15wmk&quot;, &quot;16BAS&quot;, &quot;16BTT&quot;, &quot;16SJS&quot;, &quot;17fga&quot;, 
  &quot;17HBV&quot;, &quot;17ICK&quot;, &quot;17ILJ&quot;, &quot;17KRA&quot;, &quot;18HAK&quot;, &quot;18HSB&quot;, &quot;18MMR&quot;, 
  &quot;19LNC&quot;, &quot;20BIP&quot;, &quot;20IBM&quot;, &quot;20KEK&quot;, &quot;20MKS&quot;, &quot;21BAP&quot;, &quot;21DMW&quot;, 
  &quot;21FSM&quot;, &quot;21HAR&quot;, &quot;21MAT&quot;, &quot;22BSB&quot;, &quot;22LDS&quot;, &quot;22lmr&quot;, &quot;23ERW&quot;, 
  &quot;23MSV&quot;, &quot;23uls&quot;, &quot;24BFM&quot;, &quot;24LCF&quot;, &quot;24SIC&quot;, &quot;25HCA&quot;, &quot;25IBG&quot;, 
  &quot;25SSM&quot;, &quot;26DMW&quot;, &quot;26emh&quot;, &quot;27DGJ&quot;, &quot;27IDH&quot;, &quot;27WNV&quot;, &quot;28BJT&quot;, 
  &quot;28HAS&quot;, &quot;28kmh&quot;, &quot;28MBM&quot;, &quot;28SGW&quot;, &quot;29FCJ&quot;, &quot;29GEJ&quot;, &quot;29KHF&quot;, 
  &quot;30BKH&quot;, &quot;31BRF&quot;, &quot;31LGR&quot;, &quot;31LWH&quot;), class = &quot;factor&quot;), lossPercent = c(8.695652174, 
  8.695652174, 8.695652174, 8.695652174, 8.695652174, 8.695652174, 
  8.695652174, 8.695652174, 8.695652174, 8.695652174, 8.695652174, 
  8.695652174, 8.695652174, 8.695652174, 8.695652174, 8.695652174, 
  8.695652174, 8.695652174), Neuro_Score = c(3L, 3L, 3L, 3L, 3L, 
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Female = c(0L, 
  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
  0L), Education = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L), Age = c(27L, 27L, 27L, 27L, 27L, 
  27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L
  ), Satisfaction = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L), LifeEvent = c(1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Ment_Score = c(3.67, 
  3.67, 3.67, 3.67, 3.67, 3.67, 3.67, 3.67, 3.67, 3.67, 3.67, 3.67, 
  3.67, 3.67, 3.67, 3.67, 3.67, 3.67), MentSelf_Score = c(3, 3, 
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), MentEmot_Score = c(3.25, 
  3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 3.25, 
  3.25, 3.25, 3.25, 3.25, 3.25, 3.25), MentEqui_Score = c(4, 4, 
  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), MentAffect_Score = c(4.67, 
  4.67, 4.67, 4.67, 4.67, 4.67, 4.67, 4.67, 4.67, 4.67, 4.67, 4.67, 
  4.67, 4.67, 4.67, 4.67, 4.67, 4.67), LPFS_Score = c(10L, 10L, 
  10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
  10L, 10L, 10L), SchemaED_Score = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), SchemaAB_Score = c(11L, 
  11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
  11L, 11L, 11L, 11L), SchemaMA_Score = c(10L, 10L, 10L, 10L, 10L, 
  10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L
  ), SchemaSI = c(12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
  12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), SchemaTotal_Score = c(38L, 
  38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 
  38L, 38L, 38L, 38L), RejectionTotal_Score = c(8.56, 8.56, 8.56, 
  8.56, 8.56, 8.56, 8.56, 8.56, 8.56, 8.56, 8.56, 8.56, 8.56, 8.56, 
  8.56, 8.56, 8.56, 8.56), UV1_Score = c(4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), UV2_Score = c(3L, 
  3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
  3L), UV3_Score = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L), UVTotal_Score = c(11L, 11L, 11L, 
  11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
  11L, 11L), currentPartner = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c(&quot;Spieler A&quot;, 
  &quot;Spieler B&quot;), class = &quot;factor&quot;), goodPartner = c(0L, 0L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L), 
      finalBalance = c(10L, 10L, 12L, 13L, 14L, 15L, 16L, 17L, 
      19L, 21L, 23L, 21L, 22L, 23L, 23L, 23L, 24L, 25L), goodPartnerBalance = c(10L, 
      10L, 12L, 13L, 14L, 15L, 16L, 17L, 19L, 21L, 23L, 29L, 29L, 
      29L, 29L, 29L, 30L, 31L), badPartnerBalance = c(12L, 14L, 
      14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 17L, 20L, 
      22L, 24L, 24L, 24L), investment = c(1L, 1L, 2L, 1L, 1L, 1L, 
      1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), sentBack = c(1L, 
      1L, 4L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 0L, 3L, 3L, 1L, 1L, 
      2L, 2L), win = c(0L, 0L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
      2L, -2L, 1L, 1L, 0L, 0L, 1L, 1L), round = 1:18, preCrash = c(TRUE, 
      TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), crash = c(FALSE, 
      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
      FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), initialBalance = c(10L, 
      10L, 10L, 12L, 13L, 14L, 15L, 16L, 17L, 19L, 21L, 23L, 21L, 
      22L, 23L, 23L, 23L, 24L), visibiltyBalance = c(TRUE, TRUE, 
      TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
      TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), postCrash = c(FALSE, 
      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
      FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), previousSentBack = c(NA, 
      1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 0L, 3L, 3L, 1L, 
      1L, 2L), perInvest = c(10, 10, 20, 8.33333333333333, 7.69230769230769, 
      7.14285714285714, 6.66666666666667, 6.25, 11.7647058823529, 
      10.5263157894737, 9.52380952380952, 8.69565217391304, 9.52380952380952, 
      9.09090909090909, 4.34782608695652, 4.34782608695652, 4.34782608695652, 
      4.16666666666667), ReInvestRatio = c(NA, 1, 2, 0.25, 0.5, 
      0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, Inf, 0.666666666666667, 
      0.333333333333333, 1, 1, 0.5)), row.names = c(NA, 18L), class = &quot;data.frame&quot;)    ``
  </code></pre>
  <p>`</p>
  <p>Now I want to use an <code>lmer</code> to predict the player's investment in each round, such as:</p>
  <pre><code>fit1 &lt;- lmer(log(investment + 0.5)~LPFS_Score+Education+visibiltyBalance+RejectionTotal_Score+(1|SubjectID), subset=preCrash==T)
  
  </code></pre>
  <p>which runs fine.</p>
  <p>I am dealing with a multi-round investment game where players send and receive money within each round. I'd like to use the <strong>ratio</strong> of the sum returned one round prior to the investment in a given round as a predictor.
  Computing the previously returned money as such:</p>
  <pre><code>
  previousSentBack[round == 1] &lt;- NA
  previousSentBack[round == 2] &lt;- sentBack[round == 1]
  previousSentBack[round == 3] &lt;- sentBack[round==2]
  previousSentBack[round == 4] &lt;- sentBack[round==3]
  previousSentBack[round == 5] &lt;- sentBack[round==4]
  previousSentBack[round == 6] &lt;- sentBack[round==5]
  previousSentBack[round == 7] &lt;- sentBack[round==6]
  previousSentBack[round == 8] &lt;- sentBack[round==7]
  previousSentBack[round == 9] &lt;- sentBack[round==8]
  previousSentBack[round == 10] &lt;- sentBack[round==9]
  previousSentBack[round == 11] &lt;- sentBack[round==10]
  previousSentBack[round == 12] &lt;- sentBack[round==11]
  previousSentBack[round == 13] &lt;- sentBack[round==12]
  previousSentBack[round == 14] &lt;- sentBack[round==13]
  previousSentBack[round == 15] &lt;- sentBack[round==14]
  previousSentBack[round == 16] &lt;- sentBack[round==15]
  previousSentBack[round == 17] &lt;- sentBack[round==16]
  previousSentBack[round == 18] &lt;- sentBack[round==17]
  
  trials$previousSentBack &lt;- previousSentBack
  </code></pre>
  <p>works fine as well.</p>
  <p><strong>Now comes the troubling variable</strong>. I compute the ratio as such:</p>
  <pre><code>ReInvestRatio = trials$ReInvestRatio
  
  ReInvestRatio &lt;- investment/previousSentBack
  
  trials$ReInvestRatio &lt;- ReInvestRatio
  </code></pre>
  <p>Using this newly computed variable in my <code>lmer</code> above gives me this error:</p>
  <blockquote>
  <p>Error in qr.default(X, tol = tol, LAPACK = FALSE) : NA/NaN/Inf in foreign function (arg 1)</p>
  </blockquote>
  <p>Doing some research on here and google, I have found the <code>NaN</code> and <code>Inf</code> to be the culprit, however I cannot seem to remove them. It should be noted that I am assuming that <code>NA</code> shouldn't be much of a problem for my <code>lmer</code> as some other predictors contain <code>NAs</code> aswell. I'd prefer to fix the variable without having to replace all <code>NA/NaN/Inf</code> with 0. However the replacing seems to be the problem in the first place.</p>
  <p>I tried the following code:</p>
  <pre><code>trials &lt;- do.call(data.frame, lapply(trials,function(x) replace(x, is.infinite(x), NA)))
  </code></pre>
  <p>and also:</p>
  <pre><code>is.na(trials$ReInvestRatio) &lt;- sapply(trials$ReInvestRatio, is.infinite)  
  
  is.na(trials$ReInvestRatio) &lt;- sapply(trials$ReInvestRatio, is.nan) 
  </code></pre>
  <p>Both visibly remove <code>NaN</code> and <code>Inf</code> from my dataset but they do not seem to do so &quot;internally&quot; as I still get the same error from the <code>lmer</code> and e.g. <code>sum(is.infinite(ReInvestRatio))</code> still lists several hundred cases.</p>
  <p>I have definitly reached the limit of my R-capabilities and would greatly appreciate some help!</p>
  <p>Kind regards,
  Max</p>
  <p><strong>Fix</strong></p>
  <p>Nothing really mathematical after all:</p>
  <p>I was using <code>attach(trials)</code> hence why none of my variables in the <code>lmer</code> feature any <code>trials$</code> preface. I was expecting this to work equally for <code>ReInvestRatio</code> but apparently it doesn't. When I put <code>ReInvestRatio</code> into <code>lmer(log(investment +0.5)~ReInvestRatio +(1|SubjectID))</code> R keeps pulling the values from my originally created variable in the workspace. This one is not (or at least in my case was not ) cleaned of any <code>Inf</code> etc. <code>trials$ReInvestRatio</code> as in <code>lmer(log(investment +0.5)~trials$ReInvestRatio +(1|SubjectID))</code>
  works in the model as this seems to target the correct, cleaned <code>ReInvestRatio</code> .</p>