Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

32 lignes
1.4KB

  1. NUMERIC_TYPES <- c("double", "integer")
  2. lsq <- function(X, y) {
  3. # TODO
  4. stopifnot("X may not be empty" = length(X) > 0)
  5. stopifnot("y may not be empty" = length(y) > 0)
  6. stopifnot("X must be numeric" = typeof(X) %in% NUMERIC_TYPES)
  7. stopifnot("y must be numeric" = typeof(y) %in% NUMERIC_TYPES)
  8. stopifnot("X must be matrix" = is.matrix(X))
  9. stopifnot("y must be vector or matrix with one column" = is.vector(y) || (is.matrix(y) && ncol(y) == 1))
  10. stopifnot("dimensions of X and y do not fit" = (is.vector(y) && nrow(X) == length(y)) || (is.matrix(y) && nrow(y) == nrow(X)))
  11. stopifnot("y may not contain NA" = all(!is.na(y)))
  12. stopifnot("X may not contain NA" = all(!is.na(X)))
  13. A <- t(X) %*% X
  14. stopifnot("det(t(X) %*% X) must not be zero" = det(A) != 0)
  15. solve(A, t(X) %*% y)
  16. }
  17. lsq(matrix(1:6, nrow=3), 1:3)
  18. lsq(matrix(runif(6), nrow=3), matrix(runif(3), ncol=1))
  19. lsq(matrix(letters[1:6] , nrow=2), 1:3)
  20. lsq(matrix(1:6, nrow=3), list(1,2,3))
  21. lsq(1:6, 1:3)
  22. lsq(matrix(1:6, nrow=3), array(1:3, dim=c(1,1,3)))
  23. lsq(matrix(1:6, nrow=3), 1:4)
  24. lsq(matrix(1:6, nrow=3), matrix(1:3, nrow=1))
  25. lsq(matrix(1:6, nrow=3), matrix(1:6, nrow=3))
  26. lsq(matrix(double(0), nrow=0, ncol=0), matrix(double(0), nrow=0, ncol=0))
  27. lsq(matrix(1:6, nrow=3), c(1,NA,3))
  28. lsq(matrix(c(1:5, NA), nrow=3), 1:3)
  29. lsq(matrix(c(1,1,2,1,1,2), nrow=3), 1:3)