svynls.Rd
Fits a nonlinear model by probability-weighted least squares. Uses
nls
to do the fitting, but estimates design-based standard errors with either
linearisation or replicate weights. See nls
for
documentation of model specification and fitting.
svynls(formula, design, start, weights=NULL, ...)
Nonlinear model specified as a formula; see nls
Survey design object
starting values, passed to nls
Non-sampling weights, eg precision weights to give more efficient estimation in the presence of heteroscedasticity.
Other arguments to nls
(especially, start
). Also
supports return.replicates
for replicate-weight designs and
influence
for other designs.
Object of class svynls
. The fitted nls
object is
included as the fit
element.
svymle
for maximum likelihood with linear predictors on
one or more parameters
set.seed(2020-4-3)
x<-rep(seq(0,50,1),10)
y<-((runif(1,10,20)*x)/(runif(1,0,10)+x))+rnorm(510,0,1)
pop_model<-nls(y~a*x/(b+x), start=c(a=15,b=5))
df<-data.frame(x=x,y=y)
df$p<-ifelse((y-fitted(pop_model))*(x-mean(x))>0, .4,.1)
df$strata<-ifelse(df$p==.4,"a","b")
in_sample<-stratsample(df$strata, round(table(df$strat)*c(0.4,0.1)))
sdf<-df[in_sample,]
des<-svydesign(id=~1, strata=~strata, prob=~p, data=sdf)
pop_model
#> Nonlinear regression model
#> model: y ~ a * x/(b + x)
#> data: parent.frame()
#> a b
#> 14.461 9.311
#> residual sum-of-squares: 560.6
#>
#> Number of iterations to convergence: 4
#> Achieved convergence tolerance: 3.99e-06
(biased_sample<-nls(y~a*x/(b+x),data=sdf, start=c(a=15,b=5)))
#> Nonlinear regression model
#> model: y ~ a * x/(b + x)
#> data: sdf
#> a b
#> 16.35 13.74
#> residual sum-of-squares: 105.7
#>
#> Number of iterations to convergence: 5
#> Achieved convergence tolerance: 5.098e-07
(corrected <- svynls(y~a*x/(b+x), design=des, start=c(a=15,b=5)))
#> Nonlinear survey regression model
#> model: y ~ a * x/(b + x)
#> design: Stratified Independent Sampling design (with replacement)
#> svydesign(id = ~1, strata = ~strata, prob = ~p, data = sdf)
#> a b
#> 14.453 9.569
#> weighted residual sum-of-squares: 556.7