Skip to content

Commit

Permalink
version 0.3-1
Browse files Browse the repository at this point in the history
  • Loading branch information
melff authored and gaborcsardi committed Oct 13, 2014
1 parent 0dc064f commit 315dcff
Show file tree
Hide file tree
Showing 10 changed files with 530 additions and 527 deletions.
21 changes: 12 additions & 9 deletions DESCRIPTION
@@ -1,18 +1,21 @@
Package: mclogit
Type: Package
Title: Mixed Conditional Logit
Version: 0.2-7
Date: 2013-09-19
Version: 0.3-1
Date: 2014-10-13
Author: Martin Elff
Maintainer: Martin Elff <martin.elff@uni-konstanz.de>
Description: This packages provides a function to estimate parameters for the
mixed conditional logit model, or conditional logit with random effects.
The current implementation of random effects is limited to
Maintainer: Martin Elff <elff@gmx.com>
Description: This packages provides a function to estimate parameters for
the conditional logit model (also with multinomial counts), and for the
mixed conditional logit model, or conditional logit with random effects
(random intercepts only, no random slopes yet).
The current implementation of random effects is limited to
the PQL technique, which requires large cluster sizes.
License: GPL-2
Depends: stats, memisc, Matrix
Depends: stats, Matrix
Enhances: memisc
LazyLoad: Yes
Packaged: 2013-09-20 12:11:40 UTC; elff
Packaged: 2014-10-13 21:17:58 UTC; elff
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2013-09-20 14:58:31
Date/Publication: 2014-10-14 05:27:20
15 changes: 9 additions & 6 deletions MD5
@@ -1,12 +1,15 @@
33d1f12d1f08aa3e6e7f9affc15591a7 *DESCRIPTION
7c9229440c07e8270cfe7055ccd00b64 *NAMESPACE
2d7bb76fd1af8a32c16fb41dda75c8c4 *DESCRIPTION
91de8d80be7abb5b1d5b2a78d8c5f0d5 *NAMESPACE
5f46b25a049e81e86774e78cd294ff89 *R/AIC-mclogit.R
2a4282759e56db51328aad1362272468 *R/mclogit.R
7f63c47aaab8ed3021893f8bf7fa0acf *R/zzz.R
b3b474df7d56742741906a0ed283baa8 *data/Transport.rda
dd2959c74f02d37e67a8a244c8cf95be *data/electors.rda
fda85116896df5934a2ce2b9e3118dee *R/anova-mclogit.R
95e5f690400563e770d043a1295da32b *R/getSummary-mclogit.R
041f033765de160259128f8f775ea1fe *R/mclogit.R
61e6ff81b925d46add29e1c6bba5a54e *R/zzz.R
8c73b3331c3da5530e555e7898cfd632 *data/Transport.rda
10525f6178553c47e6960272b780b805 *data/electors.rda
de4c63b49c62998527d72c185ceab0d3 *demo/00Index
6ba2e10f92f3743f465cda6cc059f5a1 *demo/mclogit.test.R
7989172556c6229e560751309221578c *inst/NEWS
c05ff442f7a2312ff8f3d6b5aa96ddeb *man/Transport.Rd
ca90251e1b6d6c9dd5d4bf069ffa090b *man/electors.Rd
f04206732a086c1534f275382a36d4b9 *man/getSummary-mclogit.Rd
Expand Down
5 changes: 3 additions & 2 deletions NAMESPACE
@@ -1,12 +1,11 @@
import(stats,Matrix)
importFrom(memisc,setSummaryTemplate)
#,memisc)
export(
mclogit,
mclogit.control,
getSummary.mclogit#,
#mclogit.fit,
#mclogit.fit.random
#mclogit.fit.rePQL
)
S3method(print,mclogit)
S3method(vcov,mclogit)
Expand All @@ -16,8 +15,10 @@ S3method(summary,mclogit)
S3method(print,summary.mclogit)
S3method(fitted,mclogit)
S3method(predict,mclogit)
S3method(residuals,mclogit)
#S3method(getSummary,mclogit)
S3method(AIC,mclogit)
S3method(BIC,mclogit)
S3method(anova,mclogit)


59 changes: 59 additions & 0 deletions R/anova-mclogit.R
@@ -0,0 +1,59 @@
anova.mclogit <- function (object, ..., dispersion = NULL, test = NULL)
{
dotargs <- list(...)
named <- if (is.null(names(dotargs)))
rep_len(FALSE, length(dotargs))
else (names(dotargs) != "")
if (any(named))
warning("the following arguments to 'anova.mclogit' are invalid and dropped: ",
paste(deparse(dotargs[named]), collapse = ", "))
dotargs <- dotargs[!named]
is.mclogit <- vapply(dotargs, function(x) inherits(x, "mclogit") ,
#&!inherits(x,"mclogitRandeff"),
NA)
dotargs <- dotargs[is.mclogit]
if (length(dotargs))
return(anova.mclogitlist(c(list(object), dotargs), dispersion = dispersion,
test = test))
stop("'anova.mclogit' can only be used to compare fitted models")
}

