Skip to content

Commit dd26978

Browse files
committed
fixed bug in rowsum() where the default argument was reorder = TRUE
1 parent 58cb38a commit dd26978

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

NEWS.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
# logitr 0.1.2.9000
22

3-
## Summary of larger updates:
4-
5-
6-
## Summary of smaller updates:
7-
8-
93
## Bugs
104

115
- Fixed bug where model with single variable would error due to a matrix being converted to a vector in the `standardDraws()` function
126
- Fixed bug in `getCatVarDummyNames()` - previously used string matching, which can accidentally match with other similarly-named variables.
7+
- Fixed bug in `rowsum()` where the `reorder` argument was set to `TRUE`, which resulted in wrong logit calculations unless the `obsID` happened to be already sorted.
138

149
# logitr 0.1.1
1510

R/logit.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Returns the logit fraction for mnl (homogeneous) models
2222
getMnlLogit <- function(V, obsID) {
2323
expV <- exp(V)
24-
sumExpV <- rowsum(expV, group = obsID)
24+
sumExpV <- rowsum(expV, group = obsID, reorder = FALSE)
2525
repTimes <- as.numeric(table(obsID))
2626
sumExpVMat <- matrix(rep(sumExpV, times = repTimes), ncol = 1)
2727
logit <- expV / sumExpVMat
@@ -90,7 +90,7 @@ getMnlHessLL <- function(pars, modelInputs) {
9090
getMxlLogit <- function(VDraws, obsID) {
9191
numDraws <- ncol(VDraws)
9292
expVDraws <- exp(VDraws)
93-
sumExpVDraws <- rowsum(expVDraws, group = obsID)
93+
sumExpVDraws <- rowsum(expVDraws, group = obsID, reorder = FALSE)
9494
repTimes <- rep(as.numeric(table(obsID)), each = numDraws)
9595
sumExpVDrawsMat <- matrix(rep(sumExpVDraws, times = repTimes),
9696
ncol = numDraws, byrow = FALSE
@@ -283,7 +283,7 @@ mxlNegGradLL_pref <- function(X, parSetup, obsID, choice, standardDraws,
283283
partial_mu <- Xtemp
284284
partial_sigma <- Xtemp * drawsMat
285285
partial <- cbind(partial_mu, partial_sigma)
286-
temp <- rowsum(logitMat * partial, group = obsID)
286+
temp <- rowsum(logitMat * partial, group = obsID, reorder = FALSE)
287287
tempMat <- matrix(rep(temp, times = repTimes),
288288
ncol = ncol(partial),
289289
byrow = F
@@ -426,7 +426,7 @@ mxlNegGradLL_wtp <- function(X, parSetup, obsID, choice, standardDraws,
426426
partial_mu <- cbind(lambda_partial_mu, gamma_partial_mu)
427427
partial_sigma <- cbind(lambda_partial_sigma, gamma_partial_sigma)
428428
partial <- cbind(partial_mu, partial_sigma)
429-
temp <- rowsum(logitMat * partial, group = obsID)
429+
temp <- rowsum(logitMat * partial, group = obsID, reorder = FALSE)
430430
tempMat <- matrix(rep(temp, times = repTimes),
431431
ncol = ncol(partial),
432432
byrow = F

foo.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
library(logitr)
3+
library(tidyverse)
4+
5+
obsIDs <- unique(yogurt$obsID)
6+
newIDs1 <- sample(seq(3000), length(obsIDs), replace = FALSE)
7+
8+
yogurt_new <- yogurt %>%
9+
left_join(
10+
data.frame(
11+
obsID = obsIDs,
12+
newIDs = newIDs1
13+
), by = "obsID"
14+
)
15+
16+
model <- logitr(
17+
data = yogurt,
18+
choiceName = 'choice',
19+
obsIDName = 'obsID',
20+
parNames = c('price', 'feat', 'hiland', 'yoplait', 'dannon')
21+
)
22+
23+
model_new1 <- logitr(
24+
data = yogurt_new,
25+
choiceName = 'choice',
26+
obsIDName = 'newIDs',
27+
parNames = c('price', 'feat', 'hiland', 'yoplait', 'dannon')
28+
)
29+
30+
model_new2 <- logitr(
31+
data = yogurt_new %>% arrange(newIDs),
32+
choiceName = 'choice',
33+
obsIDName = 'newIDs',
34+
parNames = c('price', 'feat', 'hiland', 'yoplait', 'dannon')
35+
)
36+
37+
summary(model)
38+
summary(model_new1)
39+
summary(model_new2)
40+
41+
cbind(coef(model), coef(model_new1), coef(model_new2))
42+
43+
44+
45+
46+
47+
devtools::load_all()
48+
49+
data = yogurt_new1
50+
choiceName = 'choice'
51+
obsIDName = 'newIDs'
52+
parNames = c('price', 'feat', 'hiland', 'yoplait', 'dannon')
53+
priceName = NULL
54+
randPars = NULL
55+
randPrice = NULL
56+
modelSpace = "pref"
57+
weightsName = NULL
58+
options = list()
59+
60+
61+

inst/CITATION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ citEntry(
55
title = "logitr: Random Utility Logit Models with Preference and Willingness to Pay Space Parameterizations",
66
author = "John Paul Helveston",
77
year = "2020",
8-
note = "R package version 0.1.0",
8+
note = "R package version 0.1.2",
99
url = "https://jhelvy.github.io/logitr/",
10-
textVersion = "John Paul Helveston (2021). logitr: Random utility logit models with preference and willingness to pay space parameterizations. R package version 0.1.0."
10+
textVersion = "John Paul Helveston (2021). logitr: Random utility logit models with preference and willingness to pay space parameterizations. R package version 0.1.2."
1111
)

0 commit comments

Comments
 (0)