Search code examples
rregressionnonlinear-optimizationnlsnon-linear-regression

How to use the 'weights' in the nls (non-linear least squares) function in R?


My question is on how to correctly interpret (and use) the 'weights' input variable in the nls function of R for non-linear weighted least squares regression.

The solution for solving the unknown parameters in weighted least squares theory is: enter image description here

From this the variable P is the weight square matrix of size (NxN) where N is the number of data observations.

However, when I look at the nls documentation in R found here, it says the 'weights' to be input is a vector.

This has me puzzled since based on my understanding, the weights should be a square matrix. Some insights with those who have a better understanding is appreciated.


Solution

  • Weight variable in regression, is a measure of how important an observation is to your model due to different reasons (eg. may be in terms of reliability of measurement or inverse of variance estimate). Therefore, some observations may be more important/ weigh higher than others.

    Weight vector *P*, in matrix notation converts to a diagonal matrix enter image description here for i in {1,2,3...n,} both represents the same thing (i.e. weight of ith observation). For nls package in R you need to supply weights in vector form.

    Also, it should be noted that, weighted least squares is a special variant of generalized least squares in which we use weights to counter the heteroskedasticity. If the residuals are correlated for observations, perhaps a general model might be suitable.

    PS: Cross validated would be the right place to get better detailed answer. Also, It seems to be memory efficient to store a vector rather than a matrix as the number of observations grows