Quantcast

for loop and randomization of matrix

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

for loop and randomization of matrix

kevpar
This post has NOT been accepted by the mailing list yet.
Dear Nabblers,

I'm trying to figure out a way to calculate the sum of squared distances (SSD) between two matrices, where one matrix is held constant and the other is randomized. So far I have been able to get the syntax together to obtain my observed SSD, but the problem for me is obtain SSD from 1000 randomizations to obtain a distribution. Below is my syntax, all data were standardized a priori, each matrix is 6columns x 363rows.

For now the issue seems to be that the 'sample' command destroys the matrix and turns it into a vector. I've tried to remedy this but now it seems I may have one huge matrix from my randomizations rather than 1000 of the original size.

data1<-read.table("StandDataTime1and2.txt", header=TRUE)
data1<-data.frame(data1)

#split data into two matrices 6 X 363
#####trial1
trial1<-cbind(data1$fe1, data1$fr1, data1$ar1, data1$mid1, data$bot1, data$surf1)
trial1<-as.matrix(trial1)

###trial2
trial2<-cbind(data1$fe2, data1$fr2, data1$ar2, data1$mid2, data1$bot2, standardizedData$surf2)
trial2<-as.matrix(trial2)

##calculate euclidean distance between matrices
library(fields)
distances<-rdist(trial1, trial2)

###getting the observed value SSD
squared<-distances %*% distances
ssd<-sum(squared)
ssd
###observed value is 14301499

########################################################################################### Below is where the problem is occuring


########permuting one block (trial2) and calculating a 95% distribution from 1000 randomizations
v <- rep(sample(trial2 <- as.matrix(trial2)),1000)
## Now run those thousand values through the equation to calculate the ssd2

for (i in v) {
  dist2 <-rdist(trial1,[i])
  squared2<-dist2^2
  ssd2<-sum(squared2)
  print(ssd2)
}

Working with matrices seems to add to the level of difficulty. Any help here would be greatly appreciated. Thanks in advance for any help.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: for loop and randomization of matrix

kevpar
This post has NOT been accepted by the mailing list yet.
I was able to get things fixed with regards to the way the 'sample' command was breaking up the matrix (see below). If someone could explain how to carry out this calculation 1000 times using a for loop I would greatly appreciate it.

v <- sample(trial2 <- as.matrix(onesixty))
dim(v) <- dim(trial2)
dist <-rdist(trial1,v)
squared2<-dist^2
ssd2<-sum(squared2)


Loading...