title: How do I make variable weights dynamic in lmer for loop
body: <p>I want to be able to input the variable name that I'll be using in the &quot;weights&quot; option in the lmer function. So then I can change the dataset, and cycle through the &quot;weights&quot; and pull the correct variable.</p>
  <p>I want to pull the correct column for weights within the for loop.
  So for y, the equation would be:
  lmer(y~x+(1|study), weights = weight.var)
  And y1:
  lmer(y1~x+(1|study),weights = weight.var1)
  So I named the weighting variables (weight.opt), then want to use them in the formula within the for loop. I can use &quot;as.formula&quot; to get the formula working and connected to the dataset, but I'm not sure how to do something similar with the weights.</p>
  <pre><code>x &lt;- rnorm(300,0,1)
  y &lt;- x*rnorm(300,2,0.5)
  y1 &lt;- x*rnorm(300,0.1,0.1)
  study &lt;- rep(c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;),each = 100)
  weight.var &lt;-  rep(c(0.5,2,4),each = 100)
  weight.var1 &lt;-  rep(c(0.1,.2,.15),each = 100)
  library(lme4)
  dataset &lt;- data.frame(x,y,y1,study,weight.var,weight.var1)
  resp1 &lt;- c(&quot;y&quot;,&quot;y1&quot;)
  weight.opt &lt;- c(&quot;weight.var&quot;,&quot;weight.var1&quot;)
  for(i in 1:2){
    lmer(as.formula(paste(resp1[i],&quot;~x+(1|study)&quot;)),weights = weight.opt[i],data = dataset)
  }
  </code></pre>