Learn R Programming

greybox (version 0.5.3)

alm: Advanced Linear Model

Description

Function estimates model based on the selected distribution

Usage

alm(formula, data, subset, na.action, distribution = c("dnorm", "dlogis",
  "dlaplace", "dalaplace", "ds", "dt", "dfnorm", "dlnorm", "dchisq",
  "dbcnorm", "dpois", "dnbinom", "dbeta", "plogis", "pnorm"),
  occurrence = c("none", "plogis", "pnorm"), ar = 0, i = 0,
  parameters = NULL, vcovProduce = FALSE, fast = TRUE, ...)

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted.

data

a data frame or a matrix, containing the variables in the model.

subset

an optional vector specifying a subset of observations to be used in the fitting process.

na.action

a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The factory-fresh default is na.omit. Another possible value is NULL, no action. Value na.exclude can be useful.

distribution

what density function to use in the process. The full name of the distribution should be provided here. Values with "d" in the beginning of the name refer to the density function, while "p" stands for "probability" (cumulative distribution function). The names align with the names of distribution functions in R. For example, see dnorm.

occurrence

what distribution to use for occurrence variable. Can be "none", then nothing happens; "plogis" - then the logistic regression using alm() is estimated for the occurrence part; "pnorm" - then probit is constructed via alm() for the occurrence part. In both of the latter cases, the formula used is the same as the formula for the sizes. Finally, an "alm" model can be provided and its estimates will be used in the model construction.

If this is not "none", then the model is estimated in two steps: 1. Occurrence part of the model; 2. Sizes part of the model (excluding zeroes from the data).

ar

the order of AR to include in the model. Only non-seasonal orders are accepted.

i

the order of I to include in the model. Only non-seasonal orders are accepted.

parameters

vector of parameters of the linear model. When NULL, it is estimated.

vcovProduce

whether to produce variance-covariance matrix of coefficients or not. This is done via hessian calculation, so might be computationally costly.

fast

if FALSE, then the function won't check whether the data has variability and whether the regressors are correlated. Might cause trouble, especially in cases of multicollinearity.

...

additional parameters to pass to distribution functions. This includes: alpha value for Asymmetric Laplace distribution, size for the Negative Binomial or df for the Chi-Squared. You can also pass two parameters to the optimiser: 1. maxeval - maximum number of evaluations to carry out (default is 100); 2. xtol_rel - the precision of the optimiser (the default is 1E-6); 3. algorithm - the algorithm to use in optimisation ("NLOPT_LN_SBPLX" by default). 4. print_level - the level of output for the optimiser (0 by default). You can read more about these parameters in the documentation of nloptr function.

Value

Function returns model - the final model of the class "alm", which contains:

  • coefficients - estimated parameters of the model,

  • vcov - covariance matrix of parameters of the model (based on Fisher Information). Returned only when vcovProduce=TRUE,

  • fitted - fitted values,

  • residuals - residuals of the model,

  • mu - the estimated location parameter of the distribution,

  • scale - the estimated scale parameter of the distribution,

  • distribution - distribution used in the estimation,

  • logLik - log-likelihood of the model,

  • df.residual - number of degrees of freedom of the residuals of the model,

  • df - number of degrees of freedom of the model,

  • call - how the model was called,

  • rank - rank of the model,

  • data - data used for the model construction,

  • occurrence - the occurrence model used in the estimation,

  • other - the list of all the other parameters either passed to the function or estimated in the process, but not included in the standard output (e.g. alpha for Asymmetric Laplace).

Details

This is a function, similar to lm, but for the cases of several non-normal distributions. These include:

  1. Normal distribution, dnorm,

  2. Logistic Distribution, dlogis,

  3. Laplace distribution, dlaplace,

  4. Asymmetric Laplace distribution, dalaplace,

  5. T-distribution, dt,

  6. S-distribution, ds,

  7. Folded normal distribution, dfnorm,

  8. Log normal distribution, dlnorm,

  9. Chi-Squared Distribution, dchisq,

  10. Beta distribution, dbeta,

  11. Poisson Distribution, dpois,

  12. Negative Binomial Distribution, dnbinom,

  13. Cumulative Logistic Distribution, plogis,

  14. Cumulative Normal distribution, pnorm.

This function is slower than lm, because it relies on likelihood estimation of parameters, hessian calculation and matrix multiplication. So think twice when using distribution="dnorm" here.

Probably some other distributions will be added to this function at some point...

The estimation is done using likelihood of respective distributions.

ALM function currently does not work with factors and does not accept transformations of variables in the formula. So you need to do transformations separately before using the function.

See more details and examples in the vignette "ALM": vignette("alm","greybox")

See Also

stepwise, lmCombine, xregTransformer

Examples

Run this code
# NOT RUN {
### An example with mtcars data and factors
mtcars2 <- within(mtcars, {
   vs <- factor(vs, labels = c("V", "S"))
   am <- factor(am, labels = c("automatic", "manual"))
   cyl  <- ordered(cyl)
   gear <- ordered(gear)
   carb <- ordered(carb)
})
# The standard model with Log Normal distribution
ourModel <- alm(mpg~., mtcars2[1:30,], distribution="dlnorm")
summary(ourModel)
plot(ourModel)

# Produce predictions with the one sided interval (upper bound)
predict(ourModel, mtcars2[-c(1:30),], interval="p", side="u")


### Artificial data for the other examples
xreg <- cbind(rlaplace(100,10,3),rnorm(100,50,5))
xreg <- cbind(100+0.5*xreg[,1]-0.75*xreg[,2]+rlaplace(100,0,3),xreg,rnorm(100,300,10))
colnames(xreg) <- c("y","x1","x2","Noise")
inSample <- xreg[1:80,]
outSample <- xreg[-c(1:80),]

# An example with Laplace distribution
ourModel <- alm(y~x1+x2, inSample, distribution="dlaplace")
summary(ourModel)
plot(predict(ourModel,outSample))

# And another one with Asymmetric Laplace distribution (quantile regression)
# with optimised alpha
ourModel <- alm(y~x1+x2, inSample, distribution="dalaplace")
summary(ourModel)
plot(predict(ourModel,outSample))

# An example with AR(1) order
ourModel <- alm(y~x1+x2, inSample, distribution="dnorm", ar=1)
summary(ourModel)
plot(predict(ourModel,outSample))

### Examples with the count data
xreg[,1] <- round(exp(xreg[,1]-70),0)
inSample <- xreg[1:80,]
outSample <- xreg[-c(1:80),]

# Negative Binomial distribution
ourModel <- alm(y~x1+x2, inSample, distribution="dnbinom")
summary(ourModel)
predict(ourModel,outSample,interval="p",side="u")

# Poisson distribution
ourModel <- alm(y~x1+x2, inSample, distribution="dpois")
summary(ourModel)
predict(ourModel,outSample,interval="p",side="u")


### Examples with binary response variable
xreg[,1] <- round(xreg[,1] / (1 + xreg[,1]),0)
inSample <- xreg[1:80,]
outSample <- xreg[-c(1:80),]

# Logistic distribution (logit regression)
ourModel <- alm(y~x1+x2, inSample, distribution="plogis")
summary(ourModel)
plot(predict(ourModel,outSample,interval="c"))

# Normal distribution (probit regression)
ourModel <- alm(y~x1+x2, inSample, distribution="pnorm")
summary(ourModel)
plot(predict(ourModel,outSample,interval="p"))

# }

Run the code above in your browser using DataLab