Per equation weighting
JanMarvin opened this issue · comments
It should be possible to apply per equation weights for nlsur. Something like
nlsur(eqns = model, data = dd, weights = list (w_eq1, w_eq2)
.
If length (w) > 1 construct the equations with the corresponding weights. Later it should be possible to keep the weight vector intact to keep this change simple.
@LfeiXie I have implemented inital support of per equation weighting in the linked branch. It was a bit trickier than I expected, though the following now works. Word of warning I haven't given much though to the algebra (specially regarding the robust estimation) and the solution is not entirely the nicest possible. Though, I probably will not have much time to spend on this and would appreciate, if you could have a look.
# simple linear regression
> fmls <- list("mpg ~ b_00 + b_01 * cyl",
+ "mpg ~ b_10 + b_11 * hp")
# NEW weighting eq1 with am and eq2 with wt
> coef(nlsur(fmls, data = mtcars, weights = c("am", "wt")))
startvalues created with val = 0
b_00 b_01 b_10 b_11
41.04893617 -3.28085106 28.54864505 -0.06249413
> coef(lm(mpg ~ cyl, data = mtcars, weights = am))
(Intercept) cyl
41.048936 -3.280851
> coef(lm(mpg ~ hp, data = mtcars, weights = wt))
(Intercept) hp
28.54864505 -0.06249413
# OLD weighting with: am
> coef(nlsur(fmls, data = mtcars, weights = c("am")))
startvalues created with val = 0
b_00 b_01 b_10 b_11
41.04893617 -3.28085106 31.84250125 -0.05873409
> coef(lm(mpg ~ cyl, data = mtcars, weights = am))
(Intercept) cyl
41.048936 -3.280851
> coef(lm(mpg ~ hp, data = mtcars, weights = am))
(Intercept) hp
31.84250125 -0.05873409
# OLD weighting with: wt
> coef(nlsur(fmls, data = mtcars, weights = c("wt")))
startvalues created with val = 0
b_00 b_01 b_10 b_11
36.69247157 -2.74918978 28.54864505 -0.06249413
> coef(lm(mpg ~ cyl, data = mtcars, weights = wt))
(Intercept) cyl
36.69247 -2.74919
> coef(lm(mpg ~ hp, data = mtcars, weights = wt))
(Intercept) hp
28.54864505 -0.06249413
# OLD weighting off
> coef(nlsur(fmls, data = mtcars))
startvalues created with val = 0
b_00 b_01 b_10 b_11
37.88457649 -2.87579014 30.09886054 -0.06822828
> coef(lm(mpg ~ cyl, data = mtcars))
(Intercept) cyl
37.88458 -2.87579
> coef(lm(mpg ~ hp, data = mtcars))
(Intercept) hp
30.09886054 -0.06822828
I have left comments in the commit. Log Likelihood is wrong with multiple weighting variables. Currently only the first variable is selected. Should be easy to fix though
I have added further improvements. Previously summary
was broken as well as robust
. This is fixed now and I have added tests to make sure that they work as expected. logLik
is identical to the nls
value, but might differ for the multiple equation weighting, but I haven't checked this yet. Again, maybe you could have a look.
No worries! (I have removed the unrelated parts from your answer.) You should be able to install using this command
devtools::install_github("JanMarvin/nlsur", ref = "gh_issue_5")
Thanks, the newly gh_issue_5 has been installed successfully