Reframing the question, I would like to implement the following equation:
WhereDisanarrayoforderixkxl,lambdaisamatrixkxjandpk={1,2,3,4,5,6}.
ForthisIneedsomefunctionthatoperateswiththree-dimensionalarrayinsteadofnestedloops.
Belowthepostaddedbytheauthor(probablybecausewedonothaveenoughdatatobesure):
library(KRLS)matriz=matrix(c(rnorm(160)),ncol=8)matriz=(matriz-mean(matriz))/sd(matriz)
Separatingintoparts
particao_1=matrix(rep(0,length(matriz[1,])*length(matriz[,1])),ncol=length(matriz[1,]))particao_2=matrix(rep(0,length(matriz[1,])*length(matriz[,1])),ncol=length(matriz[1,]))particao_3=matrix(rep(0,length(matriz[1,])*length(matriz[,1])),ncol=length(matriz[1,]))
Selectionofrandomformofparsingbykmeans
inicio=kmeans(matriz,3)i=1;for(iin1:length(inicio$cluster)){if(inicio$cluster[i]==1)particao_1[i,]=matriz[i,]elseif(inicio$cluster[i]==2)particao_2[i,]=matriz[i,]elseparticao_3[i,]=matriz[i,]i=i+1}
removingzeros
particao_1=particao_1[!(apply(particao_1,1,function(y)any(y==0))),]particao_2=particao_2[!(apply(particao_2,1,function(y)any(y==0))),]particao_3=particao_3[!(apply(particao_3,1,function(y)any(y==0))),]selecao=sample(1:3,1)(if(selecao==1)particao.selecionada=particao_1elseif(selecao==2)particao.selecionada=particao_2elseparticao.selecionada=particao_3)p=length(particao.selecionada[1,])#Numerodeatributos=p
CalculatingSigma
sigma=c()q10=c()q90=c()i=1;j=1;k=1for(iin1:length(matriz[,1])){for(kin1:length(particao.selecionada[,1])){if((matriz[i,]==particao.selecionada[k,])[1]){for(jin1:p){q10[j]=quantile(dist(matrix(c(matriz[,j],particao.selecionada[,j]),ncol=2),method="euclidean")^2, 0.1)
q90[j] = quantile(dist(matrix(c(matriz[,j], particao.selecionada[,j]), ncol = 2),
method = "euclidean")^2, 0.9)
sigma[j] = (q10[j] + q90[j])/2
}
}
}
}
Mounting the kernel array
matriz.kernel = matrix(rep(0, length(matriz[,1])*length(matriz[,1])), ncol = length(matriz[,1]))
for(i in 1:p){
matriz.kernel = gausskernel(matriz, sigma[j])
}
Assembling the equation (d)
#--- Montando a equacao d.1 ---#
d.1 = array(rep(0, length(matriz[,1])*length(particao.selecionada[,1])*p),
dim = c(length(matriz[,1]), length(particao.selecionada[,1]), p))
for(j in 1: length(matriz[,1])){
for(k in 1: length(particao.selecionada[,1])){
for(i in 1 : length(matriz[,1])){
if(i == j){
eq6.1[i,k, ] = matriz.kernel[i,j]
}
}
}
}
Riding the equation d.2
d.2.temp = array(rep(0, length(particao.selecionada[,1])*p),
dim = c(length(particao.selecionada[,1]), p))
d.2 = c()
for(j in 1: length(matriz[1,])){
for(k in 1: length(particao.selecionada[,1])){
for(i in 1 : length(matriz[,1])){
if((matriz[i,] == particao.selecionada[k,])[1]){
eq6.2.temp[k,j] = matriz.kernel[k,j]
eq6.2[j] = 2*sum(eq6.2.temp[,j])/length(particao.selecionada[,1])
}
}
}
}
Riding the equation d.3
d.3.temp = array(rep(0, length(particao.selecionada[,1])*p),
dim = c(length(particao.selecionada[,1]), p))
d.3 = 0
for(j in 1: length(matriz[1,])){
for(i in 1 : length(matriz[,1])){
if((matriz[i,] == particao.selecionada[k,])[1]){
for(k in 1: length(particao.selecionada[,1])){
eq6.3.temp[k,j] = matriz.kernel[k,j]
eq6.3 = sum(eq6.3.temp)/length(particao.selecionada[,1])^2
}
}
}
}
rm(d.3.temp)
n = 1
d.12 = array(rep(0, length(matriz[,1])*length(particao.selecionada[,1])*p),
dim = c(length(matriz[,1]), length(particao.selecionada[,1]), p))
while(n <= 8)
{
d.12[, , n] = eq6.1[, , n] - eq6.2[n]
n = n + 1
}
d = d.12 + d.3
rm(d.12)
The lambda that is an array of length k, where k = length (particle.selected [ 1] and ep = length (matrix [ 1].) I need to operate with the three-dimensional array to solve the lambda equation. p>