Title: | Estimating Nonlinear Least Squares for Equation Systems |
---|---|
Description: | "Estimation of Nonlinear Least Squares (NLS), Feasible Generalized NLS (FGNLS) and Iterative FGNLS (IFGNLS) for Equation Systems." |
Authors: | Jan Marvin Garbuszus |
Maintainer: | Jan Marvin Garbuszus <[email protected]> |
License: | MIT + file LICENCE |
Version: | 0.8 |
Built: | 2024-11-11 05:15:07 UTC |
Source: | https://github.com/JanMarvin/nlsur |
.nlsur()
is a function for estimation of a non-linear seemingly
unrelated regression model in R.
.nlsur( eqns, data, startvalues, S = NULL, robust = robust, nls = FALSE, fgnls = FALSE, ifgnls = FALSE, qrsolve = FALSE, MASS = FALSE, trace = FALSE, eps = eps, tau = tau, maxiter = maxiter, tol = tol, initial = initial )
.nlsur( eqns, data, startvalues, S = NULL, robust = robust, nls = FALSE, fgnls = FALSE, ifgnls = FALSE, qrsolve = FALSE, MASS = FALSE, trace = FALSE, eps = eps, tau = tau, maxiter = maxiter, tol = tol, initial = initial )
eqns |
is can be a single equation or a equation system. If eqns is a
single equation it will internally be converted to a list. Estimation of a
single equation might as well be done using |
data |
is the data set on which the equation is applied. This can be of
every type |
startvalues |
is a vector of initial start values. For |
S |
is a weighing matrix used for estimation in Feasible Generalized
Non-Linear Least Squares (FGNLS) and Iterative FGNLS. For |
robust |
should a robust standard error be calculated |
nls |
is a logical and default if estimation is done for NLSUR or NLS. |
fgnls |
is a logical and must be set, if estimation is done for FGNLS.
This is called in a function called |
ifgnls |
is a logical and must be set, if estimation is done for ifgnls.
This is called in a function called |
qrsolve |
is a logical, if TRUE |
MASS |
is a logical, if TRUE |
trace |
is a logical. If TRUE the current iterations SSR is called. |
eps |
the epsilon used for convergence in nlsur(). Default is 1e-5. |
tau |
is another convergence variable. Default is 1e-3. |
maxiter |
maximum number of iterations |
tol |
qr.solves tolerance for detecting linear dependencies. |
initial |
logical if initial calculation set. used to avoid calculation of svd numerous times |
nlsur is a function for estimation of a non-linear least squares
(NLS). In addition to nls()
it is capable of estimation of system of
equations. This estimation is done in a non-linear seemingly unrelated
regression approach.
Bates, D. M. and Watts, D. G. (1988) Nonlinear Regression Analysis and Its Applications, Wiley
Gallant, A. Ronald (1987): Nonlinear Statistical Models. Wiley: New York
Estimation of an Almost-Ideal Demand System using nlsur().
ai(w, p, x, z, a0 = 0, data, scale = FALSE, logp = TRUE, logexp = TRUE, ...)
ai(w, p, x, z, a0 = 0, data, scale = FALSE, logp = TRUE, logexp = TRUE, ...)
w |
character vector of m budgetshares used for estimation. |
p |
character vector of m prices. |
x |
single character vector of total expenditure. |
z |
character vector of k demographic variables. |
a0 |
start value for translog price index. |
data |
data.frame containing the variables. |
scale |
logical if TRUE Rays (1983) scaling is used. |
logp |
logical if prices are log prices. |
logexp |
logical if expenditure is log expenditure. |
... |
additional options passed to nlsur |
Deaton, Angus S., Muellbauer, John: An Almost Ideal Demand System, The American Economic Review 70(3), American Economic Association, 312-326, 1980
Ray, Ranjan: Measuring the costs of children: An alternative approach, Journal of Public Economics 22(1), 89-102, 1983
qai and ai.model
Modelfunction to create Deatons and Muellbauers (1980) famous Almost-Ideal Demand System or the Quadratic Almost-Ideal Demand System by Banks et al. (1997).
ai.model( w, p, exp, alph0 = 10, logp = TRUE, logexp = TRUE, priceindex = "translog", modeltype = "AI", ray = FALSE, demogr )
ai.model( w, p, exp, alph0 = 10, logp = TRUE, logexp = TRUE, priceindex = "translog", modeltype = "AI", ray = FALSE, demogr )
w |
character vector of m budgetshares used for estimation. |
p |
character vector of m prices. |
exp |
single character vector of total expenditure. |
alph0 |
start value for translog price index. |
logp |
logical if prices are log prices. |
logexp |
logical if expenditure is log expenditure. |
priceindex |
character either "translog" or "S" for the stone price index. |
modeltype |
character either "AI" or "QAI" for AI or QAI model. |
ray |
logical if Ray (1983) scaling should be included. If TRUE requires demographic vector. |
demogr |
character vector of k demographic variables. |
While Ray and Stata use log(m0) for the demographic variables, there is no guarantee, that m0 is positive. The model relies much on the correct starting values. Therefore log(abs(m0)) is used.
Deaton, Angus S., Muellbauer, John: An Almost Ideal Demand System, The American Economic Review 70(3), American Economic Association, 312-326, 1980
Banks, James, Blundell, Richard, Lewbel, Arthur: Quadratic Engel Curves and Consumer Demand, The Review of Economics and Statistics 79(4), The MIT Press, 527-539, 1997
Ray, Ranjan: Measuring the costs of children: An alternative approach, Journal of Public Economics 22(1), 89-102, 1983
ai and qai
reshape mm for blockwise multiplication in wls_est
arma_reshape(mm, sizetheta)
arma_reshape(mm, sizetheta)
mm |
a matrix |
sizetheta |
integer of length(theta) to shrink mm into |
mm <- matrix(c(11,21,31,41, 12,22,32,42, 13,23,33,43, 14,24,34,44), ncol = 4) mm_a <- arma_reshape(mm, 2) mm_m <- matrix(t(mm), nrow = 2, byrow = TRUE)
mm <- matrix(c(11,21,31,41, 12,22,32,42, 13,23,33,43, 14,24,34,44), ncol = 4) mm_a <- arma_reshape(mm, 2) mm_m <- matrix(t(mm), nrow = 2, byrow = TRUE)
Check if formula contains constant
constant(x)
constant(x)
x |
formula |
Primitive function to check a formula for a constant part. Function checks first and last term on rhs for constant variables at front and back position.
## Not run: constant(y ~ x + a * z) # x constant(y ~ x * b + 1) # 1 constant(y ~ 0 + x) # NULL constant(y ~ x) # x constant(y ~ x1 * b1 + b0 + x2 * b2) # wont find b0 constant( y ~ (x*b +k) + a*y + b*z ) # wont find k constant( y ~ (k+ x*b) + a*y + b*z ) # k constant( y ~ a*y + b*z + (k + x*b) ) # wont find k constant( y ~ a*y + b*z + (x*b + k) ) # k ## End(Not run)
## Not run: constant(y ~ x + a * z) # x constant(y ~ x * b + 1) # 1 constant(y ~ 0 + x) # NULL constant(y ~ x) # x constant(y ~ x1 * b1 + b0 + x2 * b2) # wont find b0 constant( y ~ (x*b +k) + a*y + b*z ) # wont find k constant( y ~ (k+ x*b) + a*y + b*z ) # k constant( y ~ a*y + b*z + (k + x*b) ) # wont find k constant( y ~ a*y + b*z + (x*b + k) ) # k ## End(Not run)
A dataset combining tables 1 and 2 of Brendt and Wood 1975
data(costs)
data(costs)
A data frame with 25 rows and 14 cols
Year years from 1947 - 1971
Cost Total Input Cost in billion dollars (Table 2)
Sk Cost Shares K (Table 2)
Sl Cost Shares L (Table 2)
Se Cost Shares E (Table 2)
Sm Cost Shares M (Table 2)
Pk Price Index K (Table 1)
Pl Price Index L (Table 1)
Pe Price Index E (Table 1)
Pm Price Index M (Table 1)
K Quantity Index K (Table 1)
K Quantity Index L (Table 1)
K Quantity Index E (Table 1)
K Quantity Index M (Table 1)
Berndt, E. R. and Wood, D. O. (1975). Technology, prices, and the derived demand for energy. The review of Economics and Statistics, pages 259–268.
As discussed in Wooldridge (2002, 160)
cov_robust(x, u, qS, w, sizetheta)
cov_robust(x, u, qS, w, sizetheta)
x |
matrix of derivatives |
u |
u |
qS |
weighting matrix |
w |
vector of weights |
sizetheta |
sizetheta |
dm simple delta method implementation
dm(object, form, level = 0.05)
dm(object, form, level = 0.05)
object |
of class nlsur |
form |
formula e.g. "be/bk". |
level |
value for conf. interval default is 0.05 |
Estimates the income/expenditure elasticity, the uncompensated price elasticity and the compensated price elasticity
elasticities(object, data, type = 1, usemean = FALSE)
elasticities(object, data, type = 1, usemean = FALSE)
object |
qai result |
data |
data vector used for estimation |
type |
1 = expenditure; 2 = uncompensated; 3 = compensated |
usemean |
evaluate at mean |
Formula for the expenditure (income) elasticity
Formula for the uncompensated price elasticity
Compensated price elasticities (Slutsky equation)
Banks, James, Blundell, Richard, Lewbel, Arthur: Quadratic Engel Curves and Consumer Demand, The Review of Economics and Statistics 79(4), The MIT Press, 527-539, 1997
Poi, Brian P.: Easy demand-system estimation with quaids, The Stata Journal 12(3), 433-446, 2012
ai and qai
## Not run: library(nlsur) library(readstata13) dd <- read.dta13("http://www.stata-press.com/data/r15/food.dta") w <- paste0("w", 1:4); p <- paste0("p", 1:4); x <- "expfd" est <- ai(w = w, p = p, x = x, data = dd, a0 = 10, scale = FALSE, logp = F, logexp = F) mu <- elasticities(est, data = dd, type = 1, usemean = FALSE) ue <- elasticities(est, data = dd, type = 2, usemean = FALSE) ce <- elasticities(est, data = dd, type = 3, usemean = FALSE) ## End(Not run)
## Not run: library(nlsur) library(readstata13) dd <- read.dta13("http://www.stata-press.com/data/r15/food.dta") w <- paste0("w", 1:4); p <- paste0("p", 1:4); x <- "expfd" est <- ai(w = w, p = p, x = x, data = dd, a0 = 10, scale = FALSE, logp = F, logexp = F) mu <- elasticities(est, data = dd, type = 1, usemean = FALSE) ue <- elasticities(est, data = dd, type = 2, usemean = FALSE) ce <- elasticities(est, data = dd, type = 3, usemean = FALSE) ## End(Not run)
Function to create startvalues for nlsur models
getstartvals(model, data, val)
getstartvals(model, data, val)
model |
nlsur model |
data |
the data frame used for evaluation |
val |
value |
Check if object is of class formula
is.formula(x)
is.formula(x)
x |
object |
calculate WLS using eigen similar to the approach in MASS::lm.gls
lm_gls(X, Y, W, neqs, tol = 1e-07, covb = FALSE)
lm_gls(X, Y, W, neqs, tol = 1e-07, covb = FALSE)
X |
n x m X matrix |
Y |
n x k matrix |
W |
n x n |
neqs |
k |
tol |
tolerance for qr |
covb |
if true covb is calculated else theta |
Estimate nonlinear combinations of nlsur estimates
nlcom(object, form, alpha = 0.05, rname, envir)
nlcom(object, form, alpha = 0.05, rname, envir)
object |
of class nlsur |
form |
formula e.g. "be/bk". May contain names(coef(object)) or prior nlcom estimations. |
alpha |
value for conf. interval default is 0.05 |
rname |
optional rowname for result |
envir |
optional name of environment to search for additional parameters |
## Not run: dkm <- nlcom(object = erg, form = "-dkk -dkl -dke", rname = "dkm") dkm dlm <- nlcom(object = erg, form = "-dkl -dll -dle", rname = "dlm") dlm dem <- nlcom(object = erg, form = "-dke -dle -dee", rname = "dem") dem dmm <- nlcom(object = erg, form = "-dkm -dlm -dem", rname = "dmm") dmm # last one is equivalent to the longer form of: dmm <- nlcom(object = erg, form = "-(-dkk -dkl -dke) -(-dkl -dll -dle) -(-dke -dle -dee)") dmm ## End(Not run)
## Not run: dkm <- nlcom(object = erg, form = "-dkk -dkl -dke", rname = "dkm") dkm dlm <- nlcom(object = erg, form = "-dkl -dll -dle", rname = "dlm") dlm dem <- nlcom(object = erg, form = "-dke -dle -dee", rname = "dem") dem dmm <- nlcom(object = erg, form = "-dkm -dlm -dem", rname = "dmm") dmm # last one is equivalent to the longer form of: dmm <- nlcom(object = erg, form = "-(-dkk -dkl -dke) -(-dkl -dll -dle) -(-dke -dle -dee)") dmm ## End(Not run)
nlsur()
is used to fit nonlinear regression models. It can handle the
feasible and iterative feasible variants.
nlsur( eqns, data, startvalues, type = NULL, S = NULL, trace = FALSE, robust = FALSE, stata = TRUE, qrsolve = FALSE, weights, MASS = FALSE, maxiter = 1000, val = 0, tol = 1e-07, eps = 1e-05, ifgnlseps = 1e-10, tau = 0.001, initial = FALSE )
nlsur( eqns, data, startvalues, type = NULL, S = NULL, trace = FALSE, robust = FALSE, stata = TRUE, qrsolve = FALSE, weights, MASS = FALSE, maxiter = 1000, val = 0, tol = 1e-07, eps = 1e-05, ifgnlseps = 1e-10, tau = 0.001, initial = FALSE )
eqns |
is a list object containing the model as formula. This list can handle contain only a single equations (although in this case nls() might be a better choice) or a system of equations. |
data |
an (optional) data frame containing the variables that will be evaluated in the formula. |
startvalues |
initial values for the parameters to be estimated. |
type |
can be 1 Nonlinear Least Squares (NLS), 2 Feasible Generalized NLS (FGNLS) or 3 Iterative FGNLS (IFGNLS) or the respective abbreviations in character form. |
S |
is a weight matrix used for evaluation. If no weight matrix is provided the identity matrix I will be used. |
trace |
logical whether or not SSR information should be printed. Default is FALSE. |
robust |
logical if true robust standard errors are estimated. |
stata |
is a logical. If TRUE for nls a second evaluation will be run. Stata does this by default. For this second run Stata replaces the diagonal of the I matrix with the coefficients. |
qrsolve |
logical |
weights |
Additional weight vector. |
MASS |
is a logical whether an R function similar to the MASS::lm.gls() function should be used for weighted Regression. This can cause sever RAM usage as the weight matrix tend to be huge (n-equations * n-rows). |
maxiter |
Maximum number of iterations. |
val |
If no start values supplied, create them with this start value. Default is 0. |
tol |
qr.solves tolerance for detecting linear dependencies. |
eps |
the epsilon used for convergence in nlsur(). Default is 1e-5. |
ifgnlseps |
is epsilon for ifgnls(). Default is 1e-10. |
tau |
is another convergence variable. Default is 1e-3. |
initial |
logical value to define if rankMatrix is calculated every iteration of nlsur. |
nlsur() is a wrapper around .nlsur(). The function was initially inspired by the Stata Corp Function nlsur. Nlsur estimates a nonlinear least squares demand system. With nls, fgnls or ifgnls which is equivalent to Maximum Likelihood estimation. Nonlinear least squares requires start values and nlsur requires a weighting matrix for the demand system. If no weight matrix is provided, nlsur will use the identity matrix I. If type = 1 or type = "nls" is added, nlsur will use the matrix for an initial estimation, once the estimation is done, it will swap the diagonal with the estimated results.
Most robust regression estimates shall be returned with both qrsolve and MASS TRUE, but memory consumption is largest this way. If MASS is FALSE a memory efficient RcppArmadillo solution is used for fgnls and ifgnls. If qrsolve is FALSE as well, only the Armadillo function is used.
If robust
is selected Whites HC0 is used to calculate
Heteroscedasticity Robust Standard Errors.
If initial
is TRUE rankMatrix will be calculated every iteration of
nlsur. Meaning for nls at least once, for fgnls at least twice and for ifgnls
at least three times. This adds a lot of overhead, since rankMatrix is used
to calculate k. To assure that k does not change this can be set to TRUE.
Nlsur has methods for the generic functions coef, confint, deviance, df.residual, fitted, predict, print, residuals, summary and vcov.
The function returns a list object of class nlsur. The list includes:
estimated coefficients
residuals
residuals of each equation in a single list
list of equation names
the weight matrix
Residual sum of squares
Left hand side of the evaluated model
Right hand side of the evaluated model
model type. "NLS", "FGNLS" or "IFGNLS"
standard errors
t values
asymptotic covariance matrix
equation wise estimation results of SSR, MSE, RMSE, MAE, R2 and Adj-R2. As well as n, k and df.
equation or system of equations as list containing formulas
Gallant, A. Ronald (1987): Nonlinear Statistical Models. Wiley: New York
## Not run: # Greene Example 10.3 library(nlsur) url <- "http://www.stern.nyu.edu/~wgreene/Text/Edition7/TableF10-2.txt" dd <- read.table(url, header = T) names(dd) <- c("Year", "Cost", "Sk", "Sl", "Se", "Sm", "Pk", "Pl", "Pe", "Pm") eqns <- list( Sk ~ bk + dkk * log(Pk/Pm) + dkl * log(Pl/Pm) + dke * log(Pe/Pm), Sl ~ bl + dkl * log(Pk/Pm) + dll * log(Pl/Pm) + dle * log(Pe/Pm), Se ~ be + dke * log(Pk/Pm) + dle * log(Pl/Pm) + dee * log(Pe/Pm)) strtvls <- c(be = 0, bk = 0, bl = 0, dkk = 0, dkl = 0, dke = 0, dll = 0, dle = 0, dee = 0) erg <- nlsur(eqns = eqns, data = dd, startvalues = strtvls, type = 2, trace = TRUE, eps = 1e-10) erg ## End(Not run)
## Not run: # Greene Example 10.3 library(nlsur) url <- "http://www.stern.nyu.edu/~wgreene/Text/Edition7/TableF10-2.txt" dd <- read.table(url, header = T) names(dd) <- c("Year", "Cost", "Sk", "Sl", "Se", "Sm", "Pk", "Pl", "Pe", "Pm") eqns <- list( Sk ~ bk + dkk * log(Pk/Pm) + dkl * log(Pl/Pm) + dke * log(Pe/Pm), Sl ~ bl + dkl * log(Pk/Pm) + dll * log(Pl/Pm) + dle * log(Pe/Pm), Se ~ be + dke * log(Pk/Pm) + dle * log(Pl/Pm) + dee * log(Pe/Pm)) strtvls <- c(be = 0, bk = 0, bl = 0, dkk = 0, dkl = 0, dke = 0, dll = 0, dle = 0, dee = 0) erg <- nlsur(eqns = eqns, data = dd, startvalues = strtvls, type = 2, trace = TRUE, eps = 1e-10) erg ## End(Not run)
predict()
is a function to predict nlsur results.
## S3 method for class 'nlsur' predict(object, newdata, ...)
## S3 method for class 'nlsur' predict(object, newdata, ...)
object |
is an nlsur estimation result. |
newdata |
an optional data frame for which the prediction is evaluated. |
... |
further arguments for predict. At present no optional arguments are used. |
predict.nlsur evaluates the nlsur equation(s) given nlsurs estimated parameters using either the original data.frame or newdata. Since nlsur() restricts the data object only to complete cases observations with missings will not be fitted.
# predict(nlsurObj, dataframe)
# predict(nlsurObj, dataframe)
Estimation of an Quadratic Almost-Ideal Demand System using nlsur().
qai(w, p, x, z, a0 = 0, data, scale = FALSE, logp = TRUE, logexp = TRUE, ...)
qai(w, p, x, z, a0 = 0, data, scale = FALSE, logp = TRUE, logexp = TRUE, ...)
w |
character vector of m budgetshares used for estimation. |
p |
character vector of m prices. |
x |
single character vector of total expenditure. |
z |
character vector of k demographic variables. |
a0 |
start value for translog price index. |
data |
data.frame containing the variables. |
scale |
logical if TRUE Rays (1983) scaling is used. |
logp |
logical if prices are log prices. |
logexp |
logical if expenditure is log expenditure. |
... |
additional options passed to nlsur |
Banks, James, Blundell, Richard, Lewbel, Arthur: Quadratic Engel Curves and Consumer Demand, The Review of Economics and Statistics 79(4), The MIT Press, 527-539, 1997
Ray, Ranjan: Measuring the costs of children: An alternative approach, Journal of Public Economics 22(1), 89-102, 1983
ai and ai.model
calculate SSR where
ssr_est(r, s, w)
ssr_est(r, s, w)
r |
residuals |
s |
weighting matrix |
w |
vector of weights |
Blockwise WLS estimation. Usually for X and Y
X, W and Y are of similar dimensions. In nlsur W is a cov-matrix of size
and usually way smaller than X. To avoid blowing all
matrices up for the estimation, a blockwise approach is used. X is shrunken
to match size k. W is D'D so XDX is calculated. XDy is only calculated if
wanted for a full WLS. For the cov-matrix only XDX is required.
wls_est(x, r, qS, w, sizetheta, fullreg, tol)
wls_est(x, r, qS, w, sizetheta, fullreg, tol)
x |
matrix of derivatives |
r |
residual matrix |
qS |
weighting matrix of sizetheta x sizetheta |
w |
vector of weights |
sizetheta |
integer defining the amount of coefficients |
fullreg |
bool defining if WLS or Cov is calculated |
tol |
tolerance used for qr() |
as reference see: http://www.navipedia.net/index.php/Block-Wise_Weighted_Least_Square
Calculate a weighted mean
wt_mean(x, w)
wt_mean(x, w)
x |
matrix of derivatives |
w |
vector of weights |