Learn R Programming

FSA (version 0.8.11)

extraTests: Likelihood ratio and extra sum-of-squares tests.

Description

Likelihood ratio and extra sum-of-squares tests with multiple lm or nls models nested within one common model. This function is most useful when the nested functions are all at the same level; otherise use anova() or lrtest() which are more flexible.

Usage

lrt(sim, ..., com, sim.names = sim.name, sim.name = NULL, com.name = NULL)
extraSS(sim, ..., com, sim.names = sim.name, sim.name = NULL, com.name = NULL)
"print"(x, ...)

Arguments

sim
The results of one lm or nls model, for example, that is a nested subset of the model in com=.
com
The results of one lm or nls model, for example, that the models in sim= and ... are a subset of.
sim.name, sim.names
A string vector of “names” for simple model in sim= and .... sim.names is preferred but sim.name is allowed to allow for a common typing mistake.
com.name
A single “name” string for the complex model in com=.
x
An object from lrt() or extraSS().
...
More model results that are nested subsets of the model in com=.

Value

The main function returns a matrix with as many rows as model comparisons and columns of the following types:
  • DfO The error degrees-of-freedom from the subset (more simple) model.
  • RSSO, logLikO The residual sum-of-squares (from extraSS) or log-likelihood (from lrt) from the subset (more simple) model.
  • DfA The error degrees-of-freedom from the com= model.
  • RSSA, logLikA The residual sum-of-squares (from extraSS) or log-likelihood (from lrt) from the com= model.
  • Df The difference in error degrees-of-freedom between the two models.
  • SS, logLik The difference in residual sum-of-squares (from extraSS) or log-likelihood (from lrt) between the two models.
  • F, Chisq The corresponding F- (from extraSS) or Chi-square (from lrt) test statistic.
  • Pr(>F), Pr(>Chisq) The corresponding p-value.

Details

anova and lrtest (from lmtest) provide simple methods for conducting extra sum-of-squares or likelihood ratio tests when one model is nested within another model or when there are several layers of simple models all sequentially nested within each other. However, to compare several models that are nested at the same level with one common more complex model, then anova() and lrtest() must be repeated for each comparison. This repetition can be eliminated with lapply() but then the output is voluminous. This function is designed to remove the repetitiveness and to provide output that is compact and easy to read.

Examples

Run this code
## Example data
df <- data.frame(x=c(1,2,3,4,5,6,7,8,9,10),
                 y=c(4,6,5,7,9,8,7,12,16,22),
                 z=as.factor(rep(c("A","B"),each=5)),
                 w=as.factor(rep(c("A","B"),times=5)))
df$x2 <- df$x^2

## Linear (lm()) models
#  ... regression
fit.0 <- lm(y~1,data=df)
fit.1 <- lm(y~x,data=df)
fit.2 <- lm(y~x2+x,data=df)
extraSS(fit.0,fit.1,com=fit.2)
lrt(fit.0,fit.1,com=fit.2)

# ... show labels for models
extraSS(fit.0,fit.1,com=fit.2,
    sim.names=c("Null Model","Linear"),com.name="Quadratic")
lrt(fit.0,fit.1,com=fit.2,
    sim.names=c("Null Model","Linear"),com.name="Quadratic")

#  ... dummy variable regression
fit.2b <- lm(y~x*z,data=df)
extraSS(fit.0,fit.1,com=fit.2b)
lrt(fit.0,fit.1,com=fit.2b)

#  ... ANOVAs
fit.1 <- lm(y~w,data=df)
fit.2 <- lm(y~w*z,data=df)
extraSS(fit.0,fit.1,com=fit.2)
lrt(fit.0,fit.1,com=fit.2)


## Non-linear (nls()) models
fit.0 = nls(y~c,data=df,start=list(c=10))
fit.1 = nls(y~a*x+c,data=df,start=list(a=1,c=1))
fit.2 = nls(y~b*x2+a*x+c,data=df,start=list(a=-1,b=0.3,c=10))
extraSS(fit.0,fit.1,com=fit.2)
lrt(fit.0,fit.1,com=fit.2)

## General least-squares (gls()) models
## Not run: 
#   require(nlme)
#   fit.0 <- gls(y~1,data=df,method="ML")
#   fit.1 <- gls(y~x,data=df,method="ML")
#   fit.2 <- gls(y~x2+x,data=df,method="ML")
#   lrt(fit.0,fit.1, com=fit.2)
#   ## will return an error ... does not work with gls() models
#   # extraSS(fit.0,fit.1, com=fit.2)
# ## End(Not run)

Run the code above in your browser using DataLab