Help a new user run flipMaxDiff

Hi there,
New Stan user here and am struggling, and hoping someone can find time to help.

I have setup Stan and rtools43 according to instructions here:

All seems to run fine and I can load the rstan library into RStudio.

I’m now trying to use the flipMaxDiff package from here GitHub - pelishk/flipMaxDiff to run a Hierarchical Bayes model using the example datasets from here: How to analyze max-diff data in R | R-bloggers

Data and design seem to import perfectly, but am receiving the error messages below when I try to run the FitMaxDiff function from the flipMaxDiff package.

My code is reproduced below the error messages.

ERROR MESSAGES:
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(path.expand(path), winslash, mustWork) :
  path[1]="exec/hb.stan": The system cannot find the path specified
2: In file(fname, "rt") :
  cannot open file 'C:\Users\SEJE\OneDrive - CompanyName\Desktop\R Development Playground\exec\hb.stan': No such file or directory
Error in get_model_strcode(file, model_code) : 
  cannot open model file "C:\Users\SEJW\OneDrive - CompanyName\Desktop\R Development Playground\exec\hb.stan"

MY CODE:

remove.packages("rstan")
if (file.exists(".RData")) file.remove(".RData")

Sys.setenv(DOWNLOAD_STATIC_LIBV8 = 1) # only necessary for Linux without the nodejs library / headers
install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)

example(stan_model, package = "rstan", run.dontrun = TRUE, verbose = TRUE)

library("rstan") # observe startup messages
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

library("StanHeaders")
library("flipMaxDiff")

# Import the design
tech.design = read.csv("Technology_MaxDiff_Design.csv")
View(tech.design)

# Import the full dataset
tech.data = foreign::read.spss("Technology_2017.sav", to.data.frame = TRUE)
View(tech.data)

# Create two new *vectors* which pull in the best and worst variables
best = tech.data[, c("Q5a_left", "Q5b_left", "Q5c_left", 
                     "Q5d_left", "Q5e_left", "Q5f_left")]
worst = tech.data[, c("Q5a_right", "Q5b_right", "Q5c_right", 
                      "Q5d_right", "Q5e_right", "Q5f_right")]


# Name the alternatives in the design
alt.names <- c("Apple", "Microsoft", "IBM", "Google", "Intel", 
               "Samsung", "Sony", "Dell", "Yahoo", "Nokia")
result.hb = FitMaxDiff(design = tech.design, 
                    version = rep(1, nrow(best)),   # IN THIS EXAMPLE THERE IS ONLY ONE VERSION SO THIS IS CREATING A VECTOR OF ALL 1S FOR AS MANY RO2WS AS THERE ARE 'BEST' RESPONSES 
                    best = best, 
                    worst = worst, 
                    alternative.names = alt.names,
                    algorithm = "HB",
                    hb.iterations = 100,
                    hb.chains = 8,
                    hb.max.tree.depth = 10)
print(result.hb)

[edit: escaped code]

Hi, @SEJW and welcome to the Stan forums.

I looked up flipMaxDiff and it’s an R package that depends on RStan.

So the first step is to install RStan. Edit: I missed where you first said you could execute library(rstan). Can you try actually fitting a model directly with RStan? I think the install instructions have a “Hello, World!” example.

What platform are you working on? Which version of R?

Hi Bob,
Thank so much for your welcome and your help!

Quick answers first…
I’m working on a Windows machine with R version 4.4.1 and RStudio 2024.04.2.

I’ve been able to run “Example 1: Eight Schools” from the RStan getting started page successfully.

But this has made me think… Do I need a .stan file for every project that I want to call on RStan?

I had thought that the FitMaxDiff function from within the flipMaxDiff package was passing on the relevant information but as the error messages seem to mostly concern not finding specified paths and files, the lack of a .stan file could be the issue?

As an update, I’ve gotten it to work.
The issue seemed to be that there was a file (hb.stan) missing from my working directory. Having now copied that file from where the r package is saved solved the problem, and I have models running.

Thank you for taking the time to to help me out up front 👍

1 Like

Thanks for following up with the solution. We appreciate it.