Skip to content

summary function and plots not parsing #4

@rpbradystadavis

Description

@rpbradystadavis

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions