0

I have a multivariate data with 3 different groups, where d=2 and each group have 10 data point (n=10). I am trying to find enter image description here

for each group so the final output would be 3 vector of size 2. However, when I am trying to do this (code below),

## data ##
d <- 2
n<-c(10,10,10) 
mu1=rep(1,d)
mu2=rep(1,d)
mu3=rep(1,d)
sigma <- matrix(c(1.0, 0,
                  0, 1.0), nrow = 2)
groups<-rep(1:3, n)
x1=mvrnorm(n[1], mu1,sigma)
x2=mvrnorm(n[2], mu2,sigma)
x3=mvrnorm(n[3], mu3,sigma)
x=rbind(x1,x2,x3)

K <- function(x){
  I=length(unique(groups))
  Kij <- matrix(,n[i],d)
  for (i in 1:I){
    for (j in 1:n[i]){
    Kij<- x[j,]-x[-j,]
  return (as.matrix(sum(Kij[groups==i],n[i],d)))
    }
  }
}

I keep getting 1 number as an output instead of a vector of 2 outputs for each group. Any suggestion?

Thank you in advance

H A
  • 81
  • 1
  • 1
  • 11
  • 2
    Please show a small reproducible example. What are `groups`. May be you need `m1 <- outer(seq_len(I), seq_len(p), FUN = function(i, j) sum(X[i, j]))` and then use `sum(m1[col(m1) != row(m1)])` – akrun Feb 13 '20 at 17:55
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 13 '20 at 17:57
  • I edited my post to add the data I am using. – H A Feb 14 '20 at 00:57
  • What library are you using here? – Georgery Feb 14 '20 at 10:52
  • library(mvmesh) library(mvtnorm) library(MASS) – H A Feb 14 '20 at 18:00

0 Answers0