Weder mit dem Hinweis von Edi zu Anfang ("use=" Argument)...
Hier eine Erklärung :
Funktionen haben einen Namen - "cor" in deinem Fall.
Funktionen kann man Eingabewerte übergeben, mit denen sie etwas anstellen. In deinem Fall ist das "dat2", ein Objekt der Klasse "data. frame".
Eingabewerte werden auch "Argumente" genannt und steuern oft auch details wie eine Funktion etwas berechnet. Meist sind Standardwerte vergeben.
Die Funktion cor hat auch einen Rückgabewert, hier die berechnete Korrelationsmatrix.
R hat eine sehr hochwertige Dokumentation/Hilfe die beschreibt wie eine Funktion zu Nutzen ist, was ihre Eingabe und Ausgabewerte sind.
Man erreicht diese Hilfe indem man ein "?" vor den Funktionsnamen setzt:
Die
Hilfe ist in verschiedene Abschnitte gegliedert:
Code: Alles auswählen
cor {stats}
Correlation, Variance and Covariance (Matrices)
Der Name der Funktion, das Paket in welchem sie enthalten ist ("stats" in dem Falle), sowie der Titel der Funktion.
Code: Alles auswählen
Description
var, cov and cor compute the variance of x and the covariance or correlation of x and y if these are vectors. If x and y are matrices then the covariances (or correlations) between the columns of x and the columns of y are computed.
cov2cor scales a covariance matrix into the corresponding correlation matrix efficiently.
Eine Beschreibung der Funktion.
Code: Alles auswählen
Usage
var(x, y = NULL, na.rm = FALSE, use)
cov(x, y = NULL, use = "everything",
method = c("pearson", "kendall", "spearman"))
cor(x, y = NULL, use = "everything",
method = c("pearson", "kendall", "spearman"))
cov2cor(V)
Die Hilfe in diesem Fall beschreibt sogar 4 Funktionen (var, cov, cor und cov2cor).
Hier sieht man die Funktion, samt aller Argumente und Standard-Eingabewerte. Zum Beispiel ist der Stanardwert für das Argument "na. rm" der Funktion var FALSE.
Code: Alles auswählen
Arguments
x
a numeric vector, matrix or data frame.
y
NULL (default) or a vector, matrix or data frame with compatible dimensions to x. The default is equivalent to y = x (but more efficient).
na.rm
logical. Should missing values be removed?
use
an optional character string giving a method for computing covariances in the presence of missing values. This must be (an abbreviation of) one of the strings "everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".
method
a character string indicating which correlation coefficient (or covariance) is to be computed. One of "pearson" (default), "kendall", or "spearman": can be abbreviated.
V
symmetric numeric matrix, usually positive definite such as a covariance matrix.
Es folgt eine Beschreibung der Argumente. "use" hat also etwas damit zu tun wie fehlende Werte behandelt werden.
Code: Alles auswählen
Details
For cov and cor one must either give a matrix or data frame for x or give both x and y.
The inputs must be numeric (as determined by is.numeric: logical values are also allowed for historical compatibility): the "kendall" and "spearman" methods make sense for ordered inputs but xtfrm can be used to find a suitable prior transformation to numbers.
var is just another interface to cov, where na.rm is used to determine the default for use when that is unspecified. If na.rm is TRUE then the complete observations (rows) are used (use = "na.or.complete") to compute the variance. Otherwise, by default use = "everything".
If use is "everything", NAs will propagate conceptually, i.e., a resulting value will be NA whenever one of its contributing observations is NA.
If use is "all.obs", then the presence of missing observations will produce an error. If use is "complete.obs" then missing values are handled by casewise deletion (and if there are no complete cases, that gives an error).
"na.or.complete" is the same unless there are no complete cases, that gives NA. Finally, if use has the value "pairwise.complete.obs" then the correlation or covariance between each pair of variables is computed using all complete pairs of observations on those variables. This can result in covariance or correlation matrices which are not positive semi-definite, as well as NA entries if there are no complete pairs for that pair of variables. For cov and var, "pairwise.complete.obs" only works with the "pearson" method. Note that (the equivalent of) var(double(0), use = *) gives NA for use = "everything" and "na.or.complete", and gives an error in the other cases.
The denominator n - 1 is used which gives an unbiased estimator of the (co)variance for i.i.d. observations. These functions return NA when there is only one observation (whereas S-PLUS has been returning NaN), and fail if x has length zero.
For cor(), if method is "kendall" or "spearman", Kendall's tau or Spearman's rho statistic is used to estimate a rank-based measure of association. These are more robust and have been recommended if the data do not necessarily come from a bivariate normal distribution.
For cov(), a non-Pearson method is unusual but available for the sake of completeness. Note that "spearman" basically computes cor(R(x), R(y)) (or cov(., .)) where R(u) := rank(u, na.last = "keep"). In the case of missing values, the ranks are calculated depending on the value of use, either based on complete observations, or based on pairwise completeness with reranking for each pair.
When there are ties, Kendall's tau_b is computed, as proposed by Kendall (1945).
Scaling a covariance matrix into a correlation one can be achieved in many ways, mathematically most appealing by multiplication with a diagonal matrix from left and right, or more efficiently by using sweep(.., FUN = "/") twice. The cov2cor function is even a bit more efficient, and provided mostly for didactical reasons.
Es folgen weitere Details. Hier steht u. A. was die unterschiedlichen Möglichkeiten von "use" (everything", "all.obs", "complete.obs", "na.or.complete", or "pairwise.complete.obs".) bezwecken.
Code: Alles auswählen
Value
For r <- cor(*, use = "all.obs"), it is now guaranteed that all(abs(r) <= 1).
Beschreibt den Rückgabewert.
Code: Alles auswählen
Note
Some people have noted that the code for Kendall's tau is slow for very large datasets (many more than 1000 cases). It rarely makes sense to do such a computation, but see function cor.fk in package pcaPP.
Gibt Hinweise zur Implementierung.
Code: Alles auswählen
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Kendall, M. G. (1938) A new measure of rank correlation, Biometrika 30, 81–93. https://dx.doi.org/10.1093/biomet/30.1-2.81
Kendall, M. G. (1945) The treatment of ties in rank problems. Biometrika 33 239–251. https://dx.doi.org/10.1093/biomet/33.3.239
Referenzen zu den Berechnungen.
Code: Alles auswählen
See Also
cor.test for confidence intervals (and tests).
cov.wt for weighted covariance computation.
sd for standard deviation (vectors).
Verweise zu verwandten Funktionen.
Code: Alles auswählen
Examples
var(1:10) # 9.166667
var(1:5, 1:5) # 2.5
## Two simple vectors
cor(1:10, 2:11) # == 1
## Correlation Matrix of Multivariate sample:
(Cl <- cor(longley))
## Graphical Correlation Matrix:
symnum(Cl) # highly correlated
## Spearman's rho and Kendall's tau
symnum(clS <- cor(longley, method = "spearman"))
symnum(clK <- cor(longley, method = "kendall"))
## How much do they differ?
i <- lower.tri(Cl)
cor(cbind(P = Cl[i], S = clS[i], K = clK[i]))
## cov2cor() scales a covariance matrix by its diagonal
## to become the correlation matrix.
cov2cor # see the function definition {and learn ..}
stopifnot(all.equal(Cl, cov2cor(cov(longley))),
all.equal(cor(longley, method = "kendall"),
cov2cor(cov(longley, method = "kendall"))))
##--- Missing value treatment:
C1 <- cov(swiss)
range(eigen(C1, only.values = TRUE)$values) # 6.19 1921
## swM := "swiss" with 3 "missing"s :
swM <- swiss
colnames(swM) <- abbreviate(colnames(swiss), min=6)
swM[1,2] <- swM[7,3] <- swM[25,5] <- NA # create 3 "missing"
## Consider all 5 "use" cases :
(C. <- cov(swM)) # use="everything" quite a few NA's in cov.matrix
try(cov(swM, use = "all")) # Error: missing obs...
C2 <- cov(swM, use = "complete")
stopifnot(identical(C2, cov(swM, use = "na.or.complete")))
range(eigen(C2, only.values = TRUE)$values) # 6.46 1930
C3 <- cov(swM, use = "pairwise")
range(eigen(C3, only.values = TRUE)$values) # 6.19 1938
## Kendall's tau doesn't change much:
symnum(Rc <- cor(swM, method = "kendall", use = "complete"))
symnum(Rp <- cor(swM, method = "kendall", use = "pairwise"))
symnum(R. <- cor(swiss, method = "kendall"))
## "pairwise" is closer componentwise,
summary(abs(c(1 - Rp/R.)))
summary(abs(c(1 - Rc/R.)))
## but "complete" is closer in Eigen space:
EV <- function(m) eigen(m, only.values=TRUE)$values
summary(abs(1 - EV(Rp)/EV(R.)) / abs(1 - EV(Rc)/EV(R.)))
Mit der wichtigste / hilfreichste Teil der Hilfe: Beispielberechnungen! (siehe auch Thema "reproduzierbares Beispiel").
Man kann die Beispiele einer Funktion auch mit
aufrufen.
Ich hoffe du (und alle anderen die hier mitlesen) können nun etwas mit meinem Hinweis anfangen und es hilft dazu die Hilfe richtig zu lesen (hab ich den Studententen immer inder ersten Vorlesung beigebracht) - auch wenn er (leider) nichts zu deinem Problem beiträgt.