JanMarvin / nlsur

R package for the estimation of nonlinear least squares for equation systems
Other
15 stars 4 forks source link

Per equation weighting #5

Open JanMarvin opened 3 years ago

JanMarvin commented 3 years ago

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.

JanMarvin commented 3 years ago

@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 
JanMarvin commented 3 years ago

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

JanMarvin commented 3 years ago

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.

LfeiXie commented 3 years ago

I have seen the issues “Per equation weighting” in Github and found that you have implemented the the support of per eqution weighted. Based on the example you listed in the issue, I used the SAS 9.4 software recalculated it. Overall, the weighted SUR in SAS gave the different patameter estimates, where I thought that the SAS considered the correlation between the two sub-models since the weighted regression with single model (namely OLS) generated different estimates from the weighted SUR with two models. On the contray, in R software, the estimates of single model and SUR model (including two sub-models) were the same. For more informations, please see the SAS codes in the attached file. On the other side, I wanted to install the JanMarvin/nlsur/tree/gh_issue_5 package to test the improvement as you worked. Unfortunately, it failed. Local installation and online installation were tried, but it did not work. In the local installation, the error of “ cannot open compressed file 'nlsur-gh_issue/DESCRIPTION', probable reason 'No such file or directory' Error in install.packages : cannot open the connection ” was given. I can use the code “devtools::install_github("JanMarvin/nlsur",force = T)” to install the nlsur package which without the newly functions. Would you please give me some advices to install the newly nlsur package in my R software? Now, what I can do was to calculate your examples in SAS, and do a comparison.

JanMarvin commented 3 years ago

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")
LfeiXie commented 3 years ago

Thanks, the newly gh_issue_5 has been installed successfully