anova.mclogitlist <- function (object, ..., dispersion = NULL, test = NULL)
{
responses <- as.character(lapply(object, function(x) {
deparse(formula(x)[[2L]])
}))
sameresp <- responses == responses[1L]
if (!all(sameresp)) {
object <- object[sameresp]
warning(gettextf("models with response %s removed because response differs from model 1",
sQuote(deparse(responses[!sameresp]))), domain = NA)
}
ns <- sapply(object, function(x) x$N)
if (any(ns != ns[1L]))
stop("models were not all fitted to the same size of dataset")
nmodels <- length(object)
if (nmodels == 1) stop("'anova.mclogit' can only be used to compare fitted models")

resdf <- as.numeric(lapply(object, function(x) x$residual.df))
resdev <- as.numeric(lapply(object, function(x) x$deviance))
table <- data.frame(resdf, resdev, c(NA, -diff(resdf)), c(NA,
-diff(resdev)))
variables <- lapply(object, function(x) paste(deparse(formula(x)),
collapse = "\n"))
dimnames(table) <- list(1L:nmodels, c("Resid. Df", "Resid. Dev",
"Df", "Deviance"))
title <- "Analysis of Deviance Table\n"
topnote <- paste("Model ", format(1L:nmodels), ": ", variables,
sep = "", collapse = "\n")
if (!is.null(test)) {
bigmodel <- object[[order(resdf)[1L]]]
df.dispersion <- Inf
table <- stat.anova(table = table, test = test, scale = 1,
df.scale = df.dispersion, n = bigmodel$N)
}
structure(table, heading = c(title, topnote), class = c("anova",
"data.frame"))
}


105 changes: 105 additions & 0 deletions R/getSummary-mclogit.R
@@ -0,0 +1,105 @@
getSummary.mclogit <- function(obj,
alpha=.05,
rearrange=NULL,
#as.columns=NULL,
...){

smry <- summary(obj)
N <- obj$N
coef <- smry$coefficients
varPar <- smry$varPar

lower.cf <- qnorm(p=alpha/2,mean=coef[,1],sd=coef[,2])
upper.cf <- qnorm(p=1-alpha/2,mean=coef[,1],sd=coef[,2])
coef <- cbind(coef,lower.cf,upper.cf)
colnames(coef) <- c("est","se","stat","p","lwr","upr")
if(length(varPar)){
se.log.varPar <- varPar[,1]*varPar[,2]
lower.log.varPar <- qnorm(p=alpha/2,mean=log(varPar[,1]),sd=se.log.varPar[2])
upper.log.varPar <- qnorm(p=1-alpha/2,mean=log(varPar[,1]),sd=se.log.varPar[2])
varPar <- cbind(varPar,exp(lower.log.varPar),exp(upper.log.varPar))
colnames(varPar) <- c("est","se","stat","p","lwr","upr")
rownames(varPar) <- paste("Var(",rownames(varPar),")",sep="")
}
if(length(rearrange)){
coef.grps <- lapply(rearrange,function(ii){
if(is.character(ii) && !all(ii %in% rownames(coef)))
stop("coefficient(s) ",dQuote(unname(ii[!(ii %in% rownames(coef))]))," do not exist")
structure(coef[ii,],
dimnames=list(names(ii),dimnames(coef)[[2]])
)
})
grp.titles <- names(rearrange)
coef.grps <- do.call(memisc::collect,coef.grps)
coef <- array(NA,dim=c(
dim(coef.grps)[1] + NROW(varPar),
dim(coef.grps)[2],
dim(coef.grps)[3]
))
coef[seq(dim(coef.grps)[1]),,] <- coef.grps
if(length(varPar))
coef[dim(coef.grps)[1]+seq(nrow(varPar)),,1] <- varPar
dimnames(coef) <- list(
c(dimnames(coef.grps)[[1]],rownames(varPar)),
dimnames(coef.grps)[[2]],
grp.titles
)
}
else {
.coef <- coef
coef <- matrix(NA,nrow=nrow(.coef)+NROW(varPar),ncol=ncol(.coef))
coef[seq(nrow(.coef)),] <- .coef
if(length(varPar))
coef[nrow(.coef)+seq(nrow(varPar)),] <- varPar
rownames(coef) <- c(rownames(.coef),rownames(varPar))
colnames(coef) <- colnames(.coef)
}


phi <- smry$phi
LR <- smry$null.deviance - smry$deviance
df <- obj$model.df
deviance <- deviance(obj)


if(df > 0){
p <- pchisq(LR,df,lower.tail=FALSE)
L0.pwr <- exp(-smry$null.deviance/N)
LM.pwr <- exp(-smry$deviance/N)

McFadden <- 1- smry$deviance/smry$null.deviance
Cox.Snell <- 1 - exp(-LR/N)
Nagelkerke <- Cox.Snell/(1-L0.pwr)
}
else {
LR <- NA
df <- NA
p <- NA
McFadden <- NA
Cox.Snell <- NA
Nagelkerke <- NA
}

ll <- obj$ll
AIC <- AIC(obj)
BIC <- AIC(obj,k=log(N))
sumstat <- c(
phi = phi,
LR = LR,
df = df,
#p = p,
logLik = ll,
deviance = deviance,
McFadden = McFadden,
Cox.Snell = Cox.Snell,
Nagelkerke = Nagelkerke,
AIC = AIC,
BIC = BIC,
N = N
)

#coef <- apply(coef,1,applyTemplate,template=coef.template)

#sumstat <- drop(applyTemplate(sumstat,template=sumstat.template))
list(coef=coef,sumstat=sumstat)
}

0 comments on commit 315dcff

Please sign in to comment.