Calculating just a single row of dissimilarity/distance matrix - r

I have a data-frame with 30k rows and 10 features. I would like to calculate distance matrix like below;
gower_dist <- daisy(data-frame, metric = "gower"),
This function returns whole dissimilarity matrix. I want to get just the first row.
(Just distances of the first element in data-frame). How can I do it? Do you have an idea?

You probably need to get the source and extend it.
I suggest you extend the API by adding a second parameter y that defaults to x. Then the method should return the pairwise distances of each element in x to each element in y.
Fortunately, R is GPL open source, so this is easy.
This would likely be a welcome extension, you should submit it to the package authors for inclusion.

Related

How to calculate distance from center of a hypersphere (whitened PCA)

Lets say that I have calculated the principal components of a reference data set including whitening. The transformation matrix created from the principal component vectors is then applies to a test data set, projecting it onto the subspace of PCs. Now, I should be able to measure the distance of each of the test data vectors from the center of the PC hypersphere by simply adding up the coefficients of each column. Is this correct? Applying this transformation to my reference data gives a length of zero for all columns, and the length of the vectors seems to decrease as I make the test data look more like the reference data and grows as I make the two sets more distinct.
Am I correct that I can judge "distance" in a multidimensional space in this way? Is it just the sum of the coefficients of the projected matrix?
Thanks very much for any insight you can provide.
Distance is not a linear sum, and it is never zero (outside the origin). It is computed as:
distance(x) = square_root( sum ( x(i)^2 ) )
If this was not what you where looking for, please expand your question and include some code and examples.

Cortest.mat converting to class "psych,sim"

I've been using the psych package to compare two correlation matrices using the function cortest.
Now I want to try the cortest.mat and cortest.jennrich function which require an object of the class phychand sim. I have tried converting mi correlation matrices with sim.structure which results in an object of such classes but I get an error when running either function.
Here is what I've tried using Random numbers:
Random<-cor(matrix(rnorm(400, 0, .25), nrow=(20), ncol=(20)))
SimRandom<-sim.structure(Random)
class(SimRandom)
cortest.jennrich(SimRandom,SimRandom,n1=400, n2=400)
Yields the following:
Error in if (dim(R1)[1] != p) { : argument is of length zero
I sure I'm doing it wrong 'cause of the error message and 'cause the values in Random and SimRandom are not exactly the same.
Which is the correct way to translate a correlation matrix to a type -phych, sim- to use as input for running cortest.mat?
Thanks in advance.
EDIT: Short explanation on what I want to do. Using Random numbers serves just as an example. The actual correlation matrices to compare are done as follows. I have a huge list of files each composed of 100 observations for a specific genetic location. These files can be grouped into say 20 files based on known genetic relationships, thus I use those groups of files, load them into a matrix as columns and calculate cor(). That gives a correlation matrix. As a control I load random files and treat them the same way. This matrix contains real data, but the grouping is done randomly. In the end I have two correlation matrices 1-That contains the correlations of pre-selected files and 2- that contains the correlations between randomly loaded files. Both matrices are the same size.
What I would like to do is to compare the two correlation matrices to have an idea whether the grouping has an influence on the correlation values observed.
Sorry for not explaining this earlier, I wanted to avoid the long explanation and keep the question simple.

How to add weight to attribute inside feature vector for distance measure?

I am attempting to measure the distance between two feature vectors, but I want to give more importance to one attribute inside the feature vector beyond the rest. For example, if the vector I had below were filled with numeric features, how would I place more value on "taste"?
V = [ Taste, Smell, Feel, Look ]
I know I could just isolate that value and perform the distance measure on that, but I wasn't sure if that were the best way and if I would lose the "rest of the picture" by doing so. When I search for weighted distance measures, I tend to land on pages where the weight is just being used for normalization or standardization of the data which doesn't appear to carry the same meaning as what I would like.
Am I better off using the distance measure on the full vector and then applying something like KNN with weights later on?
I think you can try matrix multiply means you can give a weight matrix and just multiply this weight matrix with your data.

calculating DFT coefficient

I have a time series and it contains 256 integer values. It looks like this:
I have calculated STFT( Short Time Forier Transform) whit this code in r:
s<-stft(datalist, win=min(80,floor(length(datalist)/10)), inc=min(24,floor(length(datalist)/30)), coef=256, wtype="hanning.window")
as a resault I have a matrix with 29 rows and 256 values. If I show one row of this matrix in a plot( i.e 10th row). I am seeing such a diagramm:
but I have this expectation, that the coefficient diagram should look like the first diagramm?(only in another dimension)
should I use another package in R to do this job? or my understood is false?
I guess you are using the stft function from package GENEArerad. In your case, the call is basically
s<-stft(datalist, win=25, inc=8, coef=256, wtype="hanning.window")
So the way I read it, you are taking 25 samples but computing 256 coefficients from this. The documentation states that the maximal (reasonable) value for coef is win/2, due to the Nyquist-Shannon sampling theorem I guess. So all but the first 12 or so coefficients will be mostly bogus. And those first few coefficients are off the scale of your plot, so we can't say anything about these either.
I don't know where your expectation did come from, and I don't share it. But I also believe there are some more fundamental problems with how you expect this to work.

Creating a correlation matrix using novel distance function

I'm trying to write a function that will create a correlation matrix using a fancy distance estimate (dcorr, Brownian distance). More generally, I want to write code for a generic "correlation" matrix in which you can plug in any distance estimator.
My data is formatted such that columns are variables and rows are observations.
I'm having problems with my basic code. My algorithm is as follows:
Use apply to take a variable
Pass to function that will again take apply on the entire matrix
At this point you should have two pairs of variables
Use na.omit to remove missing observations (necessary for dcorr)
Calculate dcorr
I was hoping this would result in the correlation matrix but I'm having a lot of problems with basic variable managment. I'm having difficulty passing variables to the apply function. In particular, I want to pass a the column that was pulled in the first apply and pass it to the second apply (that is applied on the entire original matrix)
My code:
dcormatrix <- function(Matrix){
dcorhelper <- function (Col1){
as.matrix(apply(Matrix,2,function(Col2){
B <- na.omit(cbind(Col1,Col2))
dcor(B[,1],B[,2],index=1)
},Col1=Col1))
}
apply(Matrix,2,dcorhelper(),Matrix=Matrix)
}
Any ideas? I'm sure there's gotta be an easy way to do this.
You may want to check out designdist from the vegan package. It allows one to define alternate distance / dissimilarity matrices. See here.

Resources