Calling SPSS Modeler from R

SPSS Modeler streams can be executed from R via input files and command-line calls. It's a hacky technique, but it works. And it's an easy way to make use of Modeler's excellent Expert Modeler functionality.

First, create an example of the data file that you want Modeler to read in. For example, if you want to run Modeler on a single time series, your data file will probably be a text file comprising a date column and a value column.

Then create and save a Modeler stream that reads in that file, fits the required model and produces an output file. This is fairly easy so I won't cover it here. But this is how it might look:

An example of an SPSS Modeler stream.

Next, you need to write R code to:

  1. Save data to the file that serves as the input to Modeler.
  2. Run clemb.exe on your Modeler stream from the command line.
  3. Read in the results.

Here's an example running on Windows:

# Write as a file the input to SPSS Modeler.
write.table(my_data_frame, "C:/Users/prosenmai/Desktop/series.txt", sep="\\t", row.names=FALSE, na="")

# Run the analyse.str SPSS Modeler stream on that file.
try(system('"C:\\\\Program Files\\\\IBM\\\\SPSS\\\\Modeler\\\\15\\\\bin\\\\clemb" -stream "C:\\\\Users\\\\prosenmai\\\\Desktop\\\\analyse.str" -execute -log "C:\\\\Users\\\\prosenmai\\\\Desktop\\\\log.txt"'))

# Read in the output of SPSS Modeler.
df_output_new <- read.table("C:/Users/prosenmai/Desktop/output.txt", sep="\\t", header=TRUE)

The system() function runs synchronously by default, but it may be run asynchronously. In that case, you'll want to periodically check the timestamp on the output file to see whether the call to Modeler has finished.

And if your call to Modeler doesn't work, try looking in the log file specified by the -log option in the system call.