| Title: | Support for Compiling Examination Tasks using the 'exams' Package |
|---|---|
| Description: | The main aim is to further facilitate the creation of exercises based on the package 'exams' by Grün, B., and Zeileis, A. (2009) <doi:10.18637/jss.v029.i10>. Creating effective student exercises involves challenges such as creating appropriate data sets and ensuring access to intermediate values for accurate explanation of solutions. The functionality includes the generation of univariate and bivariate data including simple time series, functions for theoretical distributions and their approximation, statistical and mathematical calculations for tasks in basic statistics courses as well as general tasks such as string manipulation, LaTeX/HTML formatting and the editing of XML task files for 'Moodle'. |
| Authors: | Sigbert Klinke [aut, cre] (ORCID: <https://orcid.org/0000-0003-3337-1863>), Kleio Chrysopoulou Tseva [ctb] |
| Maintainer: | Sigbert Klinke <[email protected]> |
| License: | GPL-3 |
| Version: | 1.0.13 |
| Built: | 2026-06-03 08:47:13 UTC |
| Source: | https://github.com/sigbertklinke/exams.forge |
The exams.forge package was created with the main goal of "forging" exam
tasks in combination with the exams package, and it includes additional
functions to simplify the creation of Moodle exercises.
The package features various functions categorized into seven groups based on
their characteristics. These categories are named:
Data Conversion and Modification, Statistical Analysis,
Mathematical Computations, Exercise Generation, String Manipulation,
LaTeX and HTML Functions, and General Purpose Functions.
This package is designed for educators who need to develop examination materials in the field of statistics, particularly for introductory courses like Statistics I and II, using the R programming language. The aim is to streamline the process of creating a large number of assessment items, enabling instructors to focus on improving the quality of the tasks themselves.
We would like to acknowledge the support provided by the Multimedia Funding Program. Their assistance has been invaluable to our project, and we extend our sincere gratitude for their contributions.
Feature 1: exams.forge simplifies the generation of examination tasks by providing tools to
create a diverse array of statistical exercises, ensuring unique problem sets for each student.
Feature 2: It includes functions for precise data conversion, statistical analysis, and mathematical computations, enhancing the accuracy and relevance of generated exercises.
Feature 3: The package supports multi-format rendering, allowing the seamless creation of LaTeX and HTML documents suitable for various educational platforms, such as Moodle.
Examples of functions included in the package:
ts_data: Creates a univariate time series by combining elements of a
linear or exponential trend, additive or multiplicative seasonal adjustment, and white noise.
The resulting time series is structured as a ts_data object, allowing for further analysis and exploration.
lmatrix: Creates a LaTeX or HTML representation of a matrix.
This function is useful for integrating well-formatted matrices into LaTeX or HTML documents.
as_obs: Creates a string representing observations with optional sorting
and LaTeX formatting, useful for generating readable data representations in educational materials.
Example usage of the package and its functions.
library(exams.forge)
# Generate a time series with specified parameters
ts_eg <- ts_data(end = 20, trend = TRUE, trend.coeff = c(1, 0.5), season = TRUE, season.coeff = c(0.2, 0.1), error = TRUE, error.coeff = 0.1, digits = 2)
print(ts_eg)
# Create a matrix
mx_data <- matrix(1:6, ncol = 2)
# Generate a LaTeX representation of the matrix with tooltip
eg_matrix <- lmatrix(
m = mx_data,
title = "Example LaTeX Matrix",
fmt = "
byrow = TRUE,
tooltip = "Die Tabelle hat
cat(eg_matrix)
# Create a string representation of observations
observations <- c(10, 20, 30, 40, 50)
observation_string <- as_obs(observations, last = " and ")
print(observation_string)
To install this package please use the following command:
install.packages("exams.forge")
Sigbert Klinke, Affiliation: Humboldt University of Berlin, School of Business and Economics, Chair of Statistics.
Sigbert Klinke [email protected]
Gnu General Public License 3.
Maintainer: Sigbert Klinke [email protected] (ORCID)
Other contributors:
Kleio Chrysopoulou Tseva [email protected] [contributor]
Generates and appends additional random values to the left and/or right ends of a numeric vector x.
The range from which these values are drawn is determined by a specified "box" and a scaling factor.
add_data(x, box, n = c(0, 1), range = c(0, 1))add_data(x, box, n = c(0, 1), range = c(0, 1))
x |
|
box |
|
n |
|
range |
|
The "box" defines the central range of the data and can be:
"boxplot" — uses the 25th and 75th percentiles (quantile(x, c(0.25, 0.75), na.rm = TRUE)).
"range" — uses the full range of the data (range(x, na.rm = TRUE)).
A numeric vector of length two — used directly as the box boundaries.
The box length is the distance between the lower and upper box boundaries.
The range parameter specifies how far left or right new values can be drawn,
as a multiple of the box length.
For the left side, values are drawn uniformly from:
For the right side, values are drawn uniformly from:
The n parameter controls how many values are added:
Single number — adds that many values to the right side only.
Length-two vector — n[1] values to the left, n[2] values to the right.
A numeric vector containing the original values from x plus the newly generated values.
x <- rnorm(8) # Add one value to the right add_data(x, "box", range = 1.5) # Add one value to the right using data range add_data(x, "range", range = 0.1) # Add one value to the right, larger possible range add_data(x, "box", range = c(1.5, 3)) # Add two values to the right add_data(x, "range", n = 2, range = 0.1) # Add two values to the left and three to the right add_data(x, "range", n = c(2, 3), range = 0.1)x <- rnorm(8) # Add one value to the right add_data(x, "box", range = 1.5) # Add one value to the right using data range add_data(x, "range", range = 0.1) # Add one value to the right, larger possible range add_data(x, "box", range = c(1.5, 3)) # Add two values to the right add_data(x, "range", n = 2, range = 0.1) # Add two values to the left and three to the right add_data(x, "range", n = c(2, 3), range = 0.1)
A set of helper functions for adding or removing specific prefixes and/or suffixes to character vectors. This is useful for formatting strings in mathematical, XML, or other structured text contexts.
affix() – Add any prefix and/or suffix to each element of a character vector; alias: add_affix().
math() – Add a dollar sign ($) to both ends of each element (LaTeX-style math); aliases: add_math(), lmath().
bracket() – Wrap each element in parentheses; aliases: add_bracket(), brkt().
cdata() – Wrap each element in <![CDATA[ ... ]]> (XML CDATA section); alias: add_cdata().
unaffix() – Remove a given prefix and/or suffix from each element; alias: remove_affix().
unquote() – Remove surrounding double quotes from each element; alias: remove_quotes().
uncdata() – Remove a surrounding CDATA section from each element.; alias: remove_cdata().
affix(txt, prefix = "", suffix = "") math(txt) bracket(txt) unaffix(txt, prefix = "", suffix = "") unquote(txt) uncdata(txt) cdata(txt) add_affix(txt, prefix = "", suffix = "") add_cdata(txt) add_math(txt) lmath(txt) add_bracket(txt) brkt(txt) remove_affix(txt, prefix = "", suffix = "") remove_quotes(txt) remove_cdata(txt)affix(txt, prefix = "", suffix = "") math(txt) bracket(txt) unaffix(txt, prefix = "", suffix = "") unquote(txt) uncdata(txt) cdata(txt) add_affix(txt, prefix = "", suffix = "") add_cdata(txt) add_math(txt) lmath(txt) add_bracket(txt) brkt(txt) remove_affix(txt, prefix = "", suffix = "") remove_quotes(txt) remove_cdata(txt)
txt |
|
prefix |
|
suffix |
|
A modified character vector of the same length as txt.
x <- c("alpha", "beta", "gamma") # Add a prefix and suffix affix(x, "[", "]") #> [1] "[alpha]" "[beta]" "[gamma]" # Wrap with LaTeX math delimiters math(x) #> [1] "$alpha$" "$beta$" "$gamma$" # Remove quotes quoted <- c('"a"', '"b"') unquote(quoted) #> [1] "a" "b" # Wrap and unwrap CDATA cdata_x <- cdata("text") uncdata(cdata_x)x <- c("alpha", "beta", "gamma") # Add a prefix and suffix affix(x, "[", "]") #> [1] "[alpha]" "[beta]" "[gamma]" # Wrap with LaTeX math delimiters math(x) #> [1] "$alpha$" "$beta$" "$gamma$" # Remove quotes quoted <- c('"a"', '"b"') unquote(quoted) #> [1] "a" "b" # Wrap and unwrap CDATA cdata_x <- cdata("text") uncdata(cdata_x)
These functions determine whether numeric values are sufficiently different from each other based on a specified tolerance.
all_different: Checks if all differences between entries in a numeric object exceed a given tolerance.
values_different: Adds candidate values to a set of initial values only if they are sufficiently different from all existing values.
all_different(obj, tol) values_different(values, candidates, tol = 0.1)all_different(obj, tol) values_different(values, candidates, tol = 0.1)
obj |
Numeric vector, matrix, or data frame to test for differences. Non-numeric inputs will be coerced to numeric if possible. |
tol |
Numeric scalar specifying the minimum allowable difference between values. |
values |
Numeric vector of initial values. |
candidates |
Numeric vector of candidate values to add if sufficiently different. |
all_different: Logical (TRUE if all differences exceed tol, FALSE otherwise).
values_different: Numeric vector with original values plus any candidates that meet the difference criterion.
# Check if all values are sufficiently different x <- runif(10) # 10 random values between 0 and 1 all_different(x, tol = 0.01) all_different(x, tol = 0.5) # Add sufficiently different candidate values starting_values <- c(0.1, 0.5, 0.9) candidates <- c(0.15, 0.4, 0.8, 1.2) values_different(starting_values, candidates, tol = 0.2)# Check if all values are sufficiently different x <- runif(10) # 10 random values between 0 and 1 all_different(x, tol = 0.01) all_different(x, tol = 0.5) # Add sufficiently different candidate values starting_values <- c(0.1, 0.5, 0.9) candidates <- c(0.15, 0.4, 0.8, 1.2) values_different(starting_values, candidates, tol = 0.2)
Rounds x according to digits and the rounding function FUN, and sets
a tolerance for the result. If tol is not provided, it defaults to 2*10^(-digits).
as_result(x, digits, tol = NA, FUN = round2) tol(x) rounded(x) val(x) digits(x) as_res(x, digits, tol = NA, FUN = round2) tolerance(x)as_result(x, digits, tol = NA, FUN = round2) tol(x) rounded(x) val(x) digits(x) as_res(x, digits, tol = NA, FUN = round2) tolerance(x)
x |
numeric: value to round |
digits |
integer or character: Number of digits to use for rounding, see Details. |
tol |
numeric: tolerance for the result (defaults to |
FUN |
function: rounding function (default: internal |
Results with Rounding
Creates a structured result with a numeric value rounded according to specified digits, an optional tolerance, and a rounding function.
By default, rounding is performed using an internal function round2(), which is similar
to exams::round2(), but users should not call it directly. You can also supply a
custom rounding function via the FUN argument.
If digits is a character, the following abbreviations are recognized:
"integer"digits = 0
"%"digits = 2
"probability"digits = 4
Partial matching of these names is allowed.
A list of class result containing:
x |
Original value |
r |
Rounded value |
digits |
Digits used for rounding |
tol |
Tolerance for the result |
x <- as_result(1/3, "probability") tol(x) rounded(x) digits(x)x <- as_result(1/3, "probability") tol(x) rounded(x) digits(x)
These functions convert vectors into human-readable string representations. They can join elements, create LaTeX-formatted fractions, or label observations.
as_string(txt, collapse = ", ", last = ", and ") as_sum(txt) as_obs(txt, name = "x", sorted = FALSE, ...) as_fraction(val, latex = FALSE, sorted = FALSE, ...) lobs(txt, name = "x", sorted = FALSE, ...) lstring(txt, collapse = ", ", last = ", and ") lfrac(val, latex = FALSE, sorted = FALSE, ...)as_string(txt, collapse = ", ", last = ", and ") as_sum(txt) as_obs(txt, name = "x", sorted = FALSE, ...) as_fraction(val, latex = FALSE, sorted = FALSE, ...) lobs(txt, name = "x", sorted = FALSE, ...) lstring(txt, collapse = ", ", last = ", and ") lfrac(val, latex = FALSE, sorted = FALSE, ...)
txt |
Character vector to merge into a single string (used in |
collapse |
Character string inserted between elements (default: |
last |
Character string used between the last two elements (default: |
name |
Character string used as the observation name (default: |
sorted |
Logical; if |
... |
Additional arguments passed to underlying functions. |
val |
Numeric vector of values to convert into fractions (used in |
latex |
Logical; if |
A single string, or a vector of formatted strings (for fractions in as_fraction).
x <- runif(5) y <- c(TRUE, FALSE, NA) # Basic string conversion as_string(x) as_string(y) as_string(as.character(x)) as_string(as.character(y)) # Observations as_obs(x) as_obs(sort(x), sorted = TRUE) # Fraction conversion x <- round(runif(5), 2) as_fraction(x) as_fraction(x, latex = TRUE) # Summing elements as a string y <- round(runif(5), 2) as_sum(y)x <- runif(5) y <- c(TRUE, FALSE, NA) # Basic string conversion as_string(x) as_string(y) as_string(as.character(x)) as_string(as.character(y)) # Observations as_obs(x) as_obs(sort(x), sorted = TRUE) # Fraction conversion x <- round(runif(5), 2) as_fraction(x) as_fraction(x, latex = TRUE) # Summing elements as a string y <- round(runif(5), 2) as_sum(y)
Converts a vector or matrix into a formatted horizontal table using xtable.
The output is returned as a character vector, where each element corresponds
to a line of the table, suitable for printing or further formatting.
as_table( x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ... ) toTable( x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ... )as_table( x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ... ) toTable( x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ... )
x |
An R object of class found among |
caption |
Character vector of length 1 or 2 containing the
table's caption or title. If length is 2, the second item is the
"short caption" used when LaTeX generates a "List of Tables". Set to
|
label |
Character vector of length 1 containing the LaTeX label
or HTML anchor. Set to |
align |
Character vector of length equal to the number of columns
of the resulting table, indicating the alignment of the corresponding
columns. Also, |
digits |
Numeric vector of length equal to one (in which case it will be
replicated as necessary) or to the number of columns of the
resulting table or matrix of the same size as the resulting
table, indicating the number of digits to display in the
corresponding columns. Since the row names are printed in the first
column, the length of the vector |
display |
Character vector of length equal to the number of columns of the
resulting table, indicating the format for the corresponding columns.
Since the row names are printed in the first column, the length of
|
auto |
Logical, indicating whether to apply automatic format when no value
is passed to |
... |
Additional arguments passed to |
Convert a Vector or Matrix to a Horizontal Table
A character vector, each element representing a line of the table.
x <- runif(5) tab <- vec2mat(x, colnames = 1:length(x)) as_table(tab)x <- runif(5) tab <- vec2mat(x, colnames = 1:length(x)) as_table(tab)
Transforms a ts_data object into a standard R ts (time series) object.
as_ts(ts)as_ts(ts)
ts |
A |
The function preserves the original time indices and frequency of the input ts_data.
It calculates the deltat based on the time vector t in ts_data.
A ts object representing the same time series as the input ts_data.
# Create a ts_data object with a linear trend ts_obj <- ts_data(12, trend.coeff = c(sample(0:10, 1), sample(1 + (1:10)/20, 1))) # Convert to standard ts object ts_standard <- as_ts(ts_obj)# Create a ts_data object with a linear trend ts_obj <- ts_data(12, trend.coeff = c(sample(0:10, 1), sample(1 + (1:10)/20, 1))) # Convert to standard ts object ts_standard <- as_ts(ts_obj)
Compute association and correlation measures for categorical and ordinal data.
The following measures are implemented:
nom.cc: Corrected contingency coefficient for nominal data.
nom.cramer: Cramer's V (or Phi) for nominal data.
ord.spearman: Spearman's rank correlation for ordinal data.
ord.kendall: Kendall's rank correlation for ordinal data.
nom.cc(tab, correct = FALSE) nom.cramer(tab, ...) ord.spearman(tab, ...) ord.kendall(tab, ...) cc_coef(tab, correct = FALSE) cramer_vf(tab, ...) cramer_coef(tab, ...) kendall_corr(tab, ...) spearman_corr(tab, ...) rs_corr(tab, ...)nom.cc(tab, correct = FALSE) nom.cramer(tab, ...) ord.spearman(tab, ...) ord.kendall(tab, ...) cc_coef(tab, correct = FALSE) cramer_vf(tab, ...) cramer_coef(tab, ...) kendall_corr(tab, ...) spearman_corr(tab, ...) rs_corr(tab, ...)
tab |
A contingency table (matrix or table) with absolute frequencies. |
correct |
Logical, whether to apply a correction (default: |
... |
Additional parameters passed to correlation functions. |
These functions provide common measures of association:
Nominal data: nom.cc, nom.cramer.
Ordinal data: ord.spearman, ord.kendall.
A numeric value representing the association or correlation measure.
# Create a random contingency table tab <- matrix(round(10 * runif(15)), ncol = 5) # Nominal association nom.cc(tab) nom.cc(tab, correct = TRUE) nom.cramer(tab) # Ordinal correlation ord.spearman(tab) ord.kendall(tab) # Using aliases cc_coef(tab) cramer_vf(tab) spearman_corr(tab) kendall_corr(tab) rs_corr(tab)# Create a random contingency table tab <- matrix(round(10 * runif(15)), ncol = 5) # Nominal association nom.cc(tab) nom.cc(tab, correct = TRUE) nom.cramer(tab) # Ordinal correlation ord.spearman(tab) ord.kendall(tab) # Using aliases cc_coef(tab) cramer_vf(tab) spearman_corr(tab) kendall_corr(tab) rs_corr(tab)
Reorders the entries of a frequency table to approximate a given target association or correlation.
The reordering preserves the marginal frequencies of the table. Note that the target association may not always be achievable, especially for extreme values (e.g., +1, -1, or values near these limits).
assoc_data( tab, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... ) reorder_association_data( tab, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... ) dassoc( tab, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... )assoc_data( tab, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... ) reorder_association_data( tab, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... ) dassoc( tab, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... )
tab |
table A contingency table of absolute frequencies. |
zero |
logical Whether zeros are allowed in the resulting table (default: |
FUN |
function A function that computes the association or correlation from a frequency table (default: |
target |
numeric Desired association or correlation value (default: |
tol |
numeric Maximum allowed deviation between the achieved and target association (default: |
maxit |
integer Maximum number of iterations to reach the target (default: |
... |
Additional parameters passed to |
The function attempts to reorder the table entries to reach the target association. If the target is extreme
(e.g., +1, -1, or values near these limits), a solution may not be possible.
If attr(joint, "iterations") equals maxit, consider increasing maxit,
reducing tol, or choosing a more feasible target value:
Nominal measures:
Ordinal measures:
A frequency table reordered to approximate the target association. The returned object includes attributes:
iterationsNumber of iterations performed.
targetAchieved association or correlation value.
tab <- table_data(3, 2) tab tab2 <- assoc_data(tab, target = 0.5) tab2tab <- table_data(3, 2) tab tab2 <- assoc_data(tab, target = 0.5) tab2
Creates a data frame of possible combinations of n (number of trials)
and p (probabilities) that satisfy specified constraints on the mean, standard deviation,
and approximation conditions for normal or Poisson distributions.
The function applies the following rules:
If length(mean) == 1 and it is an integer, it specifies the number of digits
to which the mean should be rounded.
If mean = NA (default), all mean values are allowed.
If length(mean) > 1, only combinations where n * p equals one of the specified means are retained.
The same logic applies to sd for the standard deviation.
The norm and pois arguments can be logical, NA, or a custom function of the form function(n, p).
They control which (n, p) pairs are considered valid:
NA allows all combinations.
A function returns TRUE for valid combinations and FALSE for invalid ones.
TRUE enforces standard approximation rules:
norm: n * p * (1 - p) > 9 (normal approximation condition)
pois: n > 10 & p < 0.05 (Poisson approximation condition)
FALSE excludes combinations that meet the approximation condition.
Note: The resulting data frame may be empty if no combinations meet all criteria.
binom_param(n, p, mean = NA, sd = NA, norm = NA, pois = NA, tol = 1e-06)binom_param(n, p, mean = NA, sd = NA, norm = NA, pois = NA, tol = 1e-06)
n |
integer vector of trial counts |
p |
numeric vector of probabilities |
mean |
numeric or integer specifying required mean digits or specific mean values |
sd |
numeric or integer specifying required standard deviation digits or specific sd values |
norm |
logical, |
pois |
logical, |
tol |
numeric: tolerance for numerical comparisons (default: |
A data frame with columns n, p, mean, and sd representing valid parameter combinations.
binom_param(1000:50000, (5:25)/100, mean = 0, sd = 0)binom_param(1000:50000, (5:25)/100, mean = 0, sd = 0)
Creates a numeric vector of break points for the given data x.
The resulting breaks define bins that are either equidistant (fixed width)
or non-equidistant (quantile-based). If width is not specified, it defaults
to diff(pretty(x))[1]. The probs argument can either be a single
integer, specifying the number of quantiles, or a numeric vector of probabilities
in the interval .
breaks(x, width = NULL, probs = NULL) add_breaks(x, width = NULL, probs = NULL) dbreaks(x, width = NULL, probs = NULL)breaks(x, width = NULL, probs = NULL) add_breaks(x, width = NULL, probs = NULL) dbreaks(x, width = NULL, probs = NULL)
x |
numeric vector: the data to compute breaks for. |
width |
numeric, optional: desired bin width (default: |
probs |
numeric, optional: number of quantiles (single integer) or
vector of probabilities in |
If probs is used, break points are rounded to the nearest multiple of width.
Duplicates are removed, and the range is extended if necessary to include the
full range of x.
A numeric vector containing the break points.
x <- rnorm(100, mean = 1.8, sd = 0.1) breaks(x) # equidistant bins breaks(x, width = 0.1) # custom width bins breaks(x, width = 0.1, probs = 4) # quantile-based binsx <- rnorm(100, mean = 1.8, sd = 0.1) breaks(x) # equidistant bins breaks(x, width = 0.1) # custom width bins breaks(x, width = 0.1, probs = 4) # quantile-based bins
Determines whether the current function call was initiated by a specified function. This is useful for conditional behavior depending on the caller.
calledBy(fun = "exams2pdf") called_by(fun = "exams2pdf")calledBy(fun = "exams2pdf") called_by(fun = "exams2pdf")
fun |
Character string specifying the name of the calling function to check for.
Defaults to |
A logical value: TRUE if the current call was triggered by fun, otherwise FALSE.
funB <- function() { calledBy("funA") } funA <- function() { funB() } funA() # Returns TRUE because funB was called by funAfunB <- function() { calledBy("funA") } funA <- function() { funB() } funA() # Returns TRUE because funB was called by funA
Prints text using cat only when a specified logical condition is TRUE.
catif(cond, ...) condition_cat(cond, ...)catif(cond, ...) condition_cat(cond, ...)
cond |
Logical value. If |
... |
Additional arguments passed to |
Invisibly returns the value of cond.
catif(TRUE, "PDF") # This text is printed catif(FALSE, "Moodle") # Nothing is printed condition_cat(TRUE, "Hello") # Alias works the same waycatif(TRUE, "PDF") # This text is printed catif(FALSE, "Moodle") # Nothing is printed condition_cat(TRUE, "Hello") # Alias works the same way
Computes confidence intervals for a population mean (mu) using either
supplied data or data generated from a normal distribution with specified
parameters. The function calculates key statistics such as the sample mean,
standard deviation, and confidence intervals at user-defined confidence levels.
Results are returned as a structured list containing observed and/or theoretical
values, interval endpoints, and related measures.
CImu_data( x = NULL, n = length(x), xbar = NULL, sd = NULL, conf.level = c(0.9, 0.95, 0.99), mu = NULL, sigma = NULL ) dcimu( x = NULL, n = length(x), xbar = NULL, sd = NULL, conf.level = c(0.9, 0.95, 0.99), mu = NULL, sigma = NULL )CImu_data( x = NULL, n = length(x), xbar = NULL, sd = NULL, conf.level = c(0.9, 0.95, 0.99), mu = NULL, sigma = NULL ) dcimu( x = NULL, n = length(x), xbar = NULL, sd = NULL, conf.level = c(0.9, 0.95, 0.99), mu = NULL, sigma = NULL )
x |
numeric vector of observed data. If |
n |
integer: sample size (if |
xbar |
numeric: sample mean (computed from |
sd |
numeric: sample standard deviation (computed from |
conf.level |
numeric vector of confidence levels (default: |
mu |
numeric: true population mean (used for simulation if |
sigma |
numeric: population standard deviation(s) (used for simulation if |
A list containing:
a |
upper-tail probability |
n |
sample size |
xbar |
sample mean |
mu |
theoretical mean (if provided) |
sd |
sample standard deviation |
sigma |
theoretical standard deviation (if provided) |
df |
degrees of freedom (if using a t distribution) |
q |
critical value(s) from the normal or t distribution |
ss |
standard deviation used in calculations ( |
e |
margin of error (half-width of the interval) |
l |
interval length |
v |
confidence interval endpoints |
# Using observed data x <- rnorm(100) CImu_data(x, conf.level = 0.95) # Simulating data internally CImu_data(n = 100, conf.level = 0.95, mu = 0, sigma = 1)# Using observed data x <- rnorm(100) CImu_data(x, conf.level = 0.95) # Simulating data internally CImu_data(n = 100, conf.level = 0.95, mu = 0, sigma = 1)
Data generation for the necessary sample size of a confidence interval, for the population mean value.
Either the estimation error or the length of the interval must be given ().
It is ensured that the computed s deviates from sigma.
CImulen_data( sigma, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE ) dcimulen( sigma, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE )CImulen_data( sigma, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE ) dcimulen( sigma, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE )
sigma |
numeric: vector of possible variance |
e |
numeric: vector of estimation errors |
l |
numeric: vector of lengths of the interval |
conf.level |
numeric: vector of confidence levels of the interval (default: |
nmin |
numeric: minimal value of necessary observation (default: |
size |
numeric: sample size for computing a sample standard deviation. Default |
u |
numeric: vector of quantiles used to sample the sample standard deviation (default: |
full |
logical: if |
A data frame or a list with:
: estimation error
sigma: population variance
conf.level: confidence level
: interval length
x:
q:
q2:
n: computed minimal sample size
N: the smallest integer, no less than n
s: sample standard deviation
# one solution CImulen_data (1:10, e=(1:10)/10) # all solutions mul <- CImulen_data (1:10, e=(1:10)/10, full=TRUE) str(mul)# one solution CImulen_data (1:10, e=(1:10)/10) # all solutions mul <- CImulen_data (1:10, e=(1:10)/10, full=TRUE) str(mul)
Data generation for the necessary sample size of a confidence interval, for the population proportion, using .
Either the estimation error or the length of the interval must be given ().
It is ensured that the computed p deviates from pi.
CIpilen_data( pi, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE ) dcipilen( pi, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE )CIpilen_data( pi, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE ) dcipilen( pi, e = NULL, l = NULL, conf.level = c(0.9, 0.95, 0.99), nmin = 30, size = NA, u = c(seq(0.1, 0.4, 0.001), seq(0.6, 0.9, 0.001)), full = FALSE )
pi |
numeric: vector of possible population proportions |
e |
numeric: vector of estimation errors |
l |
numeric: vector of lengths of the interval |
conf.level |
numeric: vector of confidence levels of the interval (default: |
nmin |
numeric: minimal value of necessary observation (default: |
size |
numeric: sample size for computing a sample standard deviation. Default |
u |
numeric: vector of quantiles used to sample the sample standard deviation (default: |
full |
logical: if |
A data frame or a list with:
estimation error
pi population proportion
conf.level confidence level
interval length
x
q
q2
n computed minimal sample size
N the smallest integer, no less than n
p sample proportion
# one solution CIpilen_data((1:9/10), (1:9)/10) # all solutions pil <- CIpilen_data((1:9/10), (1:9)/10, full=TRUE) str(pil)# one solution CIpilen_data((1:9/10), (1:9)/10) # all solutions pil <- CIpilen_data((1:9/10), (1:9)/10, full=TRUE) str(pil)
Deletes a file immediately after a specified delay or schedules it for deletion when the R session ends.
cleanFile(file, delay)cleanFile(file, delay)
file |
Character string. Path to the file to be deleted. |
delay |
Numeric. Number of seconds to wait before deleting the file.
If |
Invisibly returns the file path.
{ # Delete a temporary file after 2 seconds tmp <- tempfile(); if (file.create(tmp)) cleanFile(tmp, delay = 1) # Keep a temporary file until the R session ends tmp2 <- tempfile() if (file.create(tmp2)) cleanFile(tmp2, delay = 0) }{ # Delete a temporary file after 2 seconds tmp <- tempfile(); if (file.create(tmp)) cleanFile(tmp, delay = 1) # Keep a temporary file until the R session ends tmp2 <- tempfile() if (file.create(tmp2)) cleanFile(tmp2, delay = 0) }
permutation computes the number of permutations
variation computes the number of variations with and without replication
combination computes the number of combinations with and without replication
combinatorics computes all combinatorics results for k < n and returns it as list of:
permutation.npermutation.kpermutation.nkvariationvariation.repcombinationcombination.replfact computes the natural logarithm of the factorial of a given number n
lfactquot calculates the natural logarithm of the quotient of factorials
lbinom computes the natural logarithm of the binomial coefficient, "n choose k"
combinatorics(n, k) variation(n, k, repl = FALSE) combination(n, k, repl = FALSE) permutation(n, k = rep(1, n)) lfact(n) lfactquot(n, ...) lbinom(n, k) combo(n, k, repl = FALSE) combs(n, k) fact(n) factquot(n, ...) binom(n, k)combinatorics(n, k) variation(n, k, repl = FALSE) combination(n, k, repl = FALSE) permutation(n, k = rep(1, n)) lfact(n) lfactquot(n, ...) lbinom(n, k) combo(n, k, repl = FALSE) combs(n, k) fact(n) factquot(n, ...) binom(n, k)
n |
numeric: total number of elements |
k |
numeric: number of elements to choose |
repl |
logical: with repetition (default: |
... |
numeric: further arguments for |
A list.
permutation(8) permutation(8, c(1,3,2,2)) combination(8, 4) combination(8, 4, TRUE) variation(8, 4) variation(8, 4, TRUE) combinatorics(8, 4)permutation(8) permutation(8, c(1,3,2,2)) combination(8, 4) combination(8, 4, TRUE) variation(8, 4) variation(8, 4, TRUE) combinatorics(8, 4)
Rearranges the order of y to construct a dataset with a target correlation r between x and y,
as defined by stats::cor().
The marginal distributions of x and y are preserved, but the achieved correlation may deviate
from the target. The algorithm iteratively adjusts the ordering of y; increasing maxit
may improve accuracy if results are unsatisfactory.
cor_data( x, y, r, method = c("pearson", "kendall", "spearman"), ..., maxit = 1000 ) dcorr(x, y, r, method = c("pearson", "kendall", "spearman"), ..., maxit = 1000)cor_data( x, y, r, method = c("pearson", "kendall", "spearman"), ..., maxit = 1000 ) dcorr(x, y, r, method = c("pearson", "kendall", "spearman"), ..., maxit = 1000)
x |
numeric. Vector of |
y |
numeric. Vector of |
r |
numeric. Desired correlation. |
method |
character. Correlation coefficient to compute. Options are |
... |
Additional arguments passed to |
maxit |
integer. Maximum number of iterations (default: |
A two-column matrix with x and reordered y.
An attribute interim stores a matrix of intermediate values, which depends on method:
pearson:
Rows include , , , ,
squared deviations, and cross-products.
kendall:
Rows include , , (concordant pairs), and (discordant pairs).
spearman:
Rows include , , ranks of x and y, and squared rank differences.
In all cases, an additional column with row sums is appended.
x <- runif(6) y <- runif(6) xy <- cor_data(x, y, r = 0.6) cbind(x, y, xy)x <- runif(6) y <- runif(6) xy <- cor_data(x, y, r = 0.6) cbind(x, y, xy)
Generate sequences of integers representing sample sizes within a specified range.
Functions support two specialized sequences:
data_nsq / dnsq: sample sizes that are perfect squares.
data_n25 / dn25: sample sizes divisible only by 2 and 5.
data_n(max, min = 5) data_nsq(max, min = 5) data_n25(max, min = 5) dn(max, min = 5) dn25(max, min = 5) dnsq(max, min = 5)data_n(max, min = 5) data_nsq(max, min = 5) data_n25(max, min = 5) dn(max, min = 5) dn25(max, min = 5) dnsq(max, min = 5)
max |
Integer. Maximum sample size. |
min |
Integer. Minimum sample size (default: |
An integer vector of sample sizes satisfying the criteria.
data_n(10) data_nsq(1000) data_n25(1000)data_n(10) data_nsq(1000) data_n25(1000)
Generates a nrow × ncol matrix with probabilities / frequencies.
If data is given it will be normalized such that sum(data[is.finite(data)])==1.
If no rownames or colnames are given then event names from LETTERS are used.
The returned matrix will have the following attributes:
marginals a list of the row and column marginal distributions
byrow a matrix with conditional probabilities by row
bycol a matrix with conditional probabilities by column
expected a matrix with the expected probabilities under independence
prob a vector of all the probabilities computed (except the expected ones)
data_prob2( data = NULL, nrow = 2, ncol = 2, colnames = NULL, rownames = NULL, ... ) prob_mx(data = NULL, nrow = 2, ncol = 2, colnames = NULL, rownames = NULL, ...) dprob2(data = NULL, nrow = 2, ncol = 2, colnames = NULL, rownames = NULL, ...)data_prob2( data = NULL, nrow = 2, ncol = 2, colnames = NULL, rownames = NULL, ... ) prob_mx(data = NULL, nrow = 2, ncol = 2, colnames = NULL, rownames = NULL, ...) dprob2(data = NULL, nrow = 2, ncol = 2, colnames = NULL, rownames = NULL, ...)
data |
an optional data vector. Non-atomic classed R objects are coerced
by |
nrow |
numeric: desired number of rows (default: |
ncol |
numeric: desired number of columns (default: |
colnames |
character: names of column events |
rownames |
character: names of row events |
... |
further parameters given to |
A matrix and some attributes.
x <- data_prob2() str(x) data_prob2(colnames="E") data_prob2(nrow=3)x <- data_prob2() str(x) data_prob2(colnames="E") data_prob2(nrow=3)
Creates a discrete probability function based on x with a resolution unit.
If unit is not given then unit will be 10, 100, 1000, ... depending
on the length of the discrete probability function.
ddiscrete(x, unit = NULL, zero = FALSE)ddiscrete(x, unit = NULL, zero = FALSE)
x |
numeric: number of elements of vector of initial probabilities |
unit |
integer: reciprocal of the smallest non-zero probability (default: |
zero |
logical: zeros are allowed in the final probabilities (default: |
A discrete probability function.
ddiscrete(runif(6)) ddiscrete(6) ddiscrete(6, 20) ddiscrete(c(1,0,0,0), zero=TRUE)ddiscrete(runif(6)) ddiscrete(6) ddiscrete(6, 20) ddiscrete(c(1,0,0,0), zero=TRUE)
Creates a bivariate discrete probability function based on the marginal probability functions
row and col. If unit is not given then unit will be the product of the units
used in row and col, otherwise it will appear as the least common multiple unit product of
the units used in row and col.
If target is NA then the common distribution of two independent random variables
is returned, otherwise an iterative algorithm is run to approach a target association or
correlation measure, see also assoc_data() (called internally).
zero allows for zero entries in the common distribution.
FUN computes the association or correlation measures based on a
frequency table. tol gives the maximal deviation of the association or correlation measure
and the target value. maxit limits the number of steps.
Please note that a solution is not guaranteed, especially for extreme values for target, for example
for , or nearby values.
If attr(joint, "iterations")==maxit then you need
either to increase maxit, to decrease tol or to check if you have chosen an
appropriate target value (for a nominal measure in , for ordinal measure in ).
ddiscrete2( row, col, unit = NULL, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... ) biv_discrete_prob( row, col, unit = NULL, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... )ddiscrete2( row, col, unit = NULL, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... ) biv_discrete_prob( row, col, unit = NULL, zero = FALSE, FUN = nom.cc, target = NA, tol = 0.001, maxit = 500, ... )
row |
numeric: marginal |
col |
numeric: marginal |
unit |
integer: reciprocal of the smallest non-zero probability (default: |
zero |
logical: zeros are allowed in the final probabilities (default: |
FUN |
function: association or correlation function (default: |
target |
numeric: target association or correlation (default: |
tol |
numeric: tolerance for target association or correlation (default: |
maxit |
integer: maximal number of iterations (default: |
... |
further parameters for |
A bivariate discrete probability function.
row <- ddiscrete(runif(5)) col <- ddiscrete(runif(3)) joint <- ddiscrete2(row, col) joint joint <- ddiscrete2(row, col, target=0.5) joint nom.cc(joint*attr(joint, "unit"))row <- ddiscrete(runif(5)) col <- ddiscrete(runif(3)) joint <- ddiscrete2(row, col) joint joint <- ddiscrete2(row, col, target=0.5) joint nom.cc(joint*attr(joint, "unit"))
Probability mass function, distribution function, quantile function and random generation for the sum of two independent discrete uniform distributions.
ddunif2(x, min = 1, max = 6) pdunif2(q, min = 1, max = 6) qdunif2(p, min = 1, max = 6) rdunif2(n, min = 1, max = 6) sum_discrete_unif_cdf(x, min = 1, max = 6) sum_discrete_unif_pmf(q, min = 1, max = 6) sum_discrete_unif_quantile(p, min = 1, max = 6) sum_discrete_unif_rand(n, min = 1, max = 6)ddunif2(x, min = 1, max = 6) pdunif2(q, min = 1, max = 6) qdunif2(p, min = 1, max = 6) rdunif2(n, min = 1, max = 6) sum_discrete_unif_cdf(x, min = 1, max = 6) sum_discrete_unif_pmf(q, min = 1, max = 6) sum_discrete_unif_quantile(p, min = 1, max = 6) sum_discrete_unif_rand(n, min = 1, max = 6)
x, q
|
numeric: vector of quantiles |
min |
numeric: lower limit of the distribution (default: |
max |
numeric: upper limit of the distribution (default: |
p |
numeric: vector of probabilities |
n |
numeric: number of observations. If |
A numeric vector with the same length as x.
ddunif2(1:13) pdunif2(1:13) qdunif2((0:4)/4) rdunif2(10)ddunif2(1:13) pdunif2(1:13) qdunif2((0:4)/4) rdunif2(10)
Probability mass function, distribution function, quantile function and random generation
for the discrete uniform distribution. Documentation, examples and interface are taken from
extraDistr::DiscreteUniform.
ddunif(x, min, max, log = FALSE) pdunif(q, min, max, lower.tail = TRUE, log.p = FALSE) qdunif(p, min, max, lower.tail = TRUE, log.p = FALSE) rdunif(n, min, max)ddunif(x, min, max, log = FALSE) pdunif(q, min, max, lower.tail = TRUE, log.p = FALSE) qdunif(p, min, max, lower.tail = TRUE, log.p = FALSE) rdunif(n, min, max)
x, q
|
vector of quantiles. |
min, max
|
lower and upper limits of the distribution. Must be finite. |
log, log.p
|
logical; if TRUE, probabilities p are given as log(p). |
lower.tail |
logical; if TRUE (default), probabilities are |
p |
vector of probabilities. |
n |
number of observations. If |
If min == max, then discrete uniform distribution is a degenerate distribution.
x <- rdunif(1e5, 1, 10) xx <- -1:11 plot(prop.table(table(x)), type = "h") lines(xx, ddunif(xx, 1, 10), col = "red") hist(pdunif(x, 1, 10)) xx <- seq(-1, 11, by = 0.01) plot(ecdf(x)) lines(xx, pdunif(xx, 1, 10), col = "red")x <- rdunif(1e5, 1, 10) xx <- -1:11 plot(prop.table(table(x)), type = "h") lines(xx, ddunif(xx, 1, 10), col = "red") hist(pdunif(x, 1, 10)) xx <- seq(-1, 11, by = 0.01) plot(ecdf(x)) lines(xx, pdunif(xx, 1, 10), col = "red")
Holds an univariate distribution including its parameters. The name of the distribution is used to determine the right use of the function.
For example, in the case of function for quantiles: paste0("q", name). Usually the full name has to be used; some abbreviated names are possible:
binom binomial distribution, parameters: size, prob
hyper hypergeometric distribution, parameters: m, n, k
geom geometric distribution, parameters: prob
pois Poisson distribution, parameters: lambda
unif continuous uniform distribution, parameters: min, max
dunif discrete uniform distribution, parameters: min, max
dunif2 continuous uniform distribution, parameters: min, max
exp exponential distribution, parameter: rate
norm normal distribution, parameters: mean, sd
lnorm log-normal distribution, parameters: meanlog, sdlog
t Student t distribution, parameter: df
chisq chi-squared distribution, parameter: df
f F distribution, parameters: df1, df2
Note that a probability mass/density, quantile and a cumulative distribution function must exist.
The following functions exists for disributions:
distribution creates a distribution with name and parameters
quantile computes the quantiles of a distribution using paste0('q', name)
cdf computes the cumulative distribution function of a distribution using paste0('p', name)
pmdf computes the probability mass/density function of a distribution using paste0('d', name)
prob computes the probability for a interval between min and max (max included, min excluded)
prob1 computes the point probability f
is.distribution checks if object is distribution object. If name is given then it checks whether the distribution type is the same
toLatex generates a LaTeX representation of the distribution an its parameter
distribution(name, ...) ## Default S3 method: distribution(name, ..., discrete = NA) ## S3 method for class 'distribution' quantile(x, probs = seq(0, 1, 0.25), ...) cdf(x, q, ...) ## S3 method for class 'distribution' print(x, ...) ## S3 method for class 'distribution' summary(object, ...) pmdf(d, x, ...) ## S3 method for class 'distribution' toLatex(object, name = NULL, param = NULL, digits = 4, ...) is.distribution(object, name = NULL) prob(d, min = -Inf, max = +Inf, tol = 1e-06) prob1(d, x, tol = 1e-06) compute_cdf(x, q, ...) compute_pmdf(d, x, ...) compute_probability(d, min = -Inf, max = +Inf, tol = 1e-06) point_probability(d, x, tol = 1e-06) pprob(d, x, tol = 1e-06) is_distribution(object, name = NULL)distribution(name, ...) ## Default S3 method: distribution(name, ..., discrete = NA) ## S3 method for class 'distribution' quantile(x, probs = seq(0, 1, 0.25), ...) cdf(x, q, ...) ## S3 method for class 'distribution' print(x, ...) ## S3 method for class 'distribution' summary(object, ...) pmdf(d, x, ...) ## S3 method for class 'distribution' toLatex(object, name = NULL, param = NULL, digits = 4, ...) is.distribution(object, name = NULL) prob(d, min = -Inf, max = +Inf, tol = 1e-06) prob1(d, x, tol = 1e-06) compute_cdf(x, q, ...) compute_pmdf(d, x, ...) compute_probability(d, min = -Inf, max = +Inf, tol = 1e-06) point_probability(d, x, tol = 1e-06) pprob(d, x, tol = 1e-06) is_distribution(object, name = NULL)
name |
character: a replacement of the name of the distribution type |
... |
further named distribution parameters |
discrete |
logical: is the distribution discrete? (default: |
x |
vector of values |
probs |
numeric: vector of probabilities with values in |
q |
numeric: vector of quantiles |
object |
distribution object |
d |
distribution |
param |
character: names for the distribution parameters |
digits |
integer: number of digits used in |
min |
numeric: left border of interval |
max |
numeric: right border of interval |
tol |
numeric: tolerance for |
A distribution object.
d <- distribution("norm", mean=0, sd=1) quantile(d) quantile(d, c(0.025, 0.975)) cdf(d, 0) is.distribution(d) is.distribution(d, "t") toLatex(d)d <- distribution("norm", mean=0, sd=1) quantile(d) quantile(d, c(0.025, 0.975)) cdf(d, 0) is.distribution(d) is.distribution(d, "t") toLatex(d)
A data frame with the R function names, LaTeX names, discreteness and package origin of a distribution.
data(distributions)data(distributions)
A data frame with columns r, latex, discret and package
data(distributions) distributionsdata(distributions) distributions
is_terminal checks whether x's can be expressed as a terminal fraction, basically divisor_25(denominator(x))
divisor_25 checks whether all x's can be expressed as
prime_numbers returns all prime numbers up to a limit
primes prime factorization of x, returns a matrix with the power of each prime number
has_digits checks whether the x's have only digits after the decimal point, basically abs(x-round(x, digits))<tol
all_integer checks whether all x's are integer, basically all(has_digits(x,0))
divisor_25(x) denominator_25(x) is_terminal(x) round_25(x) prime_numbers(n, sieve = FALSE) primes(x, min = 2) has_digits( x, digits = 2, tol = 10^{ -digits - 6 } ) all_integer(x) only_digits( x, digits = 2, tol = 10^{ -digits - 6 } ) is_term(x) denom_25(x)divisor_25(x) denominator_25(x) is_terminal(x) round_25(x) prime_numbers(n, sieve = FALSE) primes(x, min = 2) has_digits( x, digits = 2, tol = 10^{ -digits - 6 } ) all_integer(x) only_digits( x, digits = 2, tol = 10^{ -digits - 6 } ) is_term(x) denom_25(x)
x |
numeric: values to test/check |
n |
integer: find all prime numbers up to n |
sieve |
logical: should in any case the Sieve of Eratosthenes be used to compute prime numbers (default: |
min |
integer: the minimum prime number used (default: |
digits |
numeric: number of digits to check (default: |
tol |
numeric: max. deviation from the rounded |
logical
is_terminal(2/3) # 0.6666... non-terminal is_terminal(1/5) # 0.2 terminal divisor_25(1:25) prime_numbers(100) # all prime numbers less equal 100 primes(1:20) # prime factorization of 1 to twentyis_terminal(2/3) # 0.6666... non-terminal is_terminal(1/5) # 0.2 terminal divisor_25(1:25) prime_numbers(100) # all prime numbers less equal 100 primes(1:20) # prime factorization of 1 to twenty
It performs a comparison by checking if either abs(x - y) < tol when outer == FALSE,
or if an a exists or a y[j] for each x[i] such that the condition abs(x[i] - y[j]) < tol is satisfied.
equal(x, y, tol = 1e-06, outer = FALSE) approx_equal(x, y, tol = 1e-06, outer = FALSE)equal(x, y, tol = 1e-06, outer = FALSE) approx_equal(x, y, tol = 1e-06, outer = FALSE)
x |
numeric |
y |
numeric |
tol |
numeric: tolerance (default: |
outer |
logical: compares directly or verifies whether |
logical
equal(9*1/9, 1)equal(9*1/9, 1)
equations defines a set of equations using the formula interface including a LaTeX representation of the formulae.
variables sets the variable values, the LaTeX representation and the solution interval. The first argument must be
the equations object. A named parameter starts the setting for a specific variable, e.g. ..., s=1, pos(5), "s^2",...
sets for the variable s first its numerical value, second the solution interval and finally the LaTeX representation.
equations(...) variables(...)equations(...) variables(...)
... |
For |
(for equations) An equations object.
(for variables) The modified equations object.
# The equations describe the formulae for an confidence interval of the mean e <- equations(o~x+c*s/sqrt(n), "v_o=\\bar{x}+c\\cdot\\frac{s^2}{n}", u~x-c*s/sqrt(n), "v_u=\\bar{x}-c\\cdot\\frac{s^2}{n}", e~c*s/sqrt(n), "e =c\\cdot\\frac{s^2}{\\sqrt{n}}", l~2*e, "l =2\\cdot e" ) print(e) e <- variables(e, x=0, "\\bar{x}", c=2.58, dbl(2), s=1, pos(5), "s^2", n=25, pos(5), l=pos(5), e=pos(5), u="v_u", o="v_o") print(e)# The equations describe the formulae for an confidence interval of the mean e <- equations(o~x+c*s/sqrt(n), "v_o=\\bar{x}+c\\cdot\\frac{s^2}{n}", u~x-c*s/sqrt(n), "v_u=\\bar{x}-c\\cdot\\frac{s^2}{n}", e~c*s/sqrt(n), "e =c\\cdot\\frac{s^2}{\\sqrt{n}}", l~2*e, "l =2\\cdot e" ) print(e) e <- variables(e, x=0, "\\bar{x}", c=2.58, dbl(2), s=1, pos(5), "s^2", n=25, pos(5), l=pos(5), e=pos(5), u="v_u", o="v_o") print(e)
exams2 FunctionsReturns a list with the functions' names and parameters called from .traceback().
The function name must start with "exams2".
exams2call(prefix = "exams2")exams2call(prefix = "exams2")
prefix |
character: start of the function name (default: |
A list with the function name and its evaluated parameters.
exams2call() # access current call stackexams2call() # access current call stack
Data structure for exercise data.
exercise(exer, ...) ## Default S3 method: exercise(exer = NULL, ...) exercise_data(exer, ...)exercise(exer, ...) ## Default S3 method: exercise(exer = NULL, ...) exercise_data(exer, ...)
exer |
an exercise object (default: |
... |
further parameters |
An exercise object.
exer <- exercise() # new exercise exer <- exercise(exer, x=3) # add x to the exerciseexer <- exercise() # new exercise exer <- exercise(exer, x=3) # add x to the exercise
Computes the real valued extremes (minima, maxima, and saddle points) for a univariate polynomial. The computation can be limited to a specific type of extremes.
extremes(p, type = c("all", "minimum", "maximum", "saddle"), tol = 1e-09)extremes(p, type = c("all", "minimum", "maximum", "saddle"), tol = 1e-09)
p |
a polynomial |
type |
character: either |
tol |
numeric: if the absolute value of the imaginary part of the zeroes of the derivative of |
A numeric vector.
p <- polynomial(c(0,0,0,1)) extremes(p) p <- integral(poly.calc(-1:1)) extremes(p)p <- polynomial(c(0,0,0,1)) extremes(p) p <- integral(poly.calc(-1:1)) extremes(p)
Converts a number to a string containing either a floating point or a fractional number.
Note that a repeating or recurring decimal, which is a number whose decimal representation becomes periodic,
can also be expressed as a rational number. For example, .
It is the workhorse used in num2str.
If denom is negative then always decimal point numbers are used (default).
If denom is zero then a mix of decimal point and fractional numbers are used (whatever is shorter).
If denom is one then fractional numbers are used except for integers.
If denom is larger than one, then the denominator is set to denom if possible.
fcvt(x, nsmall = 15, plus = FALSE, denom = -1)fcvt(x, nsmall = 15, plus = FALSE, denom = -1)
x |
numeric: numbers to convert |
nsmall |
integer: number of significant digits for the mantissa/significand (default: |
plus |
logical: for positive numbers a plus sign should be used (default: |
denom |
integer: denominator for a fractional number |
A character.
x1 <- c(NA, NaN, -Inf, Inf, 0, pi*10^(-20:20)) fcvt(x1) x2 <- c(-0.36, 3.6, -30.6, 0.36) fcvt(x2) x3 <- c((0:16)/8, 1/3) fcvt(x3) # as floating point number, equals denom=-1 fcvt(x3, denom=0) # as floating point or fractional number fcvt(x3, denom=1) # as fractional number except for integers fcvt(x3, denom=8) # as fractional number with denominator denom if possiblex1 <- c(NA, NaN, -Inf, Inf, 0, pi*10^(-20:20)) fcvt(x1) x2 <- c(-0.36, 3.6, -30.6, 0.36) fcvt(x2) x3 <- c((0:16)/8, 1/3) fcvt(x3) # as floating point number, equals denom=-1 fcvt(x3, denom=0) # as floating point or fractional number fcvt(x3, denom=1) # as fractional number except for integers fcvt(x3, denom=8) # as fractional number with denominator denom if possible
firstmatch seeks matches for the elements of its first argument among those of its second.
For further details please check base::charmatch(). charmatch returns a zero if multiple matches are found, whereas firstmatch
returns the first partial match if multiple matches are found.
firstmatch(x, table, nomatch = NA_integer_)firstmatch(x, table, nomatch = NA_integer_)
x |
character: the values to be matched; converted to a character vector if necessary |
table |
character: the values to be matched against; converted to a character vector if necessary |
nomatch |
integer: the value to be returned at non-matching positions (default: |
An integer.
firstmatch("d", c("chisq", "cauchy")) charmatch("c", c("chisq", "cauchy")) firstmatch("c", c("chisq", "cauchy")) firstmatch("ca", c("chisq", "cauchy"))firstmatch("d", c("chisq", "cauchy")) charmatch("c", c("chisq", "cauchy")) firstmatch("c", c("chisq", "cauchy")) firstmatch("ca", c("chisq", "cauchy"))
Finds rational approximations to the components of a real numeric object, using a standard continued fraction method.
Calls MASS::fractions() (Please refer to that for further details).
fractions(x, cycles = 10, max.denominator = 2000, ...) approx_rational(x, cycles = 10, max.denominator = 2000, ...)fractions(x, cycles = 10, max.denominator = 2000, ...) approx_rational(x, cycles = 10, max.denominator = 2000, ...)
x |
any object of the numeric mode (missing values are allowed) |
cycles |
the maximum number of steps to be used in the continued fraction approximation process |
max.denominator |
an early termination criterion. If any partial denominator exceeds |
... |
further arguments |
An object of the class fractions. A structure with a .Data component, the same as the numeric x input,
but with the rational approximations held as the character vector attribute fracs. Arithmetic operations on fractions objects are possible.
X <- matrix(runif(25), 5, 5) fractions(X) #;) fractions(solve(X, X/5)) fractions(solve(X, X/5)) + 1X <- matrix(runif(25), 5, 5) fractions(X) #;) fractions(solve(X, X/5)) fractions(solve(X, X/5)) + 1
Runs all combinations of elements in ... as parameters of FUN (grid apply).
I(.) can be used to avoid that an element is interpreted as a grid value.
If an error occurs, then the result of FUN will not be stored. You may notice missing indices
in the returning list.
gapply(FUN, ..., .simplify = TRUE) apply_grid(FUN, ..., .simplify = TRUE)gapply(FUN, ..., .simplify = TRUE) apply_grid(FUN, ..., .simplify = TRUE)
FUN |
function or character: a string naming the function to be called |
... |
list: of arguments of the function to be called. The |
.simplify |
logical: should the result be simplified to a data frame (if possible)? (default: |
A list or a data frame with the function results.
# 8 function calls: sum(1,3,5), sum(1,3,6), ..., sum(2,4,6) gapply("sum", 1:2, 3:4, 5:6) # 4 function calls: sum(1,3,5:6), sum(1,4,5:66), ..., sum(2,4,5:6) gapply("sum", 1:2, 3:4, I(5:6))# 8 function calls: sum(1,3,5), sum(1,3,6), ..., sum(2,4,6) gapply("sum", 1:2, 3:4, 5:6) # 4 function calls: sum(1,3,5:6), sum(1,4,5:66), ..., sum(2,4,5:6) gapply("sum", 1:2, 3:4, I(5:6))
Returns the names of an object as a character vector. Optionally enforces that
names are non-NULL and non-empty when strict = TRUE. The function does not
modify the original object.
getNames(x, strict = FALSE)getNames(x, strict = FALSE)
x |
An R object (vector, list, etc.). Must not be |
strict |
Logical; if TRUE, errors if |
A character vector of names, same length as x.
If x is an empty list or zero-length object, returns character(0).
If names(x) is NULL and strict = FALSE, returns a vector of empty strings of the same length as x.
x <- c(a = 1, b = 2) getNames(x) y <- 1:3 getNames(y) z <- c(1, 2); names(z) <- c("a", "") # getNames(z, strict = TRUE) # would throw an error: "`x` has empty name(s)" # Empty list returns character(0) getNames(list(), strict = TRUE) # NULL input triggers an error # getNames(NULL, strict = TRUE)x <- c(a = 1, b = 2) getNames(x) y <- 1:3 getNames(y) z <- c(1, 2); names(z) <- c("a", "") # getNames(z, strict = TRUE) # would throw an error: "`x` has empty name(s)" # Empty list returns character(0) getNames(list(), strict = TRUE) # NULL input triggers an error # getNames(NULL, strict = TRUE)
Computes a grade based on the points of the grade scheme by the Humboldt University of Berlin. (See §96c and §102 in the Achte Änderung der Fächerübergreifenden Satzung zur Regelung von Zulassung, Studium und Prüfung der Humboldt-Universität zu Berlin (ZSP-HU))
grade(points, maxpts = max(points), fixed = TRUE) hu_grade(points, maxpts = max(points), fixed = TRUE)grade(points, maxpts = max(points), fixed = TRUE) hu_grade(points, maxpts = max(points), fixed = TRUE)
points |
numeric: points achieved in exam |
maxpts |
numeric: maximal number of achievable points in an exam (default: |
fixed |
logical: a fixed or relative grade scheme (default: |
Grades as a function of points.
x <- round(runif(100, 0, 22.4)) grade(x, 22)x <- round(runif(100, 0, 22.4)) grade(x, 22)
Computes mean, mode or quantile/median of grouped data.
grouped_data(x, n, compute = c("mean", "fine", "coarse"), tol = 1e-06) grouped_stats(x, n, compute = c("mean", "fine", "coarse"), tol = 1e-06) dgrouped(x, n, compute = c("mean", "fine", "coarse"), tol = 1e-06)grouped_data(x, n, compute = c("mean", "fine", "coarse"), tol = 1e-06) grouped_stats(x, n, compute = c("mean", "fine", "coarse"), tol = 1e-06) dgrouped(x, n, compute = c("mean", "fine", "coarse"), tol = 1e-06)
x |
numeric: borders |
n |
numeric: absolute frequencies for each group |
compute |
numeric/character: coefficient to compute |
tol |
numeric: tolerance for numerical comparison |
A list with the class, result and a table.
x <- 1:4 n <- ddiscrete(runif(3)) grouped_data(x, n)x <- 1:4 n <- ddiscrete(runif(3)) grouped_data(x, n)
hyperloop ObjectSimplifies a hyperloop object if possible.
gsimplify(ga, exclude = NULL, subset = NULL) simplify_hyperloop(ga, exclude = NULL, subset = NULL) simple_hloop(ga, exclude = NULL, subset = NULL)gsimplify(ga, exclude = NULL, subset = NULL) simplify_hyperloop(ga, exclude = NULL, subset = NULL) simple_hloop(ga, exclude = NULL, subset = NULL)
ga |
list: of a |
exclude |
character or integer: elements to exclude in each list element of |
subset |
indices specifying elements of |
A data frame if possible, otherwise a list.
# calls: t.test(x, -1), t.test(x, 0), t.test(x, 1) ga <- gapply(t.test, x=I(rnorm(100)), mu=-1:1) # no simplication since `data.name` and `conf.int` have lengths larger than one gsimplify(ga) #' simplification is now possible gsimplify(ga, exclude=c("conf.int", "data.name"))# calls: t.test(x, -1), t.test(x, 0), t.test(x, 1) ga <- gapply(t.test, x=I(rnorm(100)), mu=-1:1) # no simplication since `data.name` and `conf.int` have lengths larger than one gsimplify(ga) #' simplification is now possible gsimplify(ga, exclude=c("conf.int", "data.name"))
Randomly selects size breakpoints from breaks. If outer is TRUE, then
the first and last element of breaks is always included into the returned break points.
If size is a vector, the number of breakpoints is first sampled from size.
histbreaks(breaks, size, outer = TRUE, ...) rand_breaks(breaks, size, outer = TRUE, ...) dhistbreaks(breaks, size, outer = TRUE, ...)histbreaks(breaks, size, outer = TRUE, ...) rand_breaks(breaks, size, outer = TRUE, ...) dhistbreaks(breaks, size, outer = TRUE, ...)
breaks |
numeric: a vector of possible break points |
size |
integer: number of break points |
outer |
logical: should be the first and last element of the included breaks (default: |
... |
further parameters given if sampling of |
A vector of breakpoints.
# Always includes 100 and 200 histbreaks(seq(100, 200, by=10), 4) # Always includes 100 and 200 and chooses randomly between 3 to 5 break points histbreaks(seq(100, 200, by=10), 3:5) # May not include 100 and 200 histbreaks(seq(100, 200, by=10), 4, outer=FALSE)# Always includes 100 and 200 histbreaks(seq(100, 200, by=10), 4) # Always includes 100 and 200 and chooses randomly between 3 to 5 break points histbreaks(seq(100, 200, by=10), 3:5) # May not include 100 and 200 histbreaks(seq(100, 200, by=10), 4, outer=FALSE)
Returns data for a histogram. Calls internally hist(..., plot=FALSE).
mean returns the mean of the data.
quantile and median return the quantile(s) or median with an attribute pos, the class number of the quantile(s), or the median.
histdata(x, breaks = "Sturges", probs = seq(0, 1, 0.25), ...) ## S3 method for class 'histogram' quantile(x, probs = seq(0, 1, 0.25), ...) ## S3 method for class 'histogram' median(x, ...) ## S3 method for class 'histogram' mean(x, ...) dhist(x, breaks = "Sturges", probs = seq(0, 1, 0.25), ...)histdata(x, breaks = "Sturges", probs = seq(0, 1, 0.25), ...) ## S3 method for class 'histogram' quantile(x, probs = seq(0, 1, 0.25), ...) ## S3 method for class 'histogram' median(x, ...) ## S3 method for class 'histogram' mean(x, ...) dhist(x, breaks = "Sturges", probs = seq(0, 1, 0.25), ...)
x |
numeric data or histogram data |
breaks |
one of:
In the last three cases the number is a suggestion only; as the
breakpoints will be set to |
probs |
numeric: probabilities to use if |
... |
further parameters used in graphics::hist |
Like in graphics::hist, but with this additional list of elements:
lower lower class borders,
upper upper class borders,
width class widths,
relfreq the relative class frequency,
cumfbrk the cumulated relative frequency of the breaks,
maxdens the indices of the maximal density values,
maxcount the indices of the maximal count values
x the original finite data, and
class the class number for each value in x.
#1 x <- seq(0, 1, by=0.25) print(hist(x, plot=FALSE)) histdata(x) #2 x <- seq(0, 1, by=0.25) print(hist(x, x, plot=FALSE)) histdata(x, x) #3 print(hist(x, x, right=FALSE, plot=FALSE)) histdata(x, x, right=FALSE)#1 x <- seq(0, 1, by=0.25) print(hist(x, plot=FALSE)) histdata(x) #2 x <- seq(0, 1, by=0.25) print(hist(x, x, plot=FALSE)) histdata(x, x) #3 print(hist(x, x, right=FALSE, plot=FALSE)) histdata(x, x, right=FALSE)
Generates a set of class breaks and absolute frequencies for the range from from to to.
Class widths are randomly sampled from the vector widths. The total number of classes
(nb) must be an integer multiple of min(widths); otherwise, the function stops with an error.
If the initial frequencies (n) are too small, they can be scaled by an integer factor.
The routine also checks whether the resulting class densities are terminating decimals.
histwidth(from, to, widths, dmax = 2000, maxit = 1000) width_breaks(from, to, widths, dmax = 2000, maxit = 1000) dhistwidth(from, to, widths, dmax = 2000, maxit = 1000)histwidth(from, to, widths, dmax = 2000, maxit = 1000) width_breaks(from, to, widths, dmax = 2000, maxit = 1000) dhistwidth(from, to, widths, dmax = 2000, maxit = 1000)
from |
numeric: start value of the range. |
to |
numeric: end value of the range. |
widths |
numeric: vector of possible class widths to sample from. |
dmax |
numeric: maximum denominator allowed when checking fractional densities, see |
maxit |
integer: maximum number of iterations when attempting to find a suitable break pattern. |
A list containing:
breaks |
Numeric vector of class boundaries. |
n |
Integer vector of absolute frequencies for each class. |
decimal |
Logical, |
density |
Numeric vector of class densities. |
l <- histwidth(1.6, 2.1, widths = c(0.05, 0.1, 0.15, 0.2)) x <- histx(l$breaks, l$n) histdata(x, l$breaks) # Fallback: use constant min(widths) if no valid break pattern # is found within max iterations l <- histwidth(1.6, 2.1, widths=0.05, dmax=10) str(l)l <- histwidth(1.6, 2.1, widths = c(0.05, 0.1, 0.15, 0.2)) x <- histx(l$breaks, l$n) histdata(x, l$breaks) # Fallback: use constant min(widths) if no valid break pattern # is found within max iterations l <- histwidth(1.6, 2.1, widths=0.05, dmax=10) str(l)
Given the breaks and the number of observations, a data set is generated with stats::runif(), using
the class mids: . The default alpha=0.99 ensures that
generated observations do not lie on the class borders.
histx(breaks, n, alpha = 0.99) gen_mid(breaks, n, alpha = 0.99) dhistx(breaks, n, alpha = 0.99)histx(breaks, n, alpha = 0.99) gen_mid(breaks, n, alpha = 0.99) dhistx(breaks, n, alpha = 0.99)
breaks |
numeric: class borders |
n |
numeric: number of observations in each class |
alpha |
numeric: how far the generated observations can be away from the class mids (default: |
The generated data set.
breaks <- sort(sample(seq(0.1, 0.9, by=0.1), 4)) bins <- length(breaks)-1 n <- rmultinom(1, size=100, prob=ddiscrete(runif(bins))) histx(breaks, n)breaks <- sort(sample(seq(0.1, 0.9, by=0.1), 4)) bins <- length(breaks)-1 n <- rmultinom(1, size=100, prob=ddiscrete(runif(bins))) histx(breaks, n)
html_mmatrix Modificationhm_cell or hm_index modify a data cell format (fmt="%s"), value (unnamed parameter) or style (text_align="left")
hm_col or hm_row modify a row or column format (fmt="%s"), value (unnamed parameter) or style (text_align="left")
hm_title modifies the title attribute of an html_matrix based on specific arguments
hm_table modifies the properties of the entire HTML table within an html_matrix
hm_tr modifies the properties of one or more table rows (tr elements) in an html_matrix. Row indices for modification (ind) can be specified along with additional parameters to customize the row format, values, or style
hm_cell(x, row = NULL, col = NULL, ..., byrow = FALSE) hm_index(x, ind, ...) hm_title(x, ...) hm_table(x, ...) hm_row(x, ind, ...) hm_col(x, ind, ...) hm_tr(x, ind, ...) modify_cell(x, row = NULL, col = NULL, ..., byrow = FALSE) mod_cell(x, row = NULL, col = NULL, ..., byrow = FALSE) modify_col(x, ind, ...) mod_col(x, ind, ...) modify_index(x, ind, ...) mod_ind(x, ind, ...) modify_row(x, ind, ...) mod_row(x, ind, ...) modify_table(x, ...) mod_t(x, ...) modify_title(x, ...) mod_title(x, ...) modify_tr(x, ind, ...) mod_tr(x, ind, ...)hm_cell(x, row = NULL, col = NULL, ..., byrow = FALSE) hm_index(x, ind, ...) hm_title(x, ...) hm_table(x, ...) hm_row(x, ind, ...) hm_col(x, ind, ...) hm_tr(x, ind, ...) modify_cell(x, row = NULL, col = NULL, ..., byrow = FALSE) mod_cell(x, row = NULL, col = NULL, ..., byrow = FALSE) modify_col(x, ind, ...) mod_col(x, ind, ...) modify_index(x, ind, ...) mod_ind(x, ind, ...) modify_row(x, ind, ...) mod_row(x, ind, ...) modify_table(x, ...) mod_t(x, ...) modify_title(x, ...) mod_title(x, ...) modify_tr(x, ind, ...) mod_tr(x, ind, ...)
x |
an |
row |
integer: row(s) to access |
col |
integer: column(s) to access |
... |
further elements |
byrow |
logical: order indices by row or column (default: |
ind |
integer vector or matrix: has access to various (row and columns) elements (first column: row, second column: column) |
A modified html_matrix object.
l <- html_matrix(matrix(1:6, ncol=2)) # replace l[1,1] by NA hm_cell(l, 1, 1, NA) # replace l[1,1] by NA and set the text_align to center hm_cell(l, 1, 1, NA, text_align="center") # replace l[1,3] and l[2,1] by NA rcind <- cbind(c(1,3), c(2, 1)) hm_index(l, rcind, NA) # set a new title hm_title(l, "new title") # set a new row or column title hm_row(l, 2, "row 2") hm_col(l, 1, "col 1") # set fmt by column or row print(hm_cell(l, fmt=c("%.0f", "%.1f", "%.2f"), byrow=FALSE), which="fmt") print(hm_cell(l, fmt=c("%.0f", "%.1f"), byrow=TRUE), which="fmt")l <- html_matrix(matrix(1:6, ncol=2)) # replace l[1,1] by NA hm_cell(l, 1, 1, NA) # replace l[1,1] by NA and set the text_align to center hm_cell(l, 1, 1, NA, text_align="center") # replace l[1,3] and l[2,1] by NA rcind <- cbind(c(1,3), c(2, 1)) hm_index(l, rcind, NA) # set a new title hm_title(l, "new title") # set a new row or column title hm_row(l, 2, "row 2") hm_col(l, 1, "col 1") # set fmt by column or row print(hm_cell(l, fmt=c("%.0f", "%.1f", "%.2f"), byrow=FALSE), which="fmt") print(hm_cell(l, fmt=c("%.0f", "%.1f"), byrow=TRUE), which="fmt")
exams.forge
Creates an HTML page with all the contents of the XML tags whose names match pattern.
The default is to show the contents of all XML tags. The HTML page is stored in the HTML file name.
The default name=NULL creates a temporary file. If the name does not end in .html, then a .html is appended.
If browseURL=TRUE (default) then the HTML page will be displayed in the browser.
If necessary the contents of XML tags are concatenated with "\n".
For single XML tags this can be changed, e.g. merge=list("questionlist"="<br>" leads to the XML tag
<questionlist>...</questionlist>) "<br>" being used ,instead of the "\n".
html_e2m( exam, name = NULL, pattern = ".", mathjax = TRUE, browseURL = TRUE, overwrite = FALSE, header = 2, merge = list(questionlist = "<br>"), png = TRUE ) toHTML_XML( exam, name = NULL, pattern = ".", mathjax = TRUE, browseURL = TRUE, overwrite = FALSE, header = 2, merge = list(questionlist = "<br>"), png = TRUE )html_e2m( exam, name = NULL, pattern = ".", mathjax = TRUE, browseURL = TRUE, overwrite = FALSE, header = 2, merge = list(questionlist = "<br>"), png = TRUE ) toHTML_XML( exam, name = NULL, pattern = ".", mathjax = TRUE, browseURL = TRUE, overwrite = FALSE, header = 2, merge = list(questionlist = "<br>"), png = TRUE )
exam |
list: returns a list from |
name |
character: name of the HTML file (default: |
pattern |
character: string containing a regular expression to match the list elements (default: |
mathjax |
logical: should MathJax be loaded? (default: |
browseURL |
logical: should the generated HTML be shown? (default: |
overwrite |
logical: should the HTML file be overwritten (if it exists)? (default: |
header |
integer: at which level of the list a |
merge |
list: should elements with |
png |
logical: if a entry ends with |
Invisibly, the names of listed elements in the HTML file.
The aim is similar to exams:::exams:::browse_exercise, however, html_e2m takes the information form
the XML file generated by the exams.forge package.
if (interactive()) { resexams <- readRDS(system.file("xml", "klausur-test.rds", package="exams.moodle")) html_e2m(resexams) # opens HTML file into browser }if (interactive()) { resexams <- readRDS(system.file("xml", "klausur-test.rds", package="exams.moodle")) html_e2m(resexams) # opens HTML file into browser }
Creates from a vector, a matrix, an array, or a table, an HTML representation of it. The HTML representation has one column and one row more than the data. The additional row and column are used in order to have a title (top left), the column names (top), and the row names (left).
You can set the style attributes (<td style="...">) via hm_cell, hm_title, hm_col, and hm_row.
For example: hm_cell(hm, 1, 1, text_align="right") will lead to (<td style="text-align:right;">) for the cell (1,1), and any
unnamed element will change the cell value.
Note: since - is an operator in R, we use _ instead. Of course, someone could use "text-align"="right", but I am lazy.
html_matrix(x, ...) ## Default S3 method: html_matrix( x, ..., byrow = FALSE, numeric = list(text_align = "right"), integer = list(text_align = "right"), char = list(text_align = "left"), logical = list(text_align = "right"), border = "#999999" ) html_mx(x, ...)html_matrix(x, ...) ## Default S3 method: html_matrix( x, ..., byrow = FALSE, numeric = list(text_align = "right"), integer = list(text_align = "right"), char = list(text_align = "left"), logical = list(text_align = "right"), border = "#999999" ) html_mx(x, ...)
x |
vector, matrix, array, table or html_matrix: input. |
... |
further parameters |
byrow |
logical: creates a row or column matrix if |
numeric |
list: of HTML style properties for a cell if |
integer |
list: of HTML style properties for a cell if |
char |
list: of HTML style properties for a cell if |
logical |
list: of HTML style properties for a cell if |
border |
character: vector of background color for a border cell (default: |
Returns an html_matrix.
m <- matrix(1:6, ncol=2) m l <- html_matrix(m) lm <- matrix(1:6, ncol=2) m l <- html_matrix(m) l
html_matrix Object CreationMy personal pipe creating an html_matrix object. Note that the length of fmt
must be either nrow(m) or ncol(m) depending on byrow.
html_matrix(m)
tooltip(sprintf(tooltip, nrow(m), ncol(m)))
hm_cell(fmt=fmt, byrow=byrow)
html_matrix_sk( m, title, fmt, byrow = TRUE, tooltip = "Die Tabelle hat %.0f Zeilen und %.0f Spalten", ... ) lmatrix( m, title, fmt, byrow = TRUE, tooltip = "Die Tabelle hat %.0f Zeilen und %.0f Spalten", ... )html_matrix_sk( m, title, fmt, byrow = TRUE, tooltip = "Die Tabelle hat %.0f Zeilen und %.0f Spalten", ... ) lmatrix( m, title, fmt, byrow = TRUE, tooltip = "Die Tabelle hat %.0f Zeilen und %.0f Spalten", ... )
m |
vector, matrix, array, table or html_matrix: input |
title |
character: text for the upper left entry |
fmt |
character: text format for rows (or columns) |
byrow |
logical: |
tooltip |
character: text for tooltip with column and row numbers (default: |
... |
further parameters given to |
An html_matrix object.
m <- matrix(1:6, ncol=2) html_matrix_sk(m, title="", fmt=c("%.0f", "%.1f"))m <- matrix(1:6, ncol=2) html_matrix_sk(m, title="", fmt=c("%.0f", "%.1f"))
Generates a data frame with potential values for m, n and k. If hyper2 is FALSE then the
parametrization of stats::dhyper() is used, otherwise n+m, m and k is used and transformed to m, n and k.
In accordance with specific conditions it holds that:
if length(mean)==1 and it's an integer, it signifies the desired number of digits for the mean
if mean is set to NA (the default), all means are permissible
when length(mean) > 1, the product must be one of the valid means
the same rules apply to sd
The parameters norm, pois and binom can take on the values NA, TRUE, FALSE,
or be defined as a function of the format: function(m, n, k).
These values determine which (m, n, k) combinations are eligible:
for NA, all combinations of (m, n, k) are acceptable
if specified as a function, only those combinations for which the function evaluates to TRUE are considered valid
if set to TRUE, combinations are accepted only if they satisfy either the condition
(for norm, indicating a normal distribution approximation), the conditions , and
(for pois, implying a Poisson distribution approximation) and the condition (for binom,
implying a binomial distribution approximation)
if set to FALSE, the approximations should not hold for any combination.
Please be aware that there is no guarantee that the resulting data frame will include a valid solution.
hyper_param( m, n, k, mean = NA, sd = NA, norm = NA, pois = NA, binom = NA, tol = 1e-06, hyper2 = FALSE )hyper_param( m, n, k, mean = NA, sd = NA, norm = NA, pois = NA, binom = NA, tol = 1e-06, hyper2 = FALSE )
m |
numeric: the number of white balls in the urn |
n |
numeric: the number of black balls in the urn |
k |
numeric: the number of balls drawn from the urn, hence must be in |
mean |
integer or numeric: number of digits the mean should have |
sd |
integer or numeric: number of digits the standard deviation should have |
norm |
logical or function: normal approximation possible |
pois |
logical or function: poisson approximation possible |
binom |
logical or function: binomial approximation possible |
tol |
numeric: the tolerance for numerical comparison (default: '1e-6) |
hyper2 |
logical: should the standard R parametrization |
A data frame with possible the choices of n , p, mean and sd.
hyper_param(7:14, 1:13, 3:10, norm=FALSE, pois=FALSE, binom=FALSE, hyper2=TRUE)hyper_param(7:14, 1:13, 3:10, norm=FALSE, pois=FALSE, binom=FALSE, hyper2=TRUE)
Runs a function several times with all parameter combinations, and checks:
if an argument is not a list, then it will be converted to an one element list
if an error occurs then the result of FUN will not be stored
hyperloop(FUN, ..., .simplify = FALSE) hloop(FUN, ..., .simplify = FALSE)hyperloop(FUN, ..., .simplify = FALSE) hloop(FUN, ..., .simplify = FALSE)
FUN |
function with named parameter(s) |
... |
named parameters which contain lists with possible parameter values |
.simplify |
logical: should the result be simplified to a data frame (if possible)? (default: |
A hyperloop object as a list.
x <- rnorm(100) trm <- hyperloop(mean, x=list(x), trim=as.list(seq(0, 0.5, by=0.05))) # automatic conversion of x to list(x) trm <- hyperloop(mean, x=x, trim=as.list(seq(0, 0.5, by=0.05))) unlist(trm)x <- rnorm(100) trm <- hyperloop(mean, x=list(x), trim=as.list(seq(0, 0.5, by=0.05))) # automatic conversion of x to list(x) trm <- hyperloop(mean, x=x, trim=as.list(seq(0, 0.5, by=0.05))) unlist(trm)
Creates a data frame for a test hypothesis with various columns:
h0.left left value of the null hypothesis, usually \mu or \pi
h0.operator operator of the null hypothesis, one of the following: eq, ne, lt, le, gt, or ge
h0.right right value of the null hypothesis, usually \mu_0, \pi_0, or a hypothetical value
h1.left left value of the alternative hypothesis, usually \mu or \pi
h1.operator operator of the alternative hypothesis, one of the following: eq, ne, lt, le, gt, or ge
h1.right right value of the alternative hypothesis, usually \mu_0, \pi_0, or a hypothetical value
H0 latex representation of the null hypothesis
H1 latex representation of the alternative hypothesis
match.left do the left value in the null and the alternative hypothesis match?
match.right do the right value in the null and the alternative hypothesis match?
match.operator do the operators in the null and the alternative hypothesis cover all real numbers?
match.right do the right value in the null and alternative hypothesis match?
match.type either wrong, left.sided, right.sided, two.sided, greater, or less.
If null is not given then it is determined from alternative. Otherwise hypotheses pairs are generated by
all combinations from alternative and null.
Valid values for alternativeand null are two.sided, greater, less, eq, ne, lt, le, gt, or ge.
hypothesis_latex( left, alternative = NULL, null = NULL, right = paste0(left, "_0") ) lhypo(left, alternative = NULL, null = NULL, right = paste0(left, "_0"))hypothesis_latex( left, alternative = NULL, null = NULL, right = paste0(left, "_0") ) lhypo(left, alternative = NULL, null = NULL, right = paste0(left, "_0"))
left |
character: symbol, for example |
alternative |
character: alternative hypotheses |
null |
character: null hypotheses (default: |
right |
character: a symbol (default: |
A data frame with hypothesis pairs.
# Create one hypotheses pair hypothesis_latex("\\mu") hypothesis_latex("\\pi") hypothesis_latex("\\mu", alternative="two.sided") hypothesis_latex("\\mu", alternative="two.sided", null="lt") hypothesis_latex("\\mu", alternative="ne", null="eq") hypothesis_latex("\\mu", right=c(0,1)) hypothesis_latex("\\mu", alternative=c("eq", "ne", "lt", "le", "gt", "ge")) hypothesis_latex("\\mu", alternative=c("eq", "ne", "lt", "le", "gt", "ge"), null=c("eq", "ne", "lt", "le", "gt", "ge"))# Create one hypotheses pair hypothesis_latex("\\mu") hypothesis_latex("\\pi") hypothesis_latex("\\mu", alternative="two.sided") hypothesis_latex("\\mu", alternative="two.sided", null="lt") hypothesis_latex("\\mu", alternative="ne", null="eq") hypothesis_latex("\\mu", right=c(0,1)) hypothesis_latex("\\mu", alternative=c("eq", "ne", "lt", "le", "gt", "ge")) hypothesis_latex("\\mu", alternative=c("eq", "ne", "lt", "le", "gt", "ge"), null=c("eq", "ne", "lt", "le", "gt", "ge"))
Fills a relative contingency table with n missing values, such that the table entries can be
recomputed. In case that no solution can be found, an error is generated.
incomplete_table(tab, n, maxit = 1000) cont_table_fill(tab, n, maxit = 1000)incomplete_table(tab, n, maxit = 1000) cont_table_fill(tab, n, maxit = 1000)
tab |
table: a contingency table |
n |
integer: number of missing values |
maxit |
integer: number of maximal iterations (default: |
A contingency table including marginal values and total sum with missing values.
The attribute fillin gives the necessary information about the order in which the entries can be calculated,
while the attribute full presents the contingency table, including marginal values and total sum.
tab <- rbind(c(0.02, 0.04, 0.34), c(0.02, 0.28, 0.3)) incomplete_table(tab, 7)tab <- rbind(c(0.02, 0.04, 0.34), c(0.02, 0.28, 0.3)) incomplete_table(tab, 7)
Knits txt within an R code chunk.
inline(txt) txt_knit(txt)inline(txt) txt_knit(txt)
txt |
character |
Output.
result <- inline("2 + 2")result <- inline("2 + 2")
Checks if x is in an opened or closed interval between min and max.
The default is set as such, that the chosen interval is an interval of .
For example, in the case of x being a probability.
is.prob(x, open = TRUE, min = 0, max = 1) is_prob_interval(x, open = TRUE, min = 0, max = 1) is_prob(x, open = TRUE, min = 0, max = 1) in_range(x, open = TRUE, min = 0, max = 1)is.prob(x, open = TRUE, min = 0, max = 1) is_prob_interval(x, open = TRUE, min = 0, max = 1) is_prob(x, open = TRUE, min = 0, max = 1) in_range(x, open = TRUE, min = 0, max = 1)
x |
numeric: values to check |
open |
logical: checks if the left and right borders are open or closed (default: |
min |
numeric: minimal value (default: |
max |
numeric: maximal value (default: |
A logical vector with the same length as x.
is.prob(runif(1))is.prob(runif(1))
Selects a text argument and returns the knitted result.
knitif(n, ..., envir = knit_global()) knit_select(n, ..., envir = knit_global())knitif(n, ..., envir = knit_global()) knit_select(n, ..., envir = knit_global())
n |
character: text argument to use |
... |
character: arguments to choose from |
envir |
environment: in which code chunks are to be evaluated (default: |
A character.
knitif(runif(1)<0.5, 'TRUE'="`r pi`", 'FALSE'="$\\pi=`r pi`$")knitif(runif(1)<0.5, 'TRUE'="`r pi`", 'FALSE'="$\\pi=`r pi`$")
If exams is called by exams2pdf,
latexdef adds a TeX macro by \def\name{body} and
answercol adds a \def\answercol{n} to modify the number of output columns for multiple-choice answers
to the LaTeX file.
latexdef(name, body) answercol(n) add_answercol_def(n)latexdef(name, body) answercol(n) add_answercol_def(n)
name |
character: macro name |
body, n
|
character: macro body |
Nothing
answercol(2)answercol(2)
Computes the least common multiple for a numeric vector x.
lcmval(x) lcm_vector(x)lcmval(x) lcm_vector(x)
x |
integer: numbers to find the least common multiple |
The least common multiple.
lcmval(c(144, 160)) # = 1440 lcmval(c(144, 160, 175)) # = 50.400lcmval(c(144, 160)) # = 1440 lcmval(c(144, 160, 175)) # = 50.400
Creates data suitable for a simple linear regression. In the first step, data is computed using pearson_data(),
satisfying the conditions and (similar conditions apply to .
The data are then rescaled with and
.
Finally, a simple linear regression is performed on the transformed data.
lm1_data( r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL, center = numeric(0), scale = numeric(0), ... ) slr_data( r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL, center = numeric(0), scale = numeric(0), ... )lm1_data( r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL, center = numeric(0), scale = numeric(0), ... ) slr_data( r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL, center = numeric(0), scale = numeric(0), ... )
r |
numeric: desired correlation |
n |
integer: number to decompose as sum of squares, see |
nmax |
integer: maximal number of squares in the sum, see |
maxt |
numeric: maximal number of seconds the routine should run, see |
xsos |
sos matrix: precomputed matrix, see |
ysos |
sos matrix: precomputed matrix, see |
center |
numeric(2): center of |
scale |
numeric(2): standard deviation for |
... |
further named parameters given to |
Returns an extended lm object and the additional list elements:
inter contains intermediate results (the last column contains the row sums), and
xy the generated - and -values.
data(sos100, package="exams.forge") n <- sample(5:10, 1) lm1 <- lm1_data(0.6, nmax=n, xsos=sos100) str(lm1)data(sos100, package="exams.forge") n <- sample(5:10, 1) lm1 <- lm1_data(0.6, nmax=n, xsos=sos100) str(lm1)
lm Simple Linear RegressionComputes an lm object for a simple linear regression from a range of x and y values,
including intermediate values. If r is not given then zero correlation is used (with cor_data).
digits determines the rounding for the x and y values. If only one value is given, then
it will be used for x and y. If no value is given then it will be determined from
the x and y values by 3+ceiling(-log10(diff(range(.)))).
lmr_data(xr, yr, n, r = 0, digits = NULL, ...) lm_regression_data(xr, yr, n, r = 0, digits = NULL, ...)lmr_data(xr, yr, n, r = 0, digits = NULL, ...) lm_regression_data(xr, yr, n, r = 0, digits = NULL, ...)
xr |
numeric: range of x values |
yr |
numeric: range of y values |
n |
numeric: number of observations to generate |
r |
numeric: desired correlation, uses |
digits |
numeric(2): digits for rounding, for x |
... |
further parameters used in |
An object of the class lm with the additional components:
x the generated x values
y the generated y values
sumx
sumy
sumx2
sumy2
sumxy
meanx the mean of x:
meany the mean of y:
varx the variation of x:
vary the variation of y:
varxy the common variation of x and y:
sxy the covariance of x and y
rxy the correlation of x and y
b0 the intercept of the linear regression
b1 the slope of the linear regression
r2 the coefficient of determination of the linear regression
# Engine displacement typically ranges from 500 to 2000 cm^3 # Fuel economy typically ranges from 2 to 8 liter/100 km lmr <- lmr_data(c(500, 2000), c(2, 8), n=8) str(lmr)# Engine displacement typically ranges from 500 to 2000 cm^3 # Fuel economy typically ranges from 2 to 8 liter/100 km lmr <- lmr_data(c(500, 2000), c(2, 8), n=8) str(lmr)
lsumprod creates a latex printout of with brackets if or starts with a -.
lsum creates a latex printout of as sum.
lprod creates a latex printout of as product.
lvec creates a latex printout of as vector.
lmean creates a latex printout as .
lvar creates a latex printout as .
lbr creates a latex printout of with brackets if starts with a -.
lsgn creates a latex printout of with a plus or minus at the beginning.
lsumprod(..., br = "(") lsum(x) lprod(x) lvec( x, left = c("(", "[", "{", "|", "||", "<", "a", "c", "f"), right = NULL, collapse = ", " ) lmean(x) lvar(x, mu = NULL, br = "(") lbr(x, br = c("(", "[", "{", "|", "||", "<", "a", "c", "f"), subset = NULL) lsgn(x) latex_sumprod(..., br = "(") latex_sum(x) latex_product(x) latex_mean(x) latex_var(x, mu = NULL, br = "(") latex_bracket( x, br = c("(", "[", "{", "|", "||", "<", "a", "c", "f"), subset = NULL ) latex_pmsign(x)lsumprod(..., br = "(") lsum(x) lprod(x) lvec( x, left = c("(", "[", "{", "|", "||", "<", "a", "c", "f"), right = NULL, collapse = ", " ) lmean(x) lvar(x, mu = NULL, br = "(") lbr(x, br = c("(", "[", "{", "|", "||", "<", "a", "c", "f"), subset = NULL) lsgn(x) latex_sumprod(..., br = "(") latex_sum(x) latex_product(x) latex_mean(x) latex_var(x, mu = NULL, br = "(") latex_bracket( x, br = c("(", "[", "{", "|", "||", "<", "a", "c", "f"), subset = NULL ) latex_pmsign(x)
... |
further input values |
br, left, right
|
character: which brackets to use. The possibilities are:
|
x |
numeric: input values |
collapse |
character: an optional character string to separate the results (default: ', ') |
mu |
numeric: population mean (default: |
subset |
logical: indicates which elements have brackets added (default: |
A character.
lsumprod(-2:2, (1:5)/10) lbr(-2:2) lsum(-2:2) lmean(-2:2) lvec(-2:2) lvec(-2:2, '[') lvec(0:1, '(', ']')lsumprod(-2:2, (1:5)/10) lbr(-2:2) lsum(-2:2) lmean(-2:2) lvec(-2:2) lvec(-2:2, '[') lvec(0:1, '(', ']')
Generates a character key from a vector of integers.
makekey(index) make_key(index)makekey(index) make_key(index)
index |
integer: vector of integer |
A character.
makekey(1) makekey(1:2) makekey(pi) # ;) makekey(c(5,4))makekey(1) makekey(1:2) makekey(pi) # ;) makekey(c(5,4))
Computes all modes (most common value).
mcval(x, ...) ## Default S3 method: mcval(x, ...) ## S3 method for class 'histogram' mcval(x, exact = FALSE, ...) compute_modes(x, ...) mcv(x, ...)mcval(x, ...) ## Default S3 method: mcval(x, ...) ## S3 method for class 'histogram' mcval(x, exact = FALSE, ...) compute_modes(x, ...) mcv(x, ...)
x |
data object |
... |
further arguments |
exact |
logical: either compute the exact mode or use class mids (default: |
A vector of modes.
x <- sample(1:5, 15, replace=TRUE) mcval(x)x <- sample(1:5, 15, replace=TRUE) mcval(x)
The meanint_data function generates a set of integer observations with a specified integer mean.
It takes the number of observations or x values and an optional range parameter, r, that defines the permissible range
of x values (defaulting to the range of x). Additional parameters are passed to the mean function.
The function employs a iterative process, adjusting individual observations to achieve an integer mean.
It uses a random selection approach, modifying a randomly chosen observation and checking if the resulting mean is closer to an integer.
The process continues until the mean becomes an integer.
meanint_data(x, r = range(x), ...)meanint_data(x, r = range(x), ...)
x |
numeric: number of observations or x values |
r |
numeric: the range in which the x values allowed (default: |
... |
further parameters given to |
A set of integer observations with an integer mean.
x <- meanint_data(10, c(1, 10)) mean(x)x <- meanint_data(10, c(1, 10)) mean(x)
Computes the means of x. The list returned has an attribute "mindiff" which contains
the smallest distance between two mean values before rounding.
If winsor and/or trim is set to NA then the trimmed and/or winsorized means are not computed.
Currently implemented are:
meanarithmetic mean
medianmedian
harmonicharmonic mean
geometricgeometric mean
mode(first) mode
trimtrimmed mean
winsorwinsorized mean
means_choice(x, digits, na.rm = TRUE, trim = 0.2, winsor = 0.2) means(x, digits, na.rm = TRUE, trim = 0.2, winsor = 0.2)means_choice(x, digits, na.rm = TRUE, trim = 0.2, winsor = 0.2) means(x, digits, na.rm = TRUE, trim = 0.2, winsor = 0.2)
x |
numeric: data values |
digits |
numeric: integer indicating the number of decimal points for rounding (negative values are allowed) |
na.rm |
logical: should |
trim |
numeric: the fraction (0 to 0.5) of observations to be trimmed from each end of |
winsor |
numeric: the fraction (0 to 0.5) of observations to be moved from each end of |
A list with mean values.
x <- c(runif(9), 3) means_choice(x, 2)x <- c(runif(9), 3) means_choice(x, 2)
Returns the MIME type of an image based on the filename extension.
If a MIME type for a file extension cannot not found, then the extension itself will be returned.
mime_image(filename) mime_img(filename)mime_image(filename) mime_img(filename)
filename |
character: file name |
A character.
mime_image("support.png") mime_image("support.jpg")mime_image("support.png") mime_image("support.jpg")
Creates a polynomial of the form .
monomial(degree = 1, coefficient = 1) monom(degree = 1, coefficient = 1)monomial(degree = 1, coefficient = 1) monom(degree = 1, coefficient = 1)
degree |
integer: degree of the polynomial (default: |
coefficient |
numeric: coefficient of the polynomial (default: |
A polynomial
monomial() # equivalent to polynomial() monomial(3) # x^3 monomial(3, 2) # 2*x^3monomial() # equivalent to polynomial() monomial(3) # x^3 monomial(3, 2) # 2*x^3
The exams package does not support multiple-choice questions with multiple correct answers; it only allows for one answer to be chosen.
However, Moodle does support such questions. The function reads the XML file generated by exams.forge and makes changes
for all mchoice questions:
<single>...</single> to <single>true</single>, and
modifies the attribute fraction in the tags <answer fraction="...">...</answer>.
If fraction is less than 0, it is set to zero, and if fraction is greater than 0, it is set to 100.
If the file does not end with .xml, then .xml is appended. At the end, the modified XML code is stored in newfile.
moodle_m2s(file, newfile = NULL, verbose = 1) mchoice_moodle(file, newfile = NULL, verbose = 1)moodle_m2s(file, newfile = NULL, verbose = 1) mchoice_moodle(file, newfile = NULL, verbose = 1)
file |
character: Moodle XML file with exercises to read from |
newfile |
character: Moodle XML file to write in (default: |
verbose |
integer: output generation (default: |
Invisibly, the written file name.
if (interactive()) { newfile <- tempfile(fileext=".xml") moodle_m2s(system.file("xml", "klausur-test.xml", package="exams.moodle"), newfile=newfile) file.edit(newfile) }if (interactive()) { newfile <- tempfile(fileext=".xml") moodle_m2s(system.file("xml", "klausur-test.xml", package="exams.moodle"), newfile=newfile) file.edit(newfile) }
It determines the nearest candidate value for each value in arg.
As a replacement for [base::match.arg], it is more error-tolerant, but detecting a wrong choice can be proven challenging.
nearest_arg(arg, choices, method = "cosine", ...)nearest_arg(arg, choices, method = "cosine", ...)
arg |
character: vector or NULL |
choices |
character: vector of candidate values |
method |
character: method for distance calculation (default: |
... |
further parameters for stringdist::stringdistmatrix |
For each value in arg the (first) nearest element of choices.
# match.arg("tow.sided", c("two.sided", "less", "greater")) # will fail nearest_arg("tow.sided", c("two.sided", "less", "greater")) nearest_arg(c("two.sided", "less", "greater"), c("two.sided", "less", "greater")) nearest_arg(c("two", "two", "ded", "ss", "ea"), c("two.sided", "less", "greater"))# match.arg("tow.sided", c("two.sided", "less", "greater")) # will fail nearest_arg("tow.sided", c("two.sided", "less", "greater")) nearest_arg(c("two.sided", "less", "greater"), c("two.sided", "less", "greater")) nearest_arg(c("two", "two", "ded", "ss", "ea"), c("two.sided", "less", "greater"))
nosanitize makes no sanitization on the strings.
nosanitize(str)nosanitize(str)
str |
character: vector to sanitize |
A sanitized character vector.
nosanitize("Test")nosanitize("Test")
Returns a time stamp based on the current time. now basically calls
gsub('.', '', sprintf('%.20f', as.numeric(Sys.time())), fixed=TRUE).
To ensure that at each call a different time stamp is delivered now
may call gsub(...) several times until two different results are delivered.
The last one is then returned.
now(last = 35)now(last = 35)
last |
integer: the amount of digits that should be returned (default: |
A character.
now() # returns all digits now(3) # returns only the first three digitsnow() # returns all digits now(3) # returns only the first three digits
n
nsprintf creates a text dependent on the value(s) in n. In particular, we have
round_de, it returns either Runden Sie Ihr Ergebnis auf eine ganze Zahl,
Runden Sie Ihr Ergebnis auf eine Stelle nach dem Komma, or
Runden Sie Ihr Ergebnis auf n Stellen nach dem Komma
schoice_de returns Es kann eine oder mehrere Antworten richtig sein. Es ist ausreichend, eine richtige Antwort anzugeben. Geben Sie mehrere Antworten an und eine ist falsch, dann ist die Aufgabe falsch beantwortet
nsprintf(n, ...) round_de(n) schoice_de() print_de(n, ...)nsprintf(n, ...) round_de(n) schoice_de() print_de(n, ...)
n |
integer: number(s) to be used |
... |
character: format strings to be used |
sprintfed strings
nsprintf(0, '0' = "keine Netzunterbrechung", '1' = "eine Netzunterbrechung", "%i Netzunterbrechungen") nsprintf(0:3, `0` = "keine Netzunterbrechung", `1` = "eine Netzunterbrechung", "%i Netzunterbrechungen")nsprintf(0, '0' = "keine Netzunterbrechung", '1' = "eine Netzunterbrechung", "%i Netzunterbrechungen") nsprintf(0:3, `0` = "keine Netzunterbrechung", `1` = "eine Netzunterbrechung", "%i Netzunterbrechungen")
num_result creates a list summarizing numeric values with rounding and tolerance.
It returns the original values, rounded values, the digits used, and the tolerance.
The rounding is done with the internal function fmt() (similar to exams::fmt()).
Users can pass additional arguments to fmt() via ....
x: original numeric values
fx: rounded values as character (via fmt())
tolerance: numeric tolerance for comparison
digits: digits used for rounding
If digits is not provided:
If length(x) > 1, ceiling(-log10(min(diff(sort(x)), na.rm = TRUE))) is used.
If length(x) == 1, 3 + ceiling(-log10(abs(x))) is used.
If tolerance is not provided, it defaults to tolmult * 10^(1 - digits).
int_result() is a shortcut for integer values (digits = 0, tolerance = 0.1).
num_result(x, digits = NULL, tolerance = NULL, tolmult = 2, ...) int_result(x, ...) num_res(x, digits = NULL, tolerance = NULL, tolmult = 2, ...) int_res(x, ...)num_result(x, digits = NULL, tolerance = NULL, tolmult = 2, ...) int_result(x, ...) num_res(x, digits = NULL, tolerance = NULL, tolmult = 2, ...) int_res(x, ...)
x |
numeric: the input values |
digits |
numeric: number of digits to round to (default: |
tolerance |
numeric: optional numeric tolerance (default: |
tolmult |
numeric: multiplier for tolerance calculation (default: 2) |
... |
further arguments passed to
|
A list with elements x, fx, tolerance, and digits.
# Example: numeric values x <- rnorm(10, mean = 1.8, sd = 0.25) num_result(c(mean(x), x), digits = 2) # Example: integer result int_result(mean(x)) # Example: different digits and tolerance num_result(pi, 3) num_result(pi, 6) num_result(pi, 6, tolmult = 5) num_result(pi, 6, tolmult = 5, tolerance = 1e-6)# Example: numeric values x <- rnorm(10, mean = 1.8, sd = 0.25) num_result(c(mean(x), x), digits = 2) # Example: integer result int_result(mean(x)) # Example: different digits and tolerance num_result(pi, 3) num_result(pi, 6) num_result(pi, 6, tolmult = 5) num_result(pi, 6, tolmult = 5, tolerance = 1e-6)
Given a set of equations and some variables, num_solve tries to compute the value of the target variable.
The equations are transformed to and the functions try to compute the roots of the equations
using [stats::uniroot()].
If the computation fails, then, numeric(0) is returned, otherwise the "original" value. If target=='' then all computed values
and steps are returned. The attribute compute contains a data frame.
toLatex.equation_solve returns a LaTeX representation of the solution way found by num_solve().
num_solve(target, eqs, tol = 1e-06) ## S3 method for class 'equation_solve' toLatex(object, ...) sequation(target, eqs, tol = 1e-06)num_solve(target, eqs, tol = 1e-06) ## S3 method for class 'equation_solve' toLatex(object, ...) sequation(target, eqs, tol = 1e-06)
target |
character: name of the variable value to compute |
eqs |
an |
tol |
numeric: maximal tolerance for |
object |
object of a class for which a |
... |
further arguments |
(for num_solve) Returns numeric(0), numeric(1), or a list of all (computed) values.
(For toLatex.equation_solve) A character vector.
# The equations describe the formulae for an confidence interval of the mean e <- equations(o~x+c*s/sqrt(n), "v_o=\\bar{x}+c\\cdot\\frac{s^2}{n}", u~x-c*s/sqrt(n), "v_u=\\bar{x}-c\\cdot\\frac{s^2}{n}", e~c*s/sqrt(n), "e =c\\cdot\\frac{s^2}{\\sqrt{n}}", l~2*e, "l =2\\cdot e" ) e <- variables(e, x=0, "\\bar{x}", c=2.58, dbl(2), s=1, pos(5), "s^2", n=25, pos(5), l=pos(5), e=pos(5), u="v_u", o="v_o") print(e) # Find the confidence interval length ns <- num_solve('l', e) # Compute everything that is possible ns <- num_solve('', e) toLatex(ns)# The equations describe the formulae for an confidence interval of the mean e <- equations(o~x+c*s/sqrt(n), "v_o=\\bar{x}+c\\cdot\\frac{s^2}{n}", u~x-c*s/sqrt(n), "v_u=\\bar{x}-c\\cdot\\frac{s^2}{n}", e~c*s/sqrt(n), "e =c\\cdot\\frac{s^2}{\\sqrt{n}}", l~2*e, "l =2\\cdot e" ) e <- variables(e, x=0, "\\bar{x}", c=2.58, dbl(2), s=1, pos(5), "s^2", n=25, pos(5), l=pos(5), e=pos(5), u="v_u", o="v_o") print(e) # Find the confidence interval length ns <- num_solve('l', e) # Compute everything that is possible ns <- num_solve('', e) toLatex(ns)
Converts a set of numeric variables to a list as string representation, either as decimal or as a fractional number.
num2str(..., denom = -1)num2str(..., denom = -1)
... |
numeric variables |
denom |
integer: denominator for fractional number |
A list.
x <- 1 l <- num2str(x) # returns in l$x the string representation l <- num2str(x, y=x+1) # returns in l$x and l$y the string representationsx <- 1 l <- num2str(x) # returns in l$x the string representation l <- num2str(x, y=x+1) # returns in l$x and l$y the string representations
The open_files function allows users to manage multiple files by either opening them interactively
or reading their contents programmatically. It supports interactive selection through a menu
for multiple file paths and adapts to the availability of the rstudioapi package for enhanced
integration with the RStudio IDE.
open_files(file, open = interactive(), ...)open_files(file, open = interactive(), ...)
file |
character: vector of file paths, where each path specifies the location of a file to be opened or read; paths may be absolute or relative |
open |
logical: Whether to open the files interactively ( |
... |
Additional arguments to be passed to the file opening or editing function;
these arguments are passed either to |
When the file argument contains more than one file path, the function calculates the
longest common subsequence (LCS) of the directory structures for all provided file paths
and presents the user with an interactive menu. The menu allows selection of files to open;
the options include "All," "None," or individual files that match the common directory pattern.
In non-interactive sessions, the function reads the content of the files instead.
Internally, the function determines whether the rstudioapi package is available, in which case
it uses navigateToFile for opening files within the RStudio IDE; otherwise, it falls back to the
base R function edit. In non-interactive sessions, the function reads file contents using
readLines and stores them in a named list, with filenames as the names of the list elements.
The auxiliary lcss function calculates the longest common subsequence of directory components
for the file paths; this helps in simplifying the user experience by grouping files with similar paths.
This documentation was created with the support of ChatGPT.
Returns an invisible named list. Each element corresponds to a file from the input vector, with the
value representing the result of the operation. When opening files, the result indicates success or failure.
When reading files, the result contains the file's contents. If "None" is selected, the function returns NULL.
if (interactive()) { files <- pkg.files("poylnomials", package="polynom") open_files(files[2]) # open one file open_files(files) # open several files }if (interactive()) { files <- pkg.files("poylnomials", package="polynom") open_files(files[2]) # open one file open_files(files) # open several files }
Creates a linear (power=1) or constant (power=0) density function in a interval
where
a and b are sampled from x. It samples size elements without replacement and computes
the value of the distribution function.
pdensity(x, size = 3, power = 1, tol = 1e-06) sample_density(x, size = 3, power = 1, tol = 1e-06)pdensity(x, size = 3, power = 1, tol = 1e-06) sample_density(x, size = 3, power = 1, tol = 1e-06)
x |
numeric: range of density with |
size |
numeric: number of elements to be sampled (without replacement) from x |
power |
numeric: constant or linear density function |
tol |
numeric: disallow for density coefficients near zero (default: |
A list with:
a the minimum of the interval
i the maximum of the interval
x the size sampled values
fx the distribution function at x
pcoeff a polynomial (intercept = first value)
qcoeff indefinite integral of the polynomial (intercept = first value)
pint result of the integral(pcoeff, c(a,b), 0:2)
pdensity(-5:5) pdensity(-5:5, power=1)pdensity(-5:5) pdensity(-5:5, power=1)
Generates an integer data set for computing a correlation using sumofsquares().
If n>100 and nmax>6 it is better to use one of the precomputed solutions. Otherwise it may take
up to maxt seconds. Please note that the correlation of the generated data set may differ from the desired
correlation.
pearson_data(r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL) dpearson(r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL)pearson_data(r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL) dpearson(r, n = 100, nmax = 6, maxt = 30, xsos = NULL, ysos = NULL)
r |
numeric: desired correlation |
n |
integer: number to decompose as sum of squares, see |
nmax |
integer: maximal number of squares in the sum, see |
maxt |
numeric: maximal number of seconds the routine should run, see |
xsos |
sos matrix: precomputed matrix |
ysos |
sos matrix: precomputed matrix |
A matrix with two columns and an attribute interim for intermediate values as matrix.
The rows of the matrix contain : , , , ,
, , and .
In a final step, a vector with the row of sums is appended as a further column.
data(sos100, package="exams.forge") xy <- pearson_data(0.7, xsos=sos100) colSums(xy) colSums(xy^2) sum(xy[,1]*xy[,2]) # my data x <- 100+5*xy[,1] y <- 100+5*xy[,2] cor(x, y)data(sos100, package="exams.forge") xy <- pearson_data(0.7, xsos=sos100) colSums(xy) colSums(xy^2) sum(xy[,1]*xy[,2]) # my data x <- 100+5*xy[,1] y <- 100+5*xy[,2] cor(x, y)
Computes the minimum of a polynomial in the interval . The values and the interval borders of the
polynomial p are evaluated and the minimum value is returned.
pminimum( p, interval, lower = min(interval), upper = max(interval), tol = 1e-09 ) polynomial_minimum( p, interval, lower = min(interval), upper = max(interval), tol = 1e-09 )pminimum( p, interval, lower = min(interval), upper = max(interval), tol = 1e-09 ) polynomial_minimum( p, interval, lower = min(interval), upper = max(interval), tol = 1e-09 )
p |
polynomial |
interval |
numeric: a vector containing the end-points of the interval to be searched for the minimum |
lower |
numeric: the lower end point of the interval to be searched (default: |
upper |
numeric: the upper end point of the interval to be searched (default: |
tol |
numeric: the desired accuracy (default: |
The minimal function value.
p <- polynomial(c(-5, 3, -3, 1)) pminimum(p, -3, 3)p <- polynomial(c(-5, 3, -3, 1)) pminimum(p, -3, 3)
Generates intervals based on powers of ten.
pos(pow) neg(pow) dbl(pow) idbl(pow) ipos(pow) ineg(pow)pos(pow) neg(pow) dbl(pow) idbl(pow) ipos(pow) ineg(pow)
pow |
numeric: power of ten to create intervals |
A numeric object.
dbl(2) dbl(3) pos(3) neg(3)dbl(2) dbl(3) pos(3) neg(3)
Creates for each value of a discrete random variable, a polynomial and estimates the least squares and the maximum likelihood solution. The following conditions stand:
If sample is not given then the sample contains each x value once.
If sample is an integer, then it is interpreted as the sample size and a sample is generated by rmultinom(1, sample, ddiscrete(runif(length(x)))).
If sample is a vector, it is interpreted in such a way that the corresponding x[i] value occurs i times in the sample. Thus, sum(sample) is the sample size.
If coeff is a polylist of length(x), then these polynomials are taken.
If coeff is a matrix with length(x), columns and power+1 rows, then the columns are interpreted as the coefficients of a polynomial.
Otherwise coeff is interpreted as a vector from which the coefficient is sampled. The intercepts are sampled via ddiscrete(runif(length(x)), zero=zero).
If coeff is not given then it is ensured that the least squares and the maximum likelihood solution exists and the estimated probabilities are between zero and one.
Otherwise, the results may contain NA or the estimated probabilities are outside the interval .
pprobability( x, power = 1, zero = FALSE, coef = round(seq(-1, 1, by = 0.1), 1), sample = rep(1, length(x)), pl = NULL, tol = 1e-09 ) polynomial_probability( x, power = 1, zero = FALSE, coef = round(seq(-1, 1, by = 0.1), 1), sample = rep(1, length(x)), pl = NULL, tol = 1e-09 )pprobability( x, power = 1, zero = FALSE, coef = round(seq(-1, 1, by = 0.1), 1), sample = rep(1, length(x)), pl = NULL, tol = 1e-09 ) polynomial_probability( x, power = 1, zero = FALSE, coef = round(seq(-1, 1, by = 0.1), 1), sample = rep(1, length(x)), pl = NULL, tol = 1e-09 )
x |
numeric: values of a discrete random variable |
power |
integer: the degree for the polynomials (default: |
zero |
logical: are zero coefficients and zero samples allowed? (default: |
coef |
matrix: for each degree coefficients to sample from (default: |
sample |
integer: number of |
pl |
polylist: a list of polynomials which describes the probability for |
tol |
numeric: tolerance to detect zero values (default: |
A list with the components:
p: the polynomials for the probabilities
ep: the expected value as polynomial
x: the values for the discrete random variable, the same as the input x
sample: the sample given or generated
LS$pi: the summands for the least squares problem
LS$pl: the summands for the least squares problem in LaTeX
LS$pf: the sum of LS$pi
LS$df: the derivative of LS$pf
LS$pest: the estimated parameter, minimum of LS$pf
LS$p: the estimated probabilities
ML$pi: the factors for the maximum likelihood problem
ML$pl: the summands for the maximum likelihood problem in LaTeX
ML$pf: the product of ML$pi
ML$df: the derivative of ML$pf
ML$pest: the estimated parameter, maximum of ML$pf
ML$p: the estimated probabilities
# linear polynomials pprobability(0:2) pprobability(0:2, power=1) # constant polynomials, some NAs are generated pprobability(0:3, power=0) # polynomials generated from a different set pprobability(0:2, coef=seq(-2, 2, by=0.1)) pprobability(0:2, 0, coef=seq(-2, 2, by=0.1)) # polynomials (x, x, 1-2*x) are used pprobability(0:2, 0, coef=matrix(c(0.4, 0.4, 0.3), ncol=3)) pprobability(0:2, 1, coef=polylist(c(0,1), c(0,1), c(1, -2)))# linear polynomials pprobability(0:2) pprobability(0:2, power=1) # constant polynomials, some NAs are generated pprobability(0:3, power=0) # polynomials generated from a different set pprobability(0:2, coef=seq(-2, 2, by=0.1)) pprobability(0:2, 0, coef=seq(-2, 2, by=0.1)) # polynomials (x, x, 1-2*x) are used pprobability(0:2, 0, coef=matrix(c(0.4, 0.4, 0.3), ncol=3)) pprobability(0:2, 1, coef=polylist(c(0,1), c(0,1), c(1, -2)))
Prints an equations object with equations and variables. Internally, a data frame is generated, created and printed.
## S3 method for class 'equations' print(x, ...)## S3 method for class 'equations' print(x, ...)
x |
an object used to select a method. |
... |
further arguments passed to or from other methods. |
The data frame invisibly generated.
# The equations describe the formulae for an confidence interval of the mean e <- equations(o~x+c*s/sqrt(n), "v_o=\\bar{x}+c\\cdot\\frac{s^2}{n}", u~x-c*s/sqrt(n), "v_u=\\bar{x}-c\\cdot\\frac{s^2}{n}", e~c*s/sqrt(n), "e =c\\cdot\\frac{s^2}{\\sqrt{n}}", l~2*e, "l =2\\cdot e" ) print(e)# The equations describe the formulae for an confidence interval of the mean e <- equations(o~x+c*s/sqrt(n), "v_o=\\bar{x}+c\\cdot\\frac{s^2}{n}", u~x-c*s/sqrt(n), "v_u=\\bar{x}-c\\cdot\\frac{s^2}{n}", e~c*s/sqrt(n), "e =c\\cdot\\frac{s^2}{\\sqrt{n}}", l~2*e, "l =2\\cdot e" ) print(e)
html_matrix
Prints an HTML matrix content or its components.
## S3 method for class 'html_matrix' print(x, ..., which = "")## S3 method for class 'html_matrix' print(x, ..., which = "")
x |
an html_matrix object |
... |
further parameters |
which |
character: which component to print (default: |
An invisible character matrix.
m <- matrix(1:6, ncol=2) l <- html_matrix_sk(m, title="1 to 6", fmt=rep("%f",ncol(m))) print(l, which=NA) # returns full style information print(l, which="fmt") # returns format information print(l, which="value") # identical to print(l)m <- matrix(1:6, ncol=2) l <- html_matrix_sk(m, title="1 to 6", fmt=rep("%f",ncol(m))) print(l, which=NA) # returns full style information print(l, which="fmt") # returns format information print(l, which="value") # identical to print(l)
The following functions are available:
prob_solve given a set of events it computes the total or conditional probability of the given event or
NA if no solution could be found. For the naming of the events upper case letters must be used and
the available operators are ! (complementary event), | (conditional event), and ^ (intersection of events).
The attribute latex of the return value contains the necessary computation steps for computation of the given event.
If getprob is TRUE then additionally the attribute prob, a vector with all computed probabilities, and compute,
which includes all computational steps, are generated.
print shows the solution way in ASCII.
toLatex shows the solution way in LaTeX/MathJax with an align environment.
lprob converts !A to \\bar{A} and A^B to A \\cap B.
prob_solve(target, ...) ## Default S3 method: prob_solve(target, ..., partition = NULL, getprob = FALSE, quiet = TRUE) lprob(txt) ## S3 method for class 'prob_solve' toLatex(object, ...) ## S3 method for class 'prob_solve' print(x, type = c("numeric", "latex", "prob", "compute"), ...) latex_prob(txt) probability_solution(target, ...) sprob(target, ...)prob_solve(target, ...) ## Default S3 method: prob_solve(target, ..., partition = NULL, getprob = FALSE, quiet = TRUE) lprob(txt) ## S3 method for class 'prob_solve' toLatex(object, ...) ## S3 method for class 'prob_solve' print(x, type = c("numeric", "latex", "prob", "compute"), ...) latex_prob(txt) probability_solution(target, ...) sprob(target, ...)
target |
character: target event |
... |
numeric: named events with given probabilities |
partition |
character or list: set of events which form a partition |
getprob |
logical: return all computed probabilities and used computation steps (default: |
quiet |
logical: show all computation steps (default: |
txt |
character: vector to convert |
object, x
|
|
type |
character: what to print, either |
The program applies iteratively the following rules to find a solution:
,
,
,
,
, and
for a partition .
An object of the class prob_solve with the resulting probability, including the steps for computing.
If NA is returned then no solution could be found.
prob_solve("!A", "A"=0.3) prob_solve("!A|B", "A|B"=0.3) prob_solve("B^A", "A^B"=0.3) # P(B) = P(A^B)+P(!A^B) prob_solve("B", "A^B"=0.3, "!A^B"= 0.4) prob_solve("A^B", "B"=0.7, "!A^B"= 0.4) prob_solve("!A^B", "B"=0.7, "A^B"= 0.3) # P(A|B) = P(A^B)/P(B) prob_solve("A|B", "A^B"=0.3, "B"= 0.6) prob_solve("A^B", "B"=0.6, "A|B"= 0.5) prob_solve("B", "A|B"=0.5, "A^B"= 0.3) #' latex, prob and compute attributes pmt <- prob_solve("M|T", "M"=0.6, "T|M"=0.75, "T|!M"=0.39, quiet=FALSE, getprob=TRUE) toLatex(pmt) attr(pmt, "latex") pmt <- prob_solve("M|T", "M"=0.6, "T|M"=0.75, "T|!M"=0.39, quiet=FALSE, getprob=TRUE) attr(pmt, "prob") print(pmt, "latex") print(pmt, "prob") # only if getprob=TRUE print(pmt, "compute") # only if getprob=TRUE # bayes theorem and total probability prob_solve("Z", "Z|A"=0.1, "Z|B"=0.2, "Z|C"=0.3, partition=c("A", "B", "C")) prob_solve("Z|A", "Z"=0.6, "Z|B"=0.2, "Z|C"=0.3, partition=c("A", "B", "C")) prob_solve('A|K', "A"=0.55, "B"=0.35, "C"=0.1, "K|A"=0.4, "K|B"=0.1, "K|C"=0.1, partition=c("A", "B", "C")) prob_solve('K', "A"=0.55, "B"=0.35, "C"=0.1, "K|A"=0.4, "K|B"=0.1, "K|C"=0.1, partition=c("A", "B", "C"))prob_solve("!A", "A"=0.3) prob_solve("!A|B", "A|B"=0.3) prob_solve("B^A", "A^B"=0.3) # P(B) = P(A^B)+P(!A^B) prob_solve("B", "A^B"=0.3, "!A^B"= 0.4) prob_solve("A^B", "B"=0.7, "!A^B"= 0.4) prob_solve("!A^B", "B"=0.7, "A^B"= 0.3) # P(A|B) = P(A^B)/P(B) prob_solve("A|B", "A^B"=0.3, "B"= 0.6) prob_solve("A^B", "B"=0.6, "A|B"= 0.5) prob_solve("B", "A|B"=0.5, "A^B"= 0.3) #' latex, prob and compute attributes pmt <- prob_solve("M|T", "M"=0.6, "T|M"=0.75, "T|!M"=0.39, quiet=FALSE, getprob=TRUE) toLatex(pmt) attr(pmt, "latex") pmt <- prob_solve("M|T", "M"=0.6, "T|M"=0.75, "T|!M"=0.39, quiet=FALSE, getprob=TRUE) attr(pmt, "prob") print(pmt, "latex") print(pmt, "prob") # only if getprob=TRUE print(pmt, "compute") # only if getprob=TRUE # bayes theorem and total probability prob_solve("Z", "Z|A"=0.1, "Z|B"=0.2, "Z|C"=0.3, partition=c("A", "B", "C")) prob_solve("Z|A", "Z"=0.6, "Z|B"=0.2, "Z|C"=0.3, partition=c("A", "B", "C")) prob_solve('A|K', "A"=0.55, "B"=0.35, "C"=0.1, "K|A"=0.4, "K|B"=0.1, "K|C"=0.1, partition=c("A", "B", "C")) prob_solve('K', "A"=0.55, "B"=0.35, "C"=0.1, "K|A"=0.4, "K|B"=0.1, "K|C"=0.1, partition=c("A", "B", "C"))
Creates data for a binomial test based on the properties for the test.
proptest_data( size = 10:100, prob = seq(0.05, 0.45, by = 0.05), reject = TRUE, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), norm.approx = NA, maxit = 1000 ) prop_binomtest_data( size = 10:100, prob = seq(0.05, 0.45, by = 0.05), reject = TRUE, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), norm.approx = NA, maxit = 1000 ) dbinomtest( size = 10:100, prob = seq(0.05, 0.45, by = 0.05), reject = TRUE, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), norm.approx = NA, maxit = 1000 )proptest_data( size = 10:100, prob = seq(0.05, 0.45, by = 0.05), reject = TRUE, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), norm.approx = NA, maxit = 1000 ) prop_binomtest_data( size = 10:100, prob = seq(0.05, 0.45, by = 0.05), reject = TRUE, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), norm.approx = NA, maxit = 1000 ) dbinomtest( size = 10:100, prob = seq(0.05, 0.45, by = 0.05), reject = TRUE, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), norm.approx = NA, maxit = 1000 )
size |
numeric: vector of sample sizes (default |
prob |
numeric: vector of probabilities for the hypothetical proportion |
reject |
logical: should |
alternative |
character: a character string specifying the alternative hypothesis, must be one of |
alpha |
numeric: vector of significance levels (default |
norm.approx |
logical: should a normal approximation be possible ( |
maxit |
integer: maximal numbers of trials to find a solution (default |
A list with the components:
pi0 hypothetical proportion
x counts of successes in the sample
n sample size
alpha significance level
alternative specifying the alternative hypothesis (either two.sided, greater or less)
proptest_data()proptest_data()
Computes all results for test on proportion using either stats::binom.test(), or
a normal approximation without continuity correction.
Either named parameters can be given or an arglist with the following parameters:
x number of successes
n sample size (default: sd(x))
pi0 true value of the proportion (default: 0.5)
alternative a string specifying the alternative hypothesis (default: "two.sided"), otherwise "greater" or "less" can be used
alpha significance level (default: 0.05)
binom2norm can the binomial distribution be approximated by a normal distribution? (default: NA = use binom2norm function)
proptest_num(..., arglist = NULL) prop_binomtest_num(..., arglist = NULL) nbinomtest(..., arglist = NULL)proptest_num(..., arglist = NULL) prop_binomtest_num(..., arglist = NULL) nbinomtest(..., arglist = NULL)
... |
named input parameters |
arglist |
list: named input parameters, if given |
The results of proptest_num may differ from stats::binom.test(). proptest_num is designed to return results
when you compute a binomial test by hand. For example, for computing the test statistic the approximation
is used if . The p.value is computed by stats::binom.test and may not be reliable, for Details see Note!
A list with the input parameters and the following:
X distribution of the random sampling function
Statistic distribution of the test statistics
statistic test value
critical critical value(s)
criticalx critical value(s) in x range
acceptance0 acceptance interval for H0
acceptance0x acceptance interval for H0 in x range
accept1 is H1 accepted?
p.value p value for test (note: the p-value may not be reliable see Notes!)
alphaexact exact significance level
stderr standard error of the proportion used as denominator
The computation of a p-value for non-symmetric distribution is not well defined, see https://stats.stackexchange.com/questions/140107/p-value-in-a-two-tail-test-with-asymmetric-null-distribution.
n <- 100 x <- sum(runif(n)<0.4) proptest_num(x=x, n=n)n <- 100 x <- sum(runif(n)<0.4) proptest_num(x=x, n=n)
proptests runs a bunch of modifications of the input parameters of proptest to generate all possible proportion tests.
See under "Details" the detailed parameter values which are used. Note that not giving the parameter hyperloop will
results in several hundred tests generated.
Only the distinct tests will be returned, with the first element being proptest. If only a specific element of a proptests is of interest,
provide the name of the element in elem. All proptests will then be returned where the value of elem is different.
proptests(proptest, elem = NULL, hyperloop = NULL)proptests(proptest, elem = NULL, hyperloop = NULL)
proptest |
proptest: the base result from a valid t-test generated by |
elem |
character: element to extract (default: |
hyperloop |
named list: parameter values to run over (default: see above) |
The default hyperloop is:
list(x = c(proptest$x, proptest$n-proptest$x)
pi0 = c(proptest$pi0, 1-proptest$pi0, proptest$x/proptest$n, 1-proptest$x/proptest$n)
alpha = unique(c(proptest$alpha, 0.01, 0.05, 0.1)),
alternative = c("two.sided", "greater", "less")
)
list of proptest objects is returned
basetest <- proptest_num(x=3, n=8, alternative="greater") # vary the number of observations hyperloop <- list(pi0 = c(basetest$pi0, 1-basetest$pi0, basetest$x/basetest$n, 1-basetest$x/basetest$n)) # return all different tests tts <- proptests(basetest, hyperloop=hyperloop) # return all different random sampling functions proptests(basetest, "X", hyperloop)basetest <- proptest_num(x=3, n=8, alternative="greater") # vary the number of observations hyperloop <- list(pi0 = c(basetest$pi0, 1-basetest$pi0, basetest$x/basetest$n, 1-basetest$x/basetest$n)) # return all different tests tts <- proptests(basetest, hyperloop=hyperloop) # return all different random sampling functions proptests(basetest, "X", hyperloop)
Given two (or more) quantiles it computes an (approximate) mean and standard deviation for a corresponding normal distribution.
q2norm(x, probs = c(0.025, 0.975))q2norm(x, probs = c(0.025, 0.975))
x |
numeric(2): the quantiles |
probs |
numeric(2): probabilities with values in |
A list with a component mean and sd.
q2norm(c(100,200))q2norm(c(100,200))
Returns a index from 1:length(v) randomly ordered.
random(v) rand(v)random(v) rand(v)
v |
vector: vector with elements |
Index
random(-3:3)random(-3:3)
Creates names for elements of a vector.
refer(x, fmt = "%s_{%.0f}", to = deparse(substitute(x)), index = 1:length(x)) refer2vector( x, fmt = "%s_{%.0f}", to = deparse(substitute(x)), index = 1:length(x) )refer(x, fmt = "%s_{%.0f}", to = deparse(substitute(x)), index = 1:length(x)) refer2vector( x, fmt = "%s_{%.0f}", to = deparse(substitute(x)), index = 1:length(x) )
x |
vector: a vector to create the names for |
fmt |
character: format string for |
to |
character: base name of elements |
index |
numeric: vector with indices (default: |
A character vector
x <- runif(5) refer(x) # LaTeX default refer(x, fmt="%s[%.0f]") # R defaultx <- runif(5) refer(x) # LaTeX default refer(x, fmt="%s[%.0f]") # R default
In a text it replaces names with:
values which are formatted with exams::fmt(), or
strings
replace_fmt(txt, digits = 2L, ...)replace_fmt(txt, digits = 2L, ...)
txt |
character: text where the replacement is done |
digits |
numeric or list: number of digits to round |
... |
names to replace with values |
A character with replaced names.
replace_fmt("\\frac{x}{y}", x=2, y=3) replace_fmt("\\frac{x}{y}", x=2, y=3, digits=0) replace_fmt("\\frac{x}{y}", x=2, y=3, digits=list(0)) replace_fmt("\\frac{x}{y}", x=2, y=3, digits=list(2, y=0)) replace_fmt("\\frac{x}{y}", x="\\\\sum_{i=1}^n x_i", y="\\\\sum_{i=1}^n y_i")replace_fmt("\\frac{x}{y}", x=2, y=3) replace_fmt("\\frac{x}{y}", x=2, y=3, digits=0) replace_fmt("\\frac{x}{y}", x=2, y=3, digits=list(0)) replace_fmt("\\frac{x}{y}", x=2, y=3, digits=list(2, y=0)) replace_fmt("\\frac{x}{y}", x="\\\\sum_{i=1}^n x_i", y="\\\\sum_{i=1}^n y_i")
Formats a random variable and its meaning for R Markdown.
rv(symbol, explanation) rmdFormatRV(symbol, explanation) lrv(symbol, explanation)rv(symbol, explanation) rmdFormatRV(symbol, explanation) lrv(symbol, explanation)
symbol |
character: symbol |
explanation |
character: meaning |
A formatted string.
rv("X", "Waiting time in minutes until next event")rv("X", "Waiting time in minutes until next event")
Checks if a vector of possible sample sizes and relative frequencies create integer absolute frequencies.
sample_size_freq(n, f, which = NA) dnsizefreq(n, f, which = NA)sample_size_freq(n, f, which = NA) dnsizefreq(n, f, which = NA)
n |
numeric: vector of sample size(s) to check |
f |
numeric: vector of relative frequencies |
which |
numeric: if several |
One sample size.
f <- ddiscrete(runif(5), unit=100) sample_size_freq(seq(10, 200, 1), f) sample_size_freq(seq(10, 200, 1), f, which=200)f <- ddiscrete(runif(5), unit=100) sample_size_freq(seq(10, 200, 1), f) sample_size_freq(seq(10, 200, 1), f, which=200)
Rescales x such that for the rescaled data it holds: mean(scale_to(x, mean=target))==target and
sd(scale_to(x, sd=target)==abs(target). A negative value of sd will change the sign of the x values.
scale_to(x, mean = 0, sd = 1)scale_to(x, mean = 0, sd = 1)
x |
numeric: vector of values |
mean |
numeric: mean of the rescaled |
sd |
numeric: standard deviation of the transformed |
Rescaled data.
x <- runif(50) y <- scale_to(x, mean=0.1, sd=0.2) mean(y) sd(y) y <- scale_to(x, mean=0.1, sd=-0.2) mean(y) sd(y)x <- runif(50) y <- scale_to(x, mean=0.1, sd=0.2) mean(y) sd(y) y <- scale_to(x, mean=0.1, sd=-0.2) mean(y) sd(y)
A data frame with the variables and level of measurement type. The names are in German.
data(skalenniveau)data(skalenniveau)
A data frame with columns var, and type.
data(skalenniveau) head(skalenniveau)data(skalenniveau) head(skalenniveau)
Creates a solution object and prints a meta information block for the following:
solution the default is sol_num
sol_num for a numerical solution
sol_int for an integer solution
sol_mc for a multiple choice solution
sol_ans for the answer list of a multiple choice solution
sol_tf for the solution list (True or False) of a multiple choice solution
sol_info for creating a Meta-Information block
solution(x, ...) ## Default S3 method: solution(x, ...) sol_int(x, tol = NA, digits = NA) sol_num(x, tol = NA, digits = NA) sol_mc(x, y, sample = NULL, shuffle = order, none = NULL) sol_ans(x, ...) sol_tf(x, ...) sol_info(x, ...) sol_mc_ans(x, ...) sol_meta(x, ...) sol_mc_tf(x, ...)solution(x, ...) ## Default S3 method: solution(x, ...) sol_int(x, tol = NA, digits = NA) sol_num(x, tol = NA, digits = NA) sol_mc(x, y, sample = NULL, shuffle = order, none = NULL) sol_ans(x, ...) sol_tf(x, ...) sol_info(x, ...) sol_mc_ans(x, ...) sol_meta(x, ...) sol_mc_tf(x, ...)
x |
numeric solution or false MC solutions |
... |
further parameters |
tol |
numeric: tolerance for a numeric solution (default: |
digits |
integer: number of digits for rounding (default: |
y |
true MC solutions |
sample |
integer: sampling numbers for false and/or true solutions (default: |
shuffle |
logical or function: shuffling or ordering of solutions (default |
none |
character: if you do not wish to choose any of the false and/or true solutions offered (default: |
For numerical solutions you can set tol and/or digits.
If they are not set, they are automatically selected.
If tol is not set and length(x)>1 then the tolerance is chosen as min(diff(sort(x)))/2.
Otherwise, as max(0.001, 0.001*abs(x)). I
If tol is negative, tolerance is set to 10^tol, otherwise it is used as it is.
If digits is not set, ceiling(-log10(tolerance)) is used.
A solution object.
s <- sol_num(pi) sol_info(s) # set same tolerances, e.g. for a probability sol_num(0.1) sol_num(0.1, tol=0.001) sol_num(0.1, tol=-3) # MC: Which are prime numbers? prime <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) nonprime <- setdiff(2:30, prime) # choose five false and two correct solutions s <- sol_mc(nonprime, prime, sample=c(5,2), none="There are no prime numbers in the list") sol_ans(s) sol_tf(s) sol_info(s)s <- sol_num(pi) sol_info(s) # set same tolerances, e.g. for a probability sol_num(0.1) sol_num(0.1, tol=0.001) sol_num(0.1, tol=-3) # MC: Which are prime numbers? prime <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) nonprime <- setdiff(2:30, prime) # choose five false and two correct solutions s <- sol_mc(nonprime, prime, sample=c(5,2), none="There are no prime numbers in the list") sol_ans(s) sol_tf(s) sol_info(s)
Five data matrices with precomputed results from sumofsquares(n, 10, zerosum=TRUE, maxt=Inf) for
n=100, n=200, n=400, and n=800.
More generally, datasets can be accessed using sos(n, nmax), which
retrieves or computes results from sumofsquares(n, nmax, zerosum=TRUE, maxt=Inf).
The function sos() retrieves a dataset of the form sosN, where
N is the input parameter n. The function tries several sources
in order:
Load from the package exams.forge if available.
Load from the user data directory (see R_user_dir).
Download from a GitHub repository of exams.forge.
Compute the dataset on the fly using sumofsquares().
data(sos100) data(sos200) data(sos400) data(sos800) sos200 sos400 sos800 sos(n, nmax = 10, quiet = !interactive(), download = interactive())data(sos100) data(sos200) data(sos400) data(sos800) sos200 sos400 sos800 sos(n, nmax = 10, quiet = !interactive(), download = interactive())
n |
Integer, specifying the dataset index (e.g., |
nmax |
Integer, the maximum value passed to |
quiet |
Logical. If |
download |
Logical. If |
For each line of a matrix it holds and .
It contains all integer solutions up to k<=10. NA means that this entry is not used.
In steps 3 and 4, the dataset is cached in the user data directory (compressed
with xz). If the directory does not exist and the session is interactive,
the user is prompted for permission to create it; otherwise a temporary
directory is used. In non-interactive sessions, the directory is used only
if it already exists, otherwise tempdir() is used silently.
The computation step calls:
sumofsquares(n, nmax, maxt = Inf, zerosum = TRUE)
An R object named sosN, corresponding to the requested dataset.
If no solution exists for the given n, a matrix with a single row
filled with NA values is returned.
data(sos100) head(sos100) rowSums(sos100^2, na.rm=TRUE) rowSums(sos100, na.rm=TRUE) x100 <- sos(100) # part of the package rowSums(x100, na.rm=TRUE) # only zeros rowSums(x100^2, na.rm=TRUE) # only 100's x10 <- sos(10) # must be computed rowSums(x10, na.rm=TRUE) # only zeros rowSums(x10^2, na.rm=TRUE) # only 144'sdata(sos100) head(sos100) rowSums(sos100^2, na.rm=TRUE) rowSums(sos100, na.rm=TRUE) x100 <- sos(100) # part of the package rowSums(x100, na.rm=TRUE) # only zeros rowSums(x100^2, na.rm=TRUE) # only 100's x10 <- sos(10) # must be computed rowSums(x10, na.rm=TRUE) # only zeros rowSums(x10^2, na.rm=TRUE) # only 144's
Performs a spell check on RMarkdown files ignoring some exams keywords using spelling::spell_check_files().
spell( path, ignore = c("Meta", "information", "extype", "num", "mchoice", "schoice", "Solution", "exsolution", "extol", "exname", "Question", "align", "begin", "bigg", "cases", "cdot", "end", "frac", "infty", "int", "left", "left.", "leq", "mu", "qquad", "right", "sum", "text", "vert"), lang = Sys.getenv("LANG") ) rm_spell_check( path, ignore = c("Meta", "information", "extype", "num", "mchoice", "schoice", "Solution", "exsolution", "extol", "exname", "Question", "align", "begin", "bigg", "cases", "cdot", "end", "frac", "infty", "int", "left", "left.", "leq", "mu", "qquad", "right", "sum", "text", "vert"), lang = Sys.getenv("LANG") )spell( path, ignore = c("Meta", "information", "extype", "num", "mchoice", "schoice", "Solution", "exsolution", "extol", "exname", "Question", "align", "begin", "bigg", "cases", "cdot", "end", "frac", "infty", "int", "left", "left.", "leq", "mu", "qquad", "right", "sum", "text", "vert"), lang = Sys.getenv("LANG") ) rm_spell_check( path, ignore = c("Meta", "information", "extype", "num", "mchoice", "schoice", "Solution", "exsolution", "extol", "exname", "Question", "align", "begin", "bigg", "cases", "cdot", "end", "frac", "infty", "int", "left", "left.", "leq", "mu", "qquad", "right", "sum", "text", "vert"), lang = Sys.getenv("LANG") )
path |
path to file or to spell check |
ignore |
character vector with words which will be added to the hunspell::dictionary |
lang |
set |
A data frame with problematic words.
# none# none
Computes sqrt(n*p*(1-p)) for all combinations of n and p.
If the result has only digits after the decimal point, then n, p,
and sqrt(n*p*(1-p)) are returned in a data frame.
sqrtnp(n, p, digits = 2, tol = 10^(-digits - 4))sqrtnp(n, p, digits = 2, tol = 10^(-digits - 4))
n |
numeric: vector of observations numbers |
p |
numeric: vector of probabilities |
digits |
numeric: number of digits to check (default: |
tol |
numeric: tolerance (default: |
If abs(v-round(v, digits))<tol then a number v is considered as a number
with only digits after the decimal point.
A data frame with the columns n, p, np () and snp ().
n <- 30:250 p <- (10:40)/100 sqrtnp(n, p)n <- 30:250 p <- (10:40)/100 sqrtnp(n, p)
Decomposes an integer n into a sum of squared integers (; )
with .
If zerosum is true then it is ensured that with or .
The computation of the 's is limited by maxt seconds, which may result that not all possible
solutions are found. To reduce computing time, rbind's in the function are replaced by allocating
matrices with size rows to fill in the results.
Note that the following data sets are available:
sos100=sumofsquares(100, 10, zerosum=TRUE, maxt=Inf),
sos200=sumofsquares(200, 10, zerosum=TRUE, maxt=Inf),
sos400=sumofsquares(400, 10, zerosum=TRUE, maxt=Inf), and
sos800=sumofsquares(800, 10, zerosum=TRUE, maxt=Inf)
sumofsquares(n, nmax = 10, zerosum = FALSE, maxt = 30, size = 100000L) sum_sq(n, nmax = 10, zerosum = FALSE, maxt = 30, size = 100000L)sumofsquares(n, nmax = 10, zerosum = FALSE, maxt = 30, size = 100000L) sum_sq(n, nmax = 10, zerosum = FALSE, maxt = 30, size = 100000L)
n |
integer: number to decompose as sum of squares |
nmax |
integer: maximum number of squares in the sum |
zerosum |
logical: should the solution sum up to one (default: |
maxt |
numeric: maximal number of seconds the routine should run |
size |
numeric: length of additional matrix size (default: |
A matrix with nmax column with 's. NA means number has not been used.
If no solution exists for the given n, a matrix with a single row
filled with NA values is returned.
sos <- sumofsquares(100, 6) # 23 solutions head(sos) table(rowSums(!is.na(sos))) # one solution with one or two x_i # five solutions with four x_i # six solutions with five x_i # ten solutions with six x_i rowSums(sos^2, na.rm=TRUE) # all 100 sos <- sumofsquares(100, 6, zerosum=TRUE) head(sos) rowSums(sos^2, na.rm=TRUE) # all 100 rowSums(sos, na.rm=TRUE) # all 0sos <- sumofsquares(100, 6) # 23 solutions head(sos) table(rowSums(!is.na(sos))) # one solution with one or two x_i # five solutions with four x_i # six solutions with five x_i # ten solutions with six x_i rowSums(sos^2, na.rm=TRUE) # all 100 sos <- sumofsquares(100, 6, zerosum=TRUE) head(sos) rowSums(sos^2, na.rm=TRUE) # all 100 rowSums(sos, na.rm=TRUE) # all 0
Decomposes an integer n2 into a sum of squared integers ().
If n is not NA then it is ensured that .
Note if nobs<=10 then the following data sets are available:
sos100=sumofsquares(100, 10, zerosum=TRUE, maxt=Inf),
sos200=sumofsquares(200, 10, zerosum=TRUE, maxt=Inf),
sos400=sumofsquares(400, 10, zerosum=TRUE, maxt=Inf), and
sos800=sumofsquares(800, 10, zerosum=TRUE, maxt=Inf)
sumofsquares1(n2, nobs = 10, n = 0, x = runif(nobs), maxit = 1000)sumofsquares1(n2, nobs = 10, n = 0, x = runif(nobs), maxit = 1000)
n2 |
integer: number to decompose as sum of squares |
nobs |
integer: length of return values |
n |
integer: additional sum condition (default: |
x |
numeric: vector of |
maxit |
integer: maximal number of iterations |
A integer vector of length nobs.
sumofsquares1(100, 20) sumofsquares1(100, 20)sumofsquares1(100, 20) sumofsquares1(100, 20)
These functions check whether a normal approximation is appropriate for a given distribution.
They return TRUE if the approximation condition is met, and FALSE otherwise.
The threshold parameter c can be set directly or retrieved via getOption().
The functions apply the following rules:
t2norm: n > c with default c = 30.
binom2norm:
If type = "single" (default), the approximation is valid if size × prob × (1 - prob) > c.
If type = "double", the approximation requires both size × prob > c and size × (1 - prob) > c, with default c = 9.
clt2norm: n > c with default c = 30. Note that the existence of expectation and variance, required by the Central Limit Theorem, cannot be checked automatically.
t2norm(n, c = getOption("distribution.t2norm", 30)) binom2norm( size, prob, c = getOption("distribution.binom2norm", 9), type = c("single", "double") ) clt2norm(n, c = getOption("distribution.clt2norm", 30)) approx_binom2norm( size, prob, c = getOption("distribution.binom2norm", 9), type = c("single", "double") ) approx_clt2norm(n, c = getOption("distribution.clt2norm", 30)) approx_t2norm(n, c = getOption("distribution.t2norm", 30))t2norm(n, c = getOption("distribution.t2norm", 30)) binom2norm( size, prob, c = getOption("distribution.binom2norm", 9), type = c("single", "double") ) clt2norm(n, c = getOption("distribution.clt2norm", 30)) approx_binom2norm( size, prob, c = getOption("distribution.binom2norm", 9), type = c("single", "double") ) approx_clt2norm(n, c = getOption("distribution.clt2norm", 30)) approx_t2norm(n, c = getOption("distribution.t2norm", 30))
n |
integer: number of observations (for |
c |
numeric: threshold parameter for approximation (default via |
size |
integer: number of trials (for |
prob |
numeric: probability of success on each trial (for |
type |
character: approximation type, |
logical: TRUE if the approximation is valid, FALSE otherwise
# Check for 5 and 50 observations t2norm(n = c(5, 50)) binom2norm(size = c(5, 50), prob = 0.5) binom2norm(size = c(5, 50), prob = 0.5, type = "double")# Check for 5 and 50 observations t2norm(n = c(5, 50)) binom2norm(size = c(5, 50), prob = 0.5) binom2norm(size = c(5, 50), prob = 0.5, type = "double")
Creates a frequency table where all entries can be written as . It holds that and .
If the algorithm does not find a solution, then an error is thrown. Try to increase unit to 20, 50, 100 and so on.
Once a table is found, the table is normalized by dividing all entries by a number such that the entries are still integer.
Finally, a multiplicator of the form is randomly chosen, ensuring that the sum of the entries is less than, or equal to n.
table_data( nrow, ncol, unit = 10, maxit = 1000, n = 100, m2 = ceiling(log(n)/log(2)), m5 = ceiling(log(n)/log(5)) ) freq_table( nrow, ncol, unit = 10, maxit = 1000, n = 100, m2 = ceiling(log(n)/log(2)), m5 = ceiling(log(n)/log(5)) ) dtable( nrow, ncol, unit = 10, maxit = 1000, n = 100, m2 = ceiling(log(n)/log(2)), m5 = ceiling(log(n)/log(5)) )table_data( nrow, ncol, unit = 10, maxit = 1000, n = 100, m2 = ceiling(log(n)/log(2)), m5 = ceiling(log(n)/log(5)) ) freq_table( nrow, ncol, unit = 10, maxit = 1000, n = 100, m2 = ceiling(log(n)/log(2)), m5 = ceiling(log(n)/log(5)) ) dtable( nrow, ncol, unit = 10, maxit = 1000, n = 100, m2 = ceiling(log(n)/log(2)), m5 = ceiling(log(n)/log(5)) )
nrow |
integer: number of rows |
ncol |
integer: number of columns |
unit |
integer: reciprocal of smallest non-zero probability (default: |
maxit |
integer: maximal number of iterations (default: |
n |
integer: maximal sum of table entries (default: |
m2 |
integer: maximal power of two used on normalized the table (default: |
m5 |
integer: maximal power of five used on normalized the table (default: |
A frequency table where all entries can be written as .
tab22 <- table(2, 2) tab22 divisor_25(tab22) nom.cc(tab22) # Should be zero # table(3, 2) table(4, 2)tab22 <- table(2, 2) tab22 divisor_25(tab22) nom.cc(tab22) # Should be zero # table(3, 2) table(4, 2)
A text template where R code can be embedded.
template(tmpl, ...)template(tmpl, ...)
tmpl |
character: template |
... |
named parameter used in the template |
A character where the R code is replaced by its evaluation.
tmpl <- "`r a`+`r b`" template(tmpl, a=1, b=2)tmpl <- "`r a`+`r b`" template(tmpl, a=1, b=2)
Creates a list with the elements questions and solutions values. A value can be either an entry in a vector or
a row in a data frame. correct is a logical vector which contains TRUE if its value represents a correct answer
and FALSE if it represents a wrong answer. The values can be shuffled or ordered (default).
If shuffle is a integer of length 1 then one correct answer is chosen, and shuffle wrong answers are chosen.
If shuffle is a integer of length larger than 1, then shuffle[1] correct answers are chosen and shuffle[2] wrong answers are chosen.
If any shuffle entry is zero or negative, then no shuffling will be done.
If order is a function then it is expected that the function delivers an index for the reordering of the values.
Otherwise a shuffle for all values is applied.
The shuffling works in two steps:
Sample within the correct and wrong value according to shuffle
Apply shuffling (order=NULL) or ordering (default: order=order) of all selected answers
to_choice( df, correct, shuffle = c(NA_integer_, NA_integer_), orderfun = order, ... ) choice_list( df, correct, shuffle = c(NA_integer_, NA_integer_), orderfun = order, ... )to_choice( df, correct, shuffle = c(NA_integer_, NA_integer_), orderfun = order, ... ) choice_list( df, correct, shuffle = c(NA_integer_, NA_integer_), orderfun = order, ... )
df |
vector or data frame: values, in a data frame each row holds one value |
correct |
logical: answer is correct ( |
shuffle |
integer: the numbers of correct and wrong values to shuffle (default: |
orderfun |
function: ordering of the shuffled values (default: |
... |
further named parameters used in |
list with questions and solutions
answer <- runif(5) correct <- (1:5)==3 # Third answer is correct, the rest wrong sc <- to_choice(answer, correct) str(sc) # Answers are ordered by size sc$questions <- c(format(sc$questions, nsmall=2), "No answer is correct") # Additional answer sc$solutions <- c(sc$solutions, FALSE) # TRUE or FALSE? sc <- to_choice(answer, correct, shuffle=2) str(sc) # One correct answer and two wrong answers selectedanswer <- runif(5) correct <- (1:5)==3 # Third answer is correct, the rest wrong sc <- to_choice(answer, correct) str(sc) # Answers are ordered by size sc$questions <- c(format(sc$questions, nsmall=2), "No answer is correct") # Additional answer sc$solutions <- c(sc$solutions, FALSE) # TRUE or FALSE? sc <- to_choice(answer, correct, shuffle=2) str(sc) # One correct answer and two wrong answers selected
Convert a matrix to a formatted representation in HTML or LaTeX:
toHTML: Returns an HTML table of a matrix. Optionally displays it in a browser
via a temporary file using utils::browseURL().
toLatex: Returns a LaTeX table representation. Supports a subset of style options.
toHTMLorLatex: Chooses HTML or LaTeX output based on the presence of exams2pdf
in the call stack.
## S3 method for class 'html_matrix' toHTML(x, browser = FALSE, delay = 2, ...) ## S3 method for class 'html_matrix' toLatex(object, ...) toHTMLorLatex(x, ...)## S3 method for class 'html_matrix' toHTML(x, browser = FALSE, delay = 2, ...) ## S3 method for class 'html_matrix' toLatex(object, ...) toHTMLorLatex(x, ...)
x, object
|
An object of class |
browser |
Logical; if |
delay |
Numeric; seconds to wait before deleting temporary HTML files. A value of |
... |
Additional arguments passed to |
A character string containing the HTML or LaTeX representation of the matrix.
library("tools") m <- matrix(1:12, ncol = 4) hm <- html_matrix(m) # toHTML(hm, browser = TRUE) # opens into browser toHTML(hm) toLatex(hm) toHTMLorLatex(hm)library("tools") m <- matrix(1:12, ncol = 4) hm <- html_matrix(m) # toHTML(hm, browser = TRUE) # opens into browser toHTML(hm) toLatex(hm) toHTMLorLatex(hm)
Returns a LaTeX representation of the polynomial.
## S3 method for class 'polynomial' toLatex( object, digits = TRUE, decreasing = FALSE, variable = "x", simplify = TRUE, tol = 1e-09, ... )## S3 method for class 'polynomial' toLatex( object, digits = TRUE, decreasing = FALSE, variable = "x", simplify = TRUE, tol = 1e-09, ... )
object |
polynomial |
digits |
numeric or logical: how to convert to text (default: |
decreasing |
logical: order of the terms by increasing or decreasing powers (default: |
variable |
character: name of variable used (default: |
simplify |
logical: should the polynomial representation be simplified (default: |
tol |
numeric: tolerance (default:
|
... |
unused parameters |
A character
p <- polynomial(c(-1,0,2)/3) toLatex(p, 4) toLatex(p, FALSE) toLatex(p, TRUE) toLatex(p, variable="z") toLatex(p, decreasing=TRUE) p <- polynomial(c(0,1,2)/3) toLatex(p) toLatex(p, tol=-1)p <- polynomial(c(-1,0,2)/3) toLatex(p, 4) toLatex(p, FALSE) toLatex(p, TRUE) toLatex(p, variable="z") toLatex(p, decreasing=TRUE) p <- polynomial(c(0,1,2)/3) toLatex(p) toLatex(p, tol=-1)
Adds a text tooltip to the HTML matrix.
tooltip(x, tooltip = NULL) add_tooltip(x, tooltip = NULL)tooltip(x, tooltip = NULL) add_tooltip(x, tooltip = NULL)
x |
an html_matrix object |
tooltip |
character: text to show (default: |
An html_matrix object
library("magrittr") library("tools") m <- matrix(1:12, ncol=4) hm <- html_matrix_sk(m, title='', fmt=rep("%f", ncol(m))) %>% add_tooltip(sprintf("Table has %0.f rows and %0.f columns", nrow(.), ncol(.))) if (interactive()) html <- toHTML(hm, browser=TRUE)library("magrittr") library("tools") m <- matrix(1:12, ncol=4) hm <- html_matrix_sk(m, title='', fmt=rep("%f", ncol(m))) %>% add_tooltip(sprintf("Table has %0.f rows and %0.f columns", nrow(.), ncol(.))) if (interactive()) html <- toHTML(hm, browser=TRUE)
Conversion to R Markdown.
toRMarkdown(txt)toRMarkdown(txt)
txt |
character: vector with lines of Moodle Markdown |
Lines with RMarkdown
txt <- c("[image]\n", "Ein Paar hat 8 gute Bekannte, von denen die beiden 5 zum Essen einladen möchten.", "Wie viele verschiedene Reihenfolgen des Eintreffens der eingeladenen 5 Gäste gibt es?\n", ": 56", "; 120", "; 336", "; 2002", "; 6720", "; 32768", "; 40320", "; Keine Antwort ist richtig") toRMarkdown(txt)txt <- c("[image]\n", "Ein Paar hat 8 gute Bekannte, von denen die beiden 5 zum Essen einladen möchten.", "Wie viele verschiedene Reihenfolgen des Eintreffens der eingeladenen 5 Gäste gibt es?\n", ": 56", "; 120", "; 336", "; 2002", "; 6720", "; 32768", "; 40320", "; Keine Antwort ist richtig") toRMarkdown(txt)
Creates a text representation for a polynomial, in the following scenarios:
if digits is TRUE then as.character(.) is used
if digits is FALSE then ./. is used
if digits is numeric then as.character(round(., digits)) is used
## S3 method for class 'polynomial' toString( x, digits = TRUE, decreasing = FALSE, variable = "x", simplify = TRUE, tol = 1e-09, ... )## S3 method for class 'polynomial' toString( x, digits = TRUE, decreasing = FALSE, variable = "x", simplify = TRUE, tol = 1e-09, ... )
x |
polynomial: vector of coefficients (first is intercept) |
digits |
numeric or logical: how to convert to text (default: |
decreasing |
logical: order of the terms by increasing or decreasing powers (default: |
variable |
character: name of the variable used (default: |
simplify |
logical: should the polynomial representation be simplified (default: |
tol |
numeric: tolerance (default:
|
... |
unused parameters |
A character
p <- polynomial(c(-1,0,2)/3) toString(p, 4) toString(p, FALSE) toString(p, TRUE) toString(p, variable="z") toString(p, decreasing=TRUE) p <- polynomial(c(0,1,2)/3) toString(p) toString(p, tol=-1)p <- polynomial(c(-1,0,2)/3) toString(p, 4) toString(p, FALSE) toString(p, TRUE) toString(p, variable="z") toString(p, decreasing=TRUE) p <- polynomial(c(0,1,2)/3) toString(p) toString(p, tol=-1)
Transforms x if cond is TRUE by if p==0 and .
Otherwise the transformation can be either applied to each element of x, or to all elements of x.
transformif(x, cond, a = -abs(min(x)), b = 1, p = 1)transformif(x, cond, a = -abs(min(x)), b = 1, p = 1)
x |
vector: values |
cond |
logical: condition if transformation should be applied |
a |
numeric: shift (default: |
b |
numeric: scale (default: |
p |
numeric: power (default: |
A transformed vector
x <- rnorm(5) transformif(x, min(x)<0) # all transformed elements > 0 transformif(x, x<0) # only negative elements are transformedx <- rnorm(5) transformif(x, min(x)<0) # all transformed elements > 0 transformif(x, x<0) # only negative elements are transformed
Creates an univariate time series based on a linear or an exponential trend, an additive or multiplicative seasonal adjustment and with white noise.
ts_data( end, trend = TRUE, trend.coeff = c(1, 1), season = TRUE, season.coeff = NULL, error = TRUE, error.coeff = NULL, digits = NA ) dts( end, trend = TRUE, trend.coeff = c(1, 1), season = TRUE, season.coeff = NULL, error = TRUE, error.coeff = NULL, digits = NA )ts_data( end, trend = TRUE, trend.coeff = c(1, 1), season = TRUE, season.coeff = NULL, error = TRUE, error.coeff = NULL, digits = NA ) dts( end, trend = TRUE, trend.coeff = c(1, 1), season = TRUE, season.coeff = NULL, error = TRUE, error.coeff = NULL, digits = NA )
end |
integer: length of time series |
trend |
logical: if |
trend.coeff |
numeric: coefficients for a linear model (default: |
season |
logical: if |
season.coeff |
numeric: coefficients for the adjustment (default: |
error |
logical: if |
error.coeff |
numeric: standard deviation(s) for white noise error (default: |
digits |
integer: number of digits to round the time series (default: |
A ts_data object with the following list of elements:
t the time points
s the season for the time points
xt the time series values
# Time series from linear trend ts <- ts_data(12, trend.coeff= c(sample(0:10, 1), sample(1+(1:10)/20, 1))) ts # Time series from exponential trend ts <- ts_data(12, trend.coeff= c(sample(0:10, 1), sample(1+(1:10)/20, 1)), trend=FALSE) ts # Time series from linear trend and additive seasonal adjustment (quartely data) ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1)), season.coeff=sample((-20:20)/20, 4)) ts # Time series from linear trend and additive seasonal adjustment (half-yearly data) ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1)), season.coeff=sample((-20:20)/20, 2)) ts # Time series from linear trend and mutliplicative seasonal adjustment (quartely data) ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1)), season.coeff=sample((-20:20)/20, 4), season=FALSE) ts# Time series from linear trend ts <- ts_data(12, trend.coeff= c(sample(0:10, 1), sample(1+(1:10)/20, 1))) ts # Time series from exponential trend ts <- ts_data(12, trend.coeff= c(sample(0:10, 1), sample(1+(1:10)/20, 1)), trend=FALSE) ts # Time series from linear trend and additive seasonal adjustment (quartely data) ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1)), season.coeff=sample((-20:20)/20, 4)) ts # Time series from linear trend and additive seasonal adjustment (half-yearly data) ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1)), season.coeff=sample((-20:20)/20, 2)) ts # Time series from linear trend and mutliplicative seasonal adjustment (quartely data) ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1)), season.coeff=sample((-20:20)/20, 4), season=FALSE) ts
Computes the moving average for a ts_data object.
ts_moving_average(ts, order) ts_ma(ts, order)ts_moving_average(ts, order) ts_ma(ts, order)
ts |
a |
order |
integer: order of the moving average |
Returns an extended ts_data object with list elements:
filter the filter used
moving.average the computed moving average
# trend from a quadratic model ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1), 0.5)) ts_moving_average(ts, 3)# trend from a quadratic model ts <- ts_data(12, trend.coeff=c(sample(0:10, 1), sample(1+(1:10)/20, 1), 0.5)) ts_moving_average(ts, 3)
Estimate a trend and season model from a ts_data object.
ts_trend_season(ts, trend = NULL, season = NULL) ts_ts(ts, trend = NULL, season = NULL)ts_trend_season(ts, trend = NULL, season = NULL) ts_ts(ts, trend = NULL, season = NULL)
ts |
|
trend |
numeric or logical: if |
season |
numeric or logical |
Returns an extended ts_data object with the following list of elements:
t the time points
s the season for the time points
xt the time series values
trend the fitted trend values
trend.coeff the trend coefficients
trend.linear the trend type, if NA then it is unknown
season the fitted season values
season.t the fitted season values for the time series
trend.season the fitted values for trend and season
trend.linear the trend type, if NA then it is unknown
var the variance of the residuals
r.square the of the final model
ts <- ts_data(12, trend.coeff= c(sample(0:10, 1), sample(1+(1:10)/20, 1))) ts_trend_season(ts)ts <- ts_data(12, trend.coeff= c(sample(0:10, 1), sample(1+(1:10)/20, 1))) ts_trend_season(ts)
Creates data for a t-test, for one mean, based on the test's properties.
ttest_data( size = (3:20)^2, mean = -5:5, sd = seq(0.1, 1, by = 0.1), reject = NA, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), z = seq(-4.49, 4.49, by = 0.01), use.sigma = TRUE ) dt1( size = (3:20)^2, mean = -5:5, sd = seq(0.1, 1, by = 0.1), reject = NA, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), z = seq(-4.49, 4.49, by = 0.01), use.sigma = TRUE )ttest_data( size = (3:20)^2, mean = -5:5, sd = seq(0.1, 1, by = 0.1), reject = NA, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), z = seq(-4.49, 4.49, by = 0.01), use.sigma = TRUE ) dt1( size = (3:20)^2, mean = -5:5, sd = seq(0.1, 1, by = 0.1), reject = NA, alternative = c("two.sided", "less", "greater"), alpha = c(0.01, 0.05, 0.1), z = seq(-4.49, 4.49, by = 0.01), use.sigma = TRUE )
size |
numeric: vector of possible sample sizes (default |
mean |
numeric: vector of possible means (default |
sd |
numeric: vector of possible standard deviations (default |
reject |
logical: should |
alternative |
character: a character string specifying the alternative hypothesis, must be one of |
alpha |
numeric: vector of significance levels (default |
z |
numeric: vector of possible |
use.sigma |
logical: should the standard deviation of the population (default) or the sample be used? |
A list with the components:
mu0 hypothetical mean
sigma standard deviation in the population
sd vector of possible standard deviations in the sample
xbar mean in the sample
n sample size
alpha significance level
alternative specifying the alternative hypothesis (either two.sided, greater or less)
altsd alternative values usable for sd (if use.sigma==TRUE) or sigma (if use.sigma==FALSE)
ttest_data()ttest_data()
Computes all results for a t-test. Note that the results may differ from stats::t.test(), see the "Details".
Either named parameters can be given, or a list with the parameters.
You must provide either x or mean, sd and n. If x is given then any values
given for mean, sd and n will be overwritten. Also either sd or sigma or both must be given.
x sample (default: numeric(0))
mean sample mean (default: mean(x))
n sample size (default: length(x))
sd sample standard deviation (default: sd(x))
sigma population standard deviation (default: NA = unknown)
mu0 true value of the mean (default: 0)
alternative a string specifying the alternative hypothesis (default: "two.sided"), otherwise "greater" or "less" can be used
alpha significance level (default: 0.05)
norm is the population normal distributed? (default: FALSE)
n.clt when the central limit theorem holds (default: getOption("n.clt", 30))
t2norm does the approximation hold? (default: NA= uset2norm' function)
ttest_num(..., arglist = NULL)ttest_num(..., arglist = NULL)
... |
named input parameters |
arglist |
list: named input parameters, if given |
The results of ttest_num may differ from stats::t.test(). ttest_num is designed to return results
when you compute a t-test by hand. For example, for computing the test statistic the approximation
is used if . The p.value is computed from the cumulative distribution function of the normal or
the t distribution.
A list with the input parameters and the following:
Xbar distribution of the random sampling function , only available if sigma given
Statistic distribution of the test statistics
statistic test value
critical critical value(s)
criticalx critical value(s) in x range
acceptance0 acceptance interval for H0
acceptance0x acceptance interval for H0 in x range
accept1 is H1 accepted?
p.value p value for test
x <- runif(100) ttest_num(x=x) ttest_num(mean=mean(x), sd=sd(x), n=length(x)) ret <- ttest_num(x=x) ret$alternative <- "less" ttest_num(arglist=ret)x <- runif(100) ttest_num(x=x) ttest_num(mean=mean(x), sd=sd(x), n=length(x)) ret <- ttest_num(x=x) ret$alternative <- "less" ttest_num(arglist=ret)
ttests runs a variety of modifications to the input parameters of ttest, in order to generate all possible t-tests.
See under "Details" the detailed parameter values which are used. Note that not giving the parameter hyperloop will
results in approx. 5000 t-tests generated.
Returned will be only the different t-tests with the first element being ttest. If only a specific element of a ttest
is of interest then just give the name of the element in elem and then all ttests will be returned where elem is different.
ttests(ttest, elem = NULL, hyperloop = NULL)ttests(ttest, elem = NULL, hyperloop = NULL)
ttest |
ttest: the base result from a valid t-test generated by |
elem |
character: element to extract (default: |
hyperloop |
named list: parameter values to run over (default: see above) |
The default hyperloop is:
list(n = c(1, ttest$n, ttest$n+1),
mu0 = c(ttest$mu0, ttest$mean),
mean = c(ttest$mu0, ttest$mean),
sigma = c(ttest$sigma, ttest$sd, sqrt(ttest$sigma), sqrt(ttest$sd)),
sd = c(ttest$sigma, ttest$sd, sqrt(ttest$sigma), sqrt(ttest$sd)),
norm = c(TRUE, FALSE),
alpha = unique(c(ttest$alpha, 0.01, 0.05, 0.1)),
alternative = c("two.sided", "greater", "less")
)
A list of ttest objects is returned
basetest <- ttest_num(mean=0.5, sd=1.25, n=50, sigma=1) # vary the number of observations hyperloop <- list(n=c(1, basetest$n, basetest$n^2)) # return all different t-tests tts <- ttests(basetest, hyperloop=hyperloop) # return all different random sampling functions ttests(basetest, "Xbar", hyperloop)basetest <- ttest_num(mean=0.5, sd=1.25, n=50, sigma=1) # vary the number of observations hyperloop <- list(n=c(1, basetest$n, basetest$n^2)) # return all different t-tests tts <- ttests(basetest, hyperloop=hyperloop) # return all different random sampling functions ttests(basetest, "Xbar", hyperloop)
Deletes all elements from a hyperloop object that are identical.
Since the result in each run can be a list itself, only specific list elements can be used for comparison.
unique_elem(x, elem = NULL)unique_elem(x, elem = NULL)
x |
a |
elem |
character: list elements which are used to check if |
A reduced hyperloop object
x <- rnorm(100) # 6 results: 3 different mu's, 2 var.equals hl <- hyperloop(t.test, x=x, mu=list(-1, 0, 1), var.equal=list(TRUE, FALSE)) # reduction to 3 elements since var.equal does not play any role length(unique_elem(hl)) # reduction to 1 element since the mean of x always the same length(unique_elem(hl, "estimate"))x <- rnorm(100) # 6 results: 3 different mu's, 2 var.equals hl <- hyperloop(t.test, x=x, mu=list(-1, 0, 1), var.equal=list(TRUE, FALSE)) # reduction to 3 elements since var.equal does not play any role length(unique_elem(hl)) # reduction to 1 element since the mean of x always the same length(unique_elem(hl, "estimate"))
Checks if x has a unique maximum. The largest and the second largest value must have at least a distance of tol.
unique_max(x, tol = 0.001)unique_max(x, tol = 0.001)
x |
numeric: values to check |
tol |
numeric: minimum distance between the largest and the second largest value (default: |
Logical
x <-runif(100) unique_max(x) unique_max(x, tol=0.1)x <-runif(100) unique_max(x) unique_max(x, tol=0.1)
Converts a vector to a horizontal or vertical matrix and sets row- or colnames.
If rownames or colnames are given, then existing row names or column names are overwritten.
vec2mat(x, colnames = NULL, rownames = NULL, horizontal = TRUE) to_mat(x, colnames = NULL, rownames = NULL, horizontal = TRUE)vec2mat(x, colnames = NULL, rownames = NULL, horizontal = TRUE) to_mat(x, colnames = NULL, rownames = NULL, horizontal = TRUE)
x |
vector |
colnames |
character: vector of new column names (default: |
rownames |
character: vector of new row names (default: |
horizontal |
logical: horizontal or vertical matrix (default: |
A matrix
x <- runif(5) vec2mat(x) vec2mat(x, horizontal=FALSE)x <- runif(5) vec2mat(x) vec2mat(x, horizontal=FALSE)
This function applies alternating background colors (zebra striping) to an
html_matrix object, either by rows or by columns.
zebra(x, col = c("#FFFFFF", "#CCCCCC"), byrow = TRUE)zebra(x, col = c("#FFFFFF", "#CCCCCC"), byrow = TRUE)
x |
An |
col |
A character vector of colors to use for striping. Defaults to
|
byrow |
Logical; if |
An html_matrix object with updated background colors.
library("magrittr") m <- matrix(13:24, ncol = 4) hm <- html_matrix(m) %>% zebra() html <- toHTML(hm) if (interactive()) toHTML(hm, browser = TRUE) # opens in browserlibrary("magrittr") m <- matrix(13:24, ncol = 4) hm <- html_matrix(m) %>% zebra() html <- toHTML(hm) if (interactive()) toHTML(hm, browser = TRUE) # opens in browser