Wie berechnet man diese Summe in R?

Nov 26 2020

Wenn wir einen Datensatz haben, wo $x_{ij}$der Matrix der Größe 4 mal 3 ist i = 1,2,3,4 und j = 1,2,3. y_i ist ein Vektor und z_ {ij} ist auch eine 4 x 3-Matrix.

  a1=c(1,0,1, 2)
  a2=c(2,2,1, 2)
  a3=c(1,3,2,3)
  a4=c(-1,0,-1,-1)
  x <- cbind(a1, a2, a3)
  y <- c(1,2,3,2)
  z<-cbind(a2, a3,a4)

Antworten

1 BenjaminChristoffersen Nov 26 2020 at 21:26

Ich denke, das wird reichen:

z1 <- z[1, ]
vapply(
  seq_along(z1), function(j) 
    drop((y - x[, -j] %*% z1[-j])) %*% x[, j], 
  numeric(1))
#R> [1]  10  15 -15

und eine for-Schleifenversion ist:

out <- numeric(3)
for(j in 1:3)
  for(i in 1:4){
    f1 <- y[i]
    for(k in setdiff(1:3, j))
      f1 <- f1 - x[i, k] * z[1, k]
    out[j] <- out[j] + f1 * x[i, j]
  }
out
#R> [1]  10  15 -15