title: R: lm() result differs when using `weights` argument and when using manually reweighted data
body: <p>In order to correct heteroskedasticity in error terms, I am running the following weighted least squares regression in R :</p>
  
  <pre><code>#Call:
  #lm(formula = a ~ q + q2 + b + c, data = mydata, weights = weighting)
  
  #Weighted Residuals:
  #     Min       1Q   Median       3Q      Max 
  #-1.83779 -0.33226  0.02011  0.25135  1.48516 
  
  #Coefficients:
  #             Estimate Std. Error t value Pr(&gt;|t|)    
  #(Intercept) -3.939440   0.609991  -6.458 1.62e-09 ***
  #q            0.175019   0.070101   2.497 0.013696 *  
  #q2           0.048790   0.005613   8.693 8.49e-15 ***
  #b            0.473891   0.134918   3.512 0.000598 ***
  #c            0.119551   0.125430   0.953 0.342167    
  #---
  #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  
  #Residual standard error: 0.5096 on 140 degrees of freedom
  #Multiple R-squared:  0.9639,   Adjusted R-squared:  0.9628 
  #F-statistic: 933.6 on 4 and 140 DF,  p-value: &lt; 2.2e-16
  </code></pre>
  
  <p>Where "weighting" is a variable (function of the variable <code>q</code>) used for weighting the observations. <code>q2</code> is simply <code>q^2</code>.</p>
  
  <p>Now, to double-check my results, I manually weight my variables by creating new weighted variables :</p>
  
  <pre><code>mydata$a.wls &lt;- mydata$a * mydata$weighting
  mydata$q.wls &lt;- mydata$q * mydata$weighting
  mydata$q2.wls &lt;- mydata$q2 * mydata$weighting
  mydata$b.wls &lt;- mydata$b * mydata$weighting
  mydata$c.wls &lt;- mydata$c * mydata$weighting
  </code></pre>
  
  <p>And run the following regression, without the weights option, and without a constant - since the constant is weighted, the column of 1 in the original predictor matrix should now equal the variable weighting:</p>
  
  <pre><code>Call:
  lm(formula = a.wls ~ 0 + weighting + q.wls + q2.wls + b.wls + c.wls, 
  data = mydata)
  
  #Residuals:
  #     Min       1Q   Median       3Q      Max 
  #-2.38404 -0.55784  0.01922  0.49838  2.62911 
  
  #Coefficients:
  #         Estimate Std. Error t value Pr(&gt;|t|)    
  #weighting -4.125559   0.579093  -7.124 5.05e-11 ***
  #q.wls    0.217722   0.081851   2.660 0.008726 ** 
  #q2.wls   0.045664   0.006229   7.330 1.67e-11 ***
  #b.wls    0.466207   0.121429   3.839 0.000186 ***
  #c.wls    0.133522   0.112641   1.185 0.237876    
  #---
  #Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  
  #Residual standard error: 0.915 on 140 degrees of freedom
  #Multiple R-squared:  0.9823,   Adjusted R-squared:  0.9817 
  #F-statistic:  1556 on 5 and 140 DF,  p-value: &lt; 2.2e-16
  </code></pre>
  
  <p>As you can see, the results are similar but not identical. Am I doing something wrong while manually weighting the variables, or does the option "weights" do something more than simply multiplying the variables by the weighting vector?</p>