title: "weighted" regression in R
body: <p>I have created a script like the one below to do something I called as "weighted" regression:</p>
  
  <pre><code>library(plyr)
  
  set.seed(100)
  
  temp.df &lt;- data.frame(uid=1:200,
                        bp=sample(x=c(100:200),size=200,replace=TRUE),
                        age=sample(x=c(30:65),size=200,replace=TRUE),
                        weight=sample(c(1:10),size=200,replace=TRUE),
                        stringsAsFactors=FALSE)
  
  temp.df.expand &lt;- ddply(temp.df,
                          c("uid"),
                          function(df) {
                            data.frame(bp=rep(df[,"bp"],df[,"weight"]),
                                       age=rep(df[,"age"],df[,"weight"]),
                                       stringsAsFactors=FALSE)})
  
  temp.df.lm &lt;- lm(bp~age,data=temp.df,weights=weight)
  temp.df.expand.lm &lt;- lm(bp~age,data=temp.df.expand)
  </code></pre>
  
  <p>You can see that in <code>temp.df</code>, each row has its weight, what I mean is that there is a total of 1178 sample but for rows with same <code>bp</code> and <code>age</code>, they are merge into 1 row and represented in the <code>weight</code> column.</p>
  
  <p>I used the <code>weight</code> parameters in the <code>lm</code> function, then I cross check the result with another dataframe that the <code>temp.df</code> dataframe is "expanded". But I found the <code>lm</code> outputs different for the 2 dataframe.</p>
  
  <p>Did I misinterpret the <code>weight</code> parameters in <code>lm</code> function, and can anyone let me know how to I run regression properly (i.e. without expanding the dataframe manually) for a dataset presented like <code>temp.df</code>? Thanks.</p>