Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

summary function and plots not parsing #4

Open
rpbradystadavis opened this issue Nov 14, 2023 · 0 comments
Open

summary function and plots not parsing #4

rpbradystadavis opened this issue Nov 14, 2023 · 0 comments

Comments

@rpbradystadavis
Copy link

the_table function does not work until I comment out line 502 https://github.com/radiant-rstats/radiant.multivariate/blob/master/R/conjoint.R

It seems lm call is calling factors not by names but enumerating them. Was able to run conjoint with:

the_table = function (model, dataset, evar) {
  if (is.character(model)) {
    return(list(PW = "No attributes selected."))
  }
  attr <- select_at(dataset, .vars = evar) %>% mutate_if(is.logical, 
                                                         as.factor) %>% mutate_if(is.character, as.factor)
  isFct <- sapply(attr, is.factor)
  if (sum(isFct) < ncol(attr)) {
    return(list(PW = "Only factors can be used.", IW = "Only factors can be used."))
  }
  bylevs <- lapply(attr[, isFct, drop = FALSE], levels)
  vars <- colnames(attr)[isFct]
  nlevs <- sapply(bylevs, length)
  PW.df <- data.frame(rep(vars, nlevs), unlist(bylevs), stringsAsFactors = FALSE)
  colnames(PW.df) <- c("Attributes", "Levels")
  PW.df$PW <- 0
  
  coeff <- model$estimate
  PW.df[model$term[-1], "PW"] <- coeff[-1]
  minPW <- PW.df[tapply(1:nrow(PW.df), PW.df$Attributes, function(i) i[which.min(PW.df$PW[i])]), 
  ]
  maxPW <- PW.df[tapply(1:nrow(PW.df), PW.df$Attributes, function(i) i[which.max(PW.df$PW[i])]), 
  ]
  rownames(minPW) <- minPW$Attributes
  rownames(maxPW) <- maxPW$Attributes
  rangePW <- data.frame(cbind(maxPW[vars, "PW"], minPW[vars, 
                                                       "PW"]), stringsAsFactors = FALSE)
  rangePW$Range <- rangePW[[1]] - rangePW[[2]]
  colnames(rangePW) <- c("Max", "Min", "Range")
  rownames(rangePW) <- vars
  maxlim <- rangePW[["Max"]] > abs(rangePW[["Min"]])
  maxrange <- max(rangePW[["Range"]])
  plot_ylim <- rangePW[c("Min", "Max")]
  plot_ylim[maxlim, "Max"] <- plot_ylim[maxlim, "Max"] + maxrange - 
    rangePW$Range[maxlim]
  plot_ylim[!maxlim, "Min"] <- plot_ylim[!maxlim, "Min"] - 
    (maxrange - rangePW$Range[!maxlim])
  plot_ylim <- plot_ylim * 1.01
  IW <- data.frame(vars, stringsAsFactors = FALSE)
  IW$IW <- rangePW$Range/sum(rangePW$Range)
  colnames(IW) <- c("Attributes", "IW")
  PW.df[["Attributes"]] <- as.character(PW.df[["Attributes"]])
  PW.df[["Levels"]] <- as.character(PW.df[["Levels"]])
  PW.df <- rbind(PW.df, c("Base utility", "~", coeff[1]))
  PW.df[["PW"]] <- as.numeric(PW.df[["PW"]])
  PW.df[["PW"]] <- round(PW.df[["PW"]], 3)
  IW[["IW"]] <- round(IW[["IW"]], 3)
  list(PW = PW.df, IW = IW, plot_ylim = plot_ylim)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant