| Title: | Companion to the Book "Computational Statistics with R" |
|---|---|
| Description: | Provides data sets and functions used in the book "Computational Statistics with R" (<https://cswr.nrhstat.org>). |
| Authors: | Niels Richard Hansen [aut] (ORCID: <https://orcid.org/0000-0003-3883-365X>), Johan Larsson [aut, cre] (ORCID: <https://orcid.org/0000-0002-4029-5945>) |
| Maintainer: | Johan Larsson <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.3.9000 |
| Built: | 2026-06-02 10:22:28 UTC |
| Source: | https://github.com/jolars/cswr-package |
Subsetting tracer objects
## S3 method for class 'tracer' x[i, j, ..., drop = TRUE]## S3 method for class 'tracer' x[i, j, ..., drop = TRUE]
x |
a tracer object. |
i |
the indices of the trace information to extract. |
j |
currently ignored. |
... |
other arguments passed on to |
drop |
simplify a list with one element to a vector if TRUE. |
a list
tr <- tracer("i", Delta = 0) for (i in 1:3) { tr$tracer() } tr[1]tr <- tracer("i", Delta = 0) for (i in 1:3) { tr$tracer() } tr[1]
The phi and psi torsion angles between peptide planes for the human protein 1HMP.
angleangle
A data frame with 419 rows and 5 columns:
chaina character. The chain (either A or B) of the protein
AAa character. The three-letter amino acid name
posan integer. Position in the amino acid sequence
phinumeric. The phi angle
psinumeric. The psi angle
https://warwick.ac.uk/fac/sci/moac/people/students/peter_cock/r/ramachandran
Plots the value of a traced object on a log-scale against runtime.
## S3 method for class 'tracer' autoplot(object, y, ...) ## S3 method for class 'trace' autoplot(object, y, log = TRUE, ...)## S3 method for class 'tracer' autoplot(object, y, ...) ## S3 method for class 'trace' autoplot(object, y, log = TRUE, ...)
object |
a trace or tracer object |
y |
the name of the traced object to plot |
... |
additional arguments passed to |
log |
logical. Should the y-axis be on a log-scale. Default is TRUE. |
a ggplot object
tr <- tracer("i", Delta = 0) for (i in 1:3) { tr$tracer() } ggplot2::autoplot(tr, i)tr <- tracer("i", Delta = 0) for (i in 1:3) { tr$tracer() } ggplot2::autoplot(tr, i)
Forces the evaluation of all arguments in the calling environment of this function.
force_all(...)force_all(...)
... |
arguments captured by ... are also evaluated if passed via ... |
Use force_all() as syntactic sugar to force evaluation of
all arguments to a function and thereby circumvent lazy evaluation.
If called from within a function with ... as formal argument,
use force_all(...) to force evaluation of arguments captured
by ....
NULL invisibly
affine <- function(a, b) { function(x) a * x + b } a <- 1 b <- 1 affine_11 <- affine(a, b) a <- 2 b <- 2 affine_11(1) # Gives 4 and not 2 due to lazy evaluation affine_forced <- function(a, b) { force_all() function(x) a * x + b } a <- 1 b <- 1 affine_11 <- affine_forced(a, b) a <- 2 b <- 2 affine_11(1) # Gives 2affine <- function(a, b) { function(x) a * x + b } a <- 1 b <- 1 affine_11 <- affine(a, b) a <- 2 b <- 2 affine_11(1) # Gives 4 and not 2 due to lazy evaluation affine_forced <- function(a, b) { force_all() function(x) a * x + b } a <- 1 b <- 1 affine_11 <- affine_forced(a, b) a <- 2 b <- 2 affine_11(1) # Gives 2
Monthly average temperatures in degree Celsius in Nuuk and Qaqortoq from 1873 to 2013.
greenlandgreenland
A data frame with 1692 rows and 5 columns:
Year (numeric)
Month (integer, 1 to 12)
Monthly mean temperature in Nuuk
Monthly mean temperature in Qaqortoq
Temperature difference, Temp_nuuk - Temp_Qaqortoq
https://crudata.uea.ac.uk/cru/data/greenland/
A dataset containing features about articles published by Mashable in a period of two years from January 7, 2013, to January 7, 2015. The purpose of collecting the data was to predict the number of shares of the news articles on social networks. Compared to the source, seven features are excluded. Two (url and a timestamp) are not relevant predictors and five are redundant or almost redundant, leading to collinearity.
newsnews
A data frame with 39,644 rows and 54 columns:
Number of words in the title
Number of words in the content
Rate of unique words in the content
Number of links
Number of links to other articles published by Mashable
Number of images
Number of videos
Average length of the words in the content
Number of keywords in the metadata
Is data channel 'Lifestyle'?
Is data channel 'Entertainment'?
Is data channel 'Business'?
Is data channel 'Social Media'?
Is data channel 'Tech'?
Is data channel 'World'?
Worst keyword (min. shares)
Worst keyword (max. shares)
Worst keyword (avg. shares)
Best keyword (max. shares)
Best keyword (avg. shares)
Avg. keyword (min. shares)
Avg. keyword (max. shares)
Avg. keyword (avg. shares)
Min. shares of referenced articles in Mashable
Avg. shares of referenced articles in Mashable
Was the article published on a Monday?
Was the article published on a Tuesday?
Was the article published on a Wednesday?
Was the article published on a Thursday?
Was the article published on a Friday?
Was the article published on a Saturday?
Was the article published on a Sunday?
Closeness to LDA topic 0
Closeness to LDA topic 1
Closeness to LDA topic 2
Closeness to LDA topic 3
Closeness to LDA topic 4
Text subjectivity
Text sentiment polarity
Rate of positive words in the content
Rate of negative words in the content
Rate of positive words among non-neutral tokens
Rate of negative words among non-neutral tokens
Avg. polarity of positive words
Min. polarity of positive words
Max. polarity of positive words
Avg. polarity of negative words
Min. polarity of negative words
Max. polarity of negative words
Title subjectivity
Title polarity
Absolute subjectivity level
Absolute polarity level
Number of shares (target)
https://archive.ics.uci.edu/dataset/332/online+news+popularity
Annual summaries of monthly mean temperatures in Nuuk from 1867 to 2013.
nuuknuuk
A data frame with 147 rows and 6 columns:
Year (numeric)
Annual mean temperature
Annual median of monthly mean temperatures
Annual maximum of monthly mean temperatures
Annual minimum of monthly mean temperatures
High - Low
https://crudata.uea.ac.uk/cru/data/greenland/
The plotter function returns a quoted expression that adds points to a current plot. For use with tracer and terminator objects to iteratively update plots during long running function evaluations.
plotter(y, col = "black", lty = "solid", pch = 1)plotter(y, col = "black", lty = "solid", pch = 1)
y |
a name of a symbol to plot |
col |
point and line color |
lty |
line type |
pch |
plot symbol |
a quoted expression
plotter("i")plotter("i")
Print terminator information
## S3 method for class 'terminator' print(x, ...)## S3 method for class 'terminator' print(x, ...)
x |
a terminator object |
... |
other arguments (currently ignored) |
The list of objects from the terminator's evaluation environment, returned invisibly.
term <- terminator(quote(i >= 3), print = FALSE) for (i in 1:5) { if (term$terminator()) { break } } print(term, all.names = TRUE)term <- terminator(quote(i >= 3), print = FALSE) for (i in 1:5) { if (term$terminator()) { break } } print(term, all.names = TRUE)
A random number stream uses a vectorized random number generator to generate a cache of random numbers that can then be used sequentially. Whenever the cache runs empty new numbers are generated automatically.
rng_stream(m, rng, ...)rng_stream(m, rng, ...)
m |
initial cache size |
rng |
a random number generator |
... |
additional arguments passed to the random number generator |
A function that extracts random numbers from the cache and fills the cache whenever it runs empty.
runif_stream <- rng_stream(10, runif, min = -1, max = 1) runif_stream()runif_stream <- rng_stream(10, runif, min = -1, max = 1) runif_stream()
Summarize and print trace information
## S3 method for class 'tracer' summary(object, ...) ## S3 method for class 'tracer' print(x, ...)## S3 method for class 'tracer' summary(object, ...) ## S3 method for class 'tracer' print(x, ...)
object |
a tracer object |
... |
other arguments (currently ignored) |
x |
a tracer object |
summary returns a data frame (of class trace) with columns
containing the values
of the traced objects, and if time is traced an additional column, .time,
containing the cumulative runtime in seconds.
tr <- tracer("i", Delta = 0) for (i in 1:3) { tr$tracer() } summary(tr) print(tr)tr <- tracer("i", Delta = 0) for (i in 1:3) { tr$tracer() } summary(tr) print(tr)
Terminator objects are used to write termination conditions that can be tested via a callback function within another function during its evaluation.
terminator(cond = FALSE, Delta = 1, print = TRUE, plotter = NULL, ...)terminator(cond = FALSE, Delta = 1, print = TRUE, plotter = NULL, ...)
cond |
a termination condition. Either an expression or call that evaluates to a logical. |
Delta |
an integer specifying how often the termination condition
is evaluated. |
print |
a variable name to print or a logical. If FALSE, nothing is
printed. If a variable name is given, that variable (if it exists)
will be printed every |
plotter |
an expression, possibly created by the |
... |
other arguments passed to |
Terminator objects are similar to tracer objects but serve a different purpose. Like tracer objects, they can be used to trace, print and plot values of a variable within the evaluation environment of another function during its evaluation. The primary purpose of a terminator object is, however, to evaluate a termination condition, which can trigger termination of a loop. Terminator objects do not save trace information and do not trace runtime.
A terminator object containing the functions terminator and
clear.
term <- terminator(quote(i >= 3), print = FALSE) for (i in 1:5) { if (term$terminator()) { break } } iterm <- terminator(quote(i >= 3), print = FALSE) for (i in 1:5) { if (term$terminator()) { break } } i
Tracer objects can collect, print and summarize trace information from the evaluation environment of other functions during their evaluation.
tracer( objects = NULL, Delta = 1, save = TRUE, time = TRUE, expr = NULL, plotter = NULL, ... )tracer( objects = NULL, Delta = 1, save = TRUE, time = TRUE, expr = NULL, plotter = NULL, ... )
objects |
a character vector of names of the objects that are to be traced.
The objects are searched for in the calling environment of the
|
Delta |
an integer specifying if and how often trace information is printed.
|
save |
a logical value. Determines if the trace information is stored. |
time |
a logical value. Determines if runtime information in seconds is traced. |
expr |
an expression that will be evaluated in an environment that has
the calling environment of the |
plotter |
an expression, possibly created by the |
... |
other arguments passed to |
The function tracer constructs a tracer object containing a
tracer, a get and a clear function. A call of
the objects tracer function can
be inserted in other functions and used to collect, print and plot trace information
about the internals of that function during its evaluation. The objects get
function can access that information afterwards, and its clear function deletes
all stored values in the tracer object.
A tracer object can trace time (in seconds) between tracer calls,
which are measured by
the hires_time function from the bench package. There are print
and summary methods available for summarizing the trace information.
A call of the tracer function can be manually inserted into the body of
the function that is to be traced, it can be inserted using base::trace,
or it can be passed as an argument to any function with a callback argument.
A tracer object containing the functions tracer, get and
clear.
test_tracer <- tracer(c("m", "m_sq"), expr = quote(m_sq <- m^2)) test <- function(n, cb = NULL) { for(i in 1:n) { m <- 2 * i Sys.sleep(0.1) if (!is.null(cb)) cb() } } test(10, test_tracer$tracer) summary(test_tracer)test_tracer <- tracer(c("m", "m_sq"), expr = quote(m_sq <- m^2)) test <- function(n, cb = NULL) { for(i in 1:n) { m <- 2 * i Sys.sleep(0.1) if (!is.null(cb)) cb() } } test(10, test_tracer$tracer) summary(test_tracer)
Weekly sale of frozen vegetables in different stores.
vegetablesvegetables
A data frame with 1066 rows and 3 columns:
salea numeric vector. Number of items sold in a week
normalSalea numeric vector. Estimated normal sale that week
storea character vector. Id of the different stores
Obtained by package author.