investr icon indicating copy to clipboard operation
investr copied to clipboard

Take into account weights when using weighted linear or nonlinear least squares

Open tomwenseleers opened this issue 6 years ago • 2 comments

The help file of investr seems to indicate that weights are ignored when using weighted linear (lm) or nonlinear least squares analysis (nls). Would it be hard to take into account weights, e.g. if one uses weights to deal with heteroscedastic errors? I understand that this should be straightforward, right, looking at https://stats.stackexchange.com/questions/175127/prediction-intervals-with-heteroscedasticity and http://weightinginbayesianmodels.github.io/poctcalibration/over_tutorials.html http://weightinginbayesianmodels.github.io/poctcalibration/calib_tut4_curve_ocon.html http://weightinginbayesianmodels.github.io/poctcalibration/calib_tut5_precision_ocon.html http://weightinginbayesianmodels.github.io/poctcalibration/AMfunctions.html#sdXhat ? Would it require a big change in the code to take into account weights (in my case I was using a model where variance was a power function of the mean, resulting in weights = 1/variance = 1/(fitted vals^power in an nls model)?

tomwenseleers avatar Mar 28 '18 09:03 tomwenseleers

Just realise that probably the easiest way to support weights in lm's or nls models would be to multiply both the dependent variable and each of the predictors with the sqrt(weights) if weights are given, since weighted least squares minimizes sum( w * (Y - X %% beta)^2 ) = sum_i( (sqrt(w[i]) * X[i, ] %% beta - sqrt(w[i]) * Y[i])^2 )

tomwenseleers avatar Mar 28 '18 13:03 tomwenseleers

Right, the weights argument is currently ignored, but I will try to work on this as well once the semester ends in a couple of weeks!

bgreenwell avatar Mar 28 '18 14:03 bgreenwell