Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per equation weighting #5

Open
JanMarvin opened this issue Dec 26, 2020 · 6 comments
Open

Per equation weighting #5

JanMarvin opened this issue Dec 26, 2020 · 6 comments

Comments

@JanMarvin
Copy link
Owner

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
Copy link
Owner Author

@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
Copy link
Owner Author

JanMarvin commented Jan 1, 2021

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
Copy link
Owner Author

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
Copy link

LfeiXie commented Jan 10, 2021 via email

@JanMarvin
Copy link
Owner Author

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
Copy link

LfeiXie commented Jan 10, 2021

Thanks, the newly gh_issue_5 has been installed successfully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants