Run
getSlurmExamples
at the command line ofrnd
orvleda
to have all the tutorials copied to your home directory.
In this tutorial, you will run an R script. This script generates simulated data points with random noise, fits a cubic smoothing spline to the data, and plots the data points, original model, and smoothing spline.
R Script
# fitspline.R
# Set the initial seed for the random number generator.
set.seed(sample(1:1000, 1))
# Create n = 100 random data points.
# x is n equally spaced values from 0 to 1.
n <- 100
x <- (1:n)/n
# The model in this simulation (no random error)
mval <- ((exp(x/3) - 2 * exp(-7 * x) + sin(9 * x)) + 1)/3
# Generate n independent normal random variates with mean 0
# and variance derived from the task id
tid <- as.integer(Sys.getenv("SLURM_TASK_PID"))
v <- tid/100
noise <- rnorm(n, 0, v)
# Simulated observed values (model value + noise)
y <- mval + noise
# Alternatively, you can read data from a file:
# dat <- read.table("dataset.dat", header = TRUE)
# attach(dat)
# Fit a cubic smoothing spline to the data
# Use GCV score and all basis functions
fit <- smooth.spline(x, y, cv = FALSE, all.knots = TRUE)
# Create a graph that shows the data, the smoothing spline,
# and the original model
r <- paste("result_", tid, ".ps", sep = "")
postscript(r, height = 8, width = 10, horizo = FALSE)
# Plot data points
plot(x, y, xlab = "x", ylab = "y", cex = 0.5)
# Plot original model values without noise
lines(x, mval, lty = 2)
# Plot smooth spline fit
lines(fit$x, fit$y)
# Save the graph to a PS file
graphics.off()
# To view with Ghostscript, at the
# command line type: gs result_X.ps
# where X is the corresponding task id
Slurm Script
#!/bin/bash
#
# [ fitspline.sbatch ]
#
# This script demonstrates how to run an R job, specifically,
# how to fit a smooth spline model. It uses a range of task_IDs
# as a noise parameter for the model.
# --------------------------------------------------------------------
#
# Your job begins here ...
module purge # clear all modules
module load R/4.0.2 # load R version 4.0.02
# Run the R script
R CMD BATCH --slave fitspline.R fitspline.$SLURM_TASK_PID.Rout
To run the SLURM script, you can execute this command:
sbatch fitspline.sbatch