I have to make a 4*4 Kohonen map for a project.
However, I get the error
Error in win_index[1, ] : subscript out of bounds
In addition: There were 16 warnings (use warnings() to see them)
After testing my code, I guess it's the lapply function (line 77) that doesn't is not executed correctly because the matrix thus created contains only NA. And so since this matrix is used later on, the result is not correct because NA is present throughout the program.
#############################################
##Function for distance calculation (RMSDA)##
#############################################
RMSDA<-function(data_phipsi,Kohonen_matrix)
{
difference<-data_phipsi-Kohonen_matrix
for(j in 1:length(difference)){
if (difference[j]< -180) {difference[j]=difference[j]+360}
if (difference[j]> +180) {difference[j]=difference[j]-360}
}
distance=mean(sqrt(difference^2))
return(distance)
}
##############################
## Program ###
##############################
for(step in 1:iteration)
{
data_phipsi<-data_phipsi[sample(nrow(data_phipsi)),] # Sample vectors of training (samples of lines of the dataframe)
print(step) #Visualize where we are in loops
for(k_row in 1:nrow(data_phipsi))
{
#Update learn_rate and radius at each row of each iteration
learn_rate<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
learn_radius<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
#Find distance between each vectors of angles of Kohonen Map and the training vector
phipsi_RMSDA<-lapply(random_list, RMSDA, data_phipsi=data_phipsi[k_row,])
}
How is it possible to fix this lapply error?
Thank you. Thank you.
edit : These are the only useful elements for the lapply
For the random list we can use:
random_list <- list(
c(88, 148, 60, 83, -119, -59, -96, 169),
c(104, 101, 174, -48, 18, 10, -159, 158),
c(164, -80, 137, -170, -172, 52, -149, 96),
c(88, 18, -115, 48, -3, -158, -92, -154),
c(170, -107, -109, -14, -142, -77, -120, 76),
c(-121, 15, -46, -145, -128, 74, -166, 44),
c(46, -178, 67, -88, -125, -130, 88, -11),
c(131, 147, -32, 103, -16, 116, 78, -125),
c(75, -95, -137, 133, -97, -134, 126, -105),
c(115, 173, -82, -135, 134, 82, -143, -43),
c(111, 13, -54, -53, 103, 132, -13, -43),
c(-143, 89, -91, -137, -63, 14, -166, 83),
c(-98, 178, 14, -80, -122, -25, 19, 117),
c(-113, -97, 34, -178, -56, 18, -167, 84),
c(49, 82, 50, 168, -157, -154, 51, 78),
c(173, -4, 164, 125, 31, 115, -74, -92)
)
and a exemple for data_phipsi[1,]
data_phipsi <- read.table(header = TRUE, text = "
phi1 psi2 phy2 psi3 phy3 psi4 phy4 psi5
-24.5 81.9 -155.2 -81.4 127.7 -118 166 -82.1")
data_phipsi
# phi1 psi2 phy2 psi3 phy3 psi4 phy4 psi5
# 1 -24.5 81.9 -155.2 -81.4 127.7 -118 166 -82.1