cor_data {exams.forge} | R Documentation |
Correlation and Data Generation
Description
Generates a data set based on x
and y
for a given target correlation r
according to stats::cor()
.
The algorithm modifies the order of the y
's, therefore is guaranteed that the (marginal) distribution of x
and y
will not be modified. Please note that it is not guaranteed that the final correlation will be the desired correlation;
the algorithm interactively modifies the order. If you are unsatisfied with the result, it might help to increase maxit
.
Usage
cor_data(
x,
y,
r,
method = c("pearson", "kendall", "spearman"),
...,
maxit = 1000
)
dcorr(x, y, r, method = c("pearson", "kendall", "spearman"), ..., maxit = 1000)
Arguments
x |
numeric: given |
y |
numeric: given |
r |
numeric: desired correlation |
method |
character: indicates which correlation coefficient is to be computed (default: '"pearson") |
... |
further parameters given to |
maxit |
numeric: maximal number of iterations (default: |
Value
A matrix with two columns and an attribute interim
for intermediate values as matrix.
The rows of the matrix contain:
if
method=="pearson"
:,
,
,
,
,
, and
.
if
method=="kendall"
:-
: The original x values.
-
: The original y values.
-
: The number of concordant pairs.
-
: The number of discordant pairs.
-
if
method=="spearman"
:,
,
(concordant pairs), and
(disconcordant pairs). In a final step a vector with the row
sums
is appended as further column.
Examples
x <- runif(6)
y <- runif(6)
xy <- cor_data(x, y, r=0.6)
cbind(x, y, xy)