Skip to content

Commit

Permalink
exported getCoefTable() + lots of small edits to vignettes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhelvy committed Dec 7, 2020
1 parent bc15ac9 commit 69a04d7
Show file tree
Hide file tree
Showing 28 changed files with 225 additions and 127 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package: logitr
Title: Random utility logit models with preference and willingness to pay space parameterizations
Date: 2020-12-04
Version: 0.0.4
Date: 2020-12-07
Version: 0.0.5
Authors@R: c(
person(given = "John",
family = "Helveston",
role = c("aut", "cre", "cph"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-2657-9191")))
Description: logitr estimates multinomial (MNL) and mixed logit (MXL) models in R. Models can be estimated using "Preference" space or "Willingness-to-pay" (WTP) space utility parameterizations. The program includes an option to run a multistart optimization loop with random starting points in each iteration, which is useful for non-convex problems like MXL models or models with WTP space utility parameterizations. The main optimization loop uses the nloptr function to minimize the negative log-likelihood function. The package has additional functions for computing and comparing WTP from both preference space and WTP space models and for simulating the expected shares of a set of alternatives using an estimated model.
Description: logitr estimates multinomial (MNL) and mixed logit (MXL) models in R. Models can be estimated using "Preference" space or "Willingness-to-pay" (WTP) space utility parameterizations. The program includes an option to run a multistart optimization loop with random starting points in each iteration, which is useful for non-convex problems like MXL models or models with WTP space utility parameterizations. The main optimization loop uses the {nloptr} package to minimize the negative log-likelihood function. The package has additional functions for computing and comparing WTP from both preference space and WTP space models and for simulating the expected shares of a set of alternatives using an estimated model.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method(coef,logitr)
S3method(summary,logitr)
export(dummyCode)
export(getCoefTable)
export(logitr)
export(simulateShares)
export(statusCodes)
Expand Down
16 changes: 16 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@

# logitr 0.0.5

## Summary of larger updates:

- Added support for auto creating interactions amongst variables

## Summary of smaller updates:

- Added new documentation for prepping data:
- overall structure
- dummyCode() function
- handling interactions
- All vignettes proof-read with lots of small changes to examples
- Added a hex sticker

# logitr 0.0.4

Weighted models, new dataset, new encoding features
Expand Down
Binary file modified R/.DS_Store
Binary file not shown.
17 changes: 15 additions & 2 deletions R/main.R → R/logitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,30 @@
#'
#' @export
#' @examples
#' # For detailed examples, visit https://jhelvy.github.io/logitr/articles/examples.html
#' \dontrun{
#' # For more detailed examples, visit
#' https://jhelvy.github.io/logitr/articles/
#'
#' # Run a MNL model in the Preference Space:
#' library(logitr)
#'
#' # Run a MNL model in the Preference Space:
#' mnl_pref <- logitr(
#' data = yogurt,
#' choiceName = "choice",
#' obsIDName = "obsID",
#' parNames = c("price", "feat", "dannon", "hiland", "yoplait")
#' )
#'
#' # Run a MNL model in the WTP Space:
#' mnl_wtp <- logitr(
#' data = yogurt,
#' choiceName = "choice",
#' obsIDName = "obsID",
#' parNames = c("feat", "dannon", "hiland", "yoplait"),
#' priceName = "price",
#' modelSpace = "wtp"
#' )
#' }
logitr <- function(data, choiceName, obsIDName, parNames, priceName = NULL,
randPars = NULL, randPrice = NULL, modelSpace = "pref",
weightsName = NULL, options = list()) {
Expand Down
37 changes: 32 additions & 5 deletions R/summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ printMultistartSummary <- function(model) {
}

printModelSummary <- function(model) {
coefTable <- getCoefTable(
model$coef, model$standErrs, model$numObs,
model$numParams
)
coefTable <- getCoefTable(model)
statTable <- getStatTable(
model$logLik, model$nullLogLik, model$numObs,
model$numParams
Expand Down Expand Up @@ -136,7 +133,37 @@ getBasicInfoTable <- function(model) {
return(basicInfoSummary)
}

getCoefTable <- function(coef, se, numObs, numParams) {
#' Get the coefficient summary table as a data frame
#'
#' Returns a data frame of the coefficient summary table of a model estimated using the `logitr()` function.
#' @keywords logitr, summary, coefTable
#' @param object The output of a model estimated model using the `logitr()` function.
#' @return Returns a data frame of the coefficient summary table of a model estimated using the `logitr()` function.
#' @export
#' @examples
#' library(logitr)
#'
#' # Run a MNL model in the preference space
#' mnl_pref <- logitr(
#' data = yogurt,
#' choiceName = "choice",
#' obsIDName = "obsID",
#' parNames = c("price", "feat", "dannon", "hiland", "yoplait")
#' )
#'
#' # Get the coefficient summary table as a data frame
#' getCoefTable(mnl_pref)
getCoefTable <- function(object) {
coefTable <- getCoefSummaryTable(
coef = object$coef,
se = object$standErrs,
numObs = object$numObs,
numParams = object$numParams
)
return(coefTable)
}

getCoefSummaryTable <- function(coef, se, numObs, numParams) {
tStat <- rep(NA, length(coef))
pVal <- rep(NA, length(coef))
signif <- rep("", length(coef))
Expand Down
5 changes: 3 additions & 2 deletions R/wtp.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ wtp <- function(model, priceName) {
} else if (model$modelSpace == "wtp") {
wtp_mean <- stats::coef(model)
wtp_se <- model$standErrs
return(getCoefTable(wtp_mean, wtp_se, model$numObs, model$numParams))
return(getCoefSummaryTable(
wtp_mean, wtp_se, model$numObs, model$numParams))
}
}

Expand All @@ -60,7 +61,7 @@ getPrefSpaceWtp <- function(model, priceName) {
wtpDraws <- draws / priceDraws
wtpDraws[, priceID] <- draws[, priceID]
wtp_se <- apply(wtpDraws, 2, stats::sd)
return(getCoefTable(wtp_mean, wtp_se, model$numObs, model$numParams))
return(getCoefSummaryTable(wtp_mean, wtp_se, model$numObs, model$numParams))
}

#' Compare WTP from preference and WTP space models
Expand Down
22 changes: 10 additions & 12 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ output: github_document

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "man/figures/",
collapse = TRUE,
warning = FALSE,
message = FALSE,
comment = "#>",
fig.path = "man/figures/",
fig.retina = 3
)
```
Expand All @@ -28,16 +28,13 @@ status](https://www.r-pkg.org/badges/version/logitr)](https://CRAN.R-project.org
R. Models can be estimated using “Preference” space or
“Willingness-to-pay” (WTP) space [utility
parameterizations](https://jhelvy.github.io/logitr/articles/utility_models.html).
The current version includes support for:
The latest version includes support for:

- Homogeneous multinomial logit (MNL) models
- Heterogeneous mixed logit (MXL) models (with normal and log-normal parameter distributions).
- Preference space utility parameterization.
- WTP space utility parameterization.
- An optional multistart optimization that uses different random starting points in each iteration (useful for non-convex problems like MXL models or models with WTP space parameterizations).

The package also has additional functions for:

- An option to run a multistart optimization loop that uses different random starting points in each iteration (useful for non-convex problems like MXL models or models with WTP space parameterizations).
- Computing and comparing WTP from both preference space and WTP space models.
- Simulating the expected shares of a set of alternatives using an estimated model.

Expand All @@ -55,6 +52,7 @@ devtools::install_github("jhelvy/logitr")
```

Load the library with:

```{r, eval=FALSE}
library(logitr)
```
Expand All @@ -69,11 +67,11 @@ library(logitr)
- Date First Written: *Sunday, September 28, 2014*
- Most Recent Update: `r format(Sys.Date(), format="%B %d %Y")`
- License: [MIT](https://github.com/jhelvy/logitr/blob/master/LICENSE.md)
- [Latest Release](https://github.com/jhelvy/logitr/releases/latest): 0.0.4
- [Latest Release](https://github.com/jhelvy/logitr/releases/latest): 0.0.5

## Citation Information

If you use this package for in a publication, I would greatly appreciate it if you cited it. You can get the citation information by typing `citation("logitr")` into R:
If you use this package for in a publication, I would greatly appreciate it if you cited it - you can get the citation by typing `citation("logitr")` into R:

```{r}
citation("logitr")
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ status](https://www.r-pkg.org/badges/version/logitr)](https://CRAN.R-project.org
R. Models can be estimated using “Preference” space or
“Willingness-to-pay” (WTP) space [utility
parameterizations](https://jhelvy.github.io/logitr/articles/utility_models.html).
The current version includes support for:
The latest version includes support for:

- Homogeneous multinomial logit (MNL) models
- Heterogeneous mixed logit (MXL) models (with normal and log-normal
parameter distributions).
- Preference space utility parameterization.
- WTP space utility parameterization.
- An optional multistart optimization that uses different random
starting points in each iteration (useful for non-convex problems
like MXL models or models with WTP space parameterizations).

The package also has additional functions for:

- An option to run a multistart optimization loop that uses different
random starting points in each iteration (useful for non-convex
problems like MXL models or models with WTP space
parameterizations).
- Computing and comparing WTP from both preference space and WTP space
models.
- Simulating the expected shares of a set of alternatives using an
Expand Down Expand Up @@ -77,12 +75,12 @@ probabilities).
- License:
[MIT](https://github.com/jhelvy/logitr/blob/master/LICENSE.md)
- [Latest Release](https://github.com/jhelvy/logitr/releases/latest):
0.0.4
0.0.5

## Citation Information

If you use this package for in a publication, I would greatly appreciate
it if you cited it. You can get the citation information by typing
it if you cited it - you can get the citation by typing
`citation("logitr")` into R:

``` r
Expand Down
26 changes: 12 additions & 14 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ navbar:
href: articles/utility_models.html
- text: "Data Formatting and Encoding"
href: articles/data_formatting.html
- text: "Models with Interactions"
href: articles/interactions.html
- text: "Examples"
menu:
- text: "Estimating multinomial logit models"
- text: "Estimating Multinomial Logit Models"
href: articles/mnl_models.html
- text: "Estimating mixed logit models"
- text: "Estimating Mixed Logit Models"
href: articles/mxl_models.html
- text: "Estimating weighted logit models"
- text: "Estimating Models with Interactions"
href: articles/interactions.html
- text: "Estimating Weighted Logit Models"
href: articles/weighted_models.html
- text: "Simulating shares"
- text: "Simulating Shares from Estimated Models"
href: articles/simulations.html
- text: "Changelog"
href: news/index.html
Expand All @@ -43,30 +41,30 @@ navbar:
href: https://github.com/jhelvy/logitr

reference:
- title: "Encoding data"
- title: "Encoding Data"
desc: "Functions for encoding data prior to model estimation."
contents:
- dummyCode
- title: "Estimating models"
- title: "Estimating Models"
desc: "Functions for estimating multinomial logit models with preference space and WTP space utility parameterizations."
contents:
- logitr
- title: "Viewing results"
- title: "Viewing Results"
desc: "Functions for viewing the results of estimated models."
contents:
- summary.logitr
- coef.logitr
- statusCodes
- title: "Computing and comparing WTP"
- title: "Computing and Comparing WTP"
desc: "Functions for computing and comparing WTP from estimated models."
contents:
- wtp
- wtpCompare
- title: "Simulating shares"
- title: "Simulating Shares from Estimated Models"
desc: "Functions for simulating expected shares from a set of alternatives."
contents:
- simulateShares
- title: "Example data sets"
- title: "Example Data Sets"
desc: "Descriptions of data included with this package"
contents:
- yogurt
Expand Down
4 changes: 2 additions & 2 deletions build.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ devtools::document()
# Install the package
devtools::install(force = TRUE)

# Build the pkgdown site
pkgdown::build_site()
# # Build the pkgdown site
# pkgdown::build_site()

# Check package
devtools::check()
Expand Down
4 changes: 2 additions & 2 deletions inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ citEntry(
title = "logitr: Random utility logit models with preference and willingness to pay space parameterizations",
author = "John Paul Helveston",
year = "2020",
note = "R package version 0.0.4",
note = "R package version 0.0.5",
url = "https://jhelvy.github.io/logitr/",
textVersion = "John Paul Helveston. logitr: Random utility logit models with preference and willingness to pay space parameterizations (2020)"
textVersion = "John Paul Helveston (2020). logitr: Random utility logit models with preference and willingness to pay space parameterizations."
)
2 changes: 2 additions & 0 deletions inst/example/data_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ model_dummy_price <- logitr(
'bev150', 'american', 'japanese', 'chinese', 'skorean',
'phevFastcharge', 'bevFastcharge','opCost', 'accelTime'))

summary(model_dummy_price)

# Save results
saveRDS(model_default,
here::here('inst', 'extdata', 'encoding_model_default.Rds'))
Expand Down
Binary file modified inst/extdata/encoding_model_character_price.Rds
Binary file not shown.
Binary file modified inst/extdata/encoding_model_default.Rds
Binary file not shown.
Binary file modified inst/extdata/encoding_model_dummy_price.Rds
Binary file not shown.
34 changes: 34 additions & 0 deletions man/getCoefTable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 69a04d7

Please sign in to comment.