Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dc064f
commit 315dcff
Showing
10 changed files
with
530 additions
and
527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.