Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6711905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:11:47+00:00 2026-05-26T08:11:47+00:00

My intention was to write several functions aimed at finding the overall similarity between

  • 0

My intention was to write several functions aimed at finding the overall similarity between two covariance matrices, either by multiplying them with random vectors and correlating the response vectors or by bootstrapping one of the matrices to obtain the correlation coefficient distribution that can serve for comparison. But in both cases I got erroneous results. The observed between-matrix correlation was high up to 0.93, but the distribution only ranged up to 0.2 the most. This is the function`s code:

resamplerSimAlt <- function(mat1, mat2, numR, graph = FALSE)
{
  statSim <- numeric(numR)
  mat1vcv <- cov(mat1)
  mat2vcvT <- cov(mat2)
  ltM1 <- mat1vcv[col(mat1vcv) <= row(mat1vcv)]
  ltM2T <- mat2vcvT[col(mat2vcvT) <= row(mat2vcvT)]
  statObs <- cor(ltM1, ltM2T)                           
  indice <- c(1:length(mat2))
  resamplesIndices <- lapply(1:numR, function(i) sample(indice, replace = F))
  for (i in 1:numR)
  {
    ss <- mat2[sample(resamplesIndices[[i]])]
    ss <- matrix(ss, nrow = dim(mat2)[[1]], ncol = dim(mat2)[[2]])
    mat2ss <- cov(ss)
    ltM2ss <- mat2ss[col(mat2ss) <= row(mat2ss)]
    statSim[i] <- cor(ltM1, ltM2ss)
  }
  if (graph == TRUE)
  {
    plot(1, main = "resampled data density distribution", xlim = c(0, statObs+0.1), ylim = c(0,14))
    points(density(statSim), type="l", lwd=2)
    abline(v = statObs)
    text(10, 10, "observed corelation = ")
  }
  list( obs = statObs , sumFit = sum(statSim > statObs)/numR)
}  

In fact it is hard for me to belive that correlation coefficient between two original matrices is high, and the one between the first original matrix and the second re-sampled one is maximal 0.2 after 10000 bootstrap repetitions.

Any comments on the validity of the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:11:48+00:00Added an answer on May 26, 2026 at 8:11 am

    Sorry, I am not enough educated to catch up your goals about checking the correlation efficient between two covariance matrices, but I tried to apprehend your code per se.

    If I am right, you are making up 10.000 different matrices from the same matrix (mat2) by reordering the cells all round, and recomputing the correlation between the covariance matrix of mat1 and the covariance matrix of the resampled array. Those are stored in the statSim variable.

    You said the original correaltion efficient was high (statObs), but the maximum of statSim was low, which is strange. I think the problem is with your result list:

    list( obs = statObs , sumFit = sum(statSim > statObs)/numR)
    

    Where you return the original correaltion coefficient (obs), but not the written maximum with sumFit. There you might use eg. max(statSim). I see the point in returning sumFit for checking if the resampling did any improvement to the correlation efficient, but based on your code, I see no problem about the theory.

    Updated function with max of simulated correlation coefficients:

    resamplerSimAlt <- function(mat1, mat2, numR, graph = FALSE)
    {
      statSim <- numeric(numR)
      mat1vcv <- cov(mat1)
      mat2vcvT <- cov(mat2)
      ltM1 <- mat1vcv[col(mat1vcv) <= row(mat1vcv)]
      ltM2T <- mat2vcvT[col(mat2vcvT) <= row(mat2vcvT)]
      statObs <- cor(ltM1, ltM2T)                           
      indice <- c(1:length(mat2))
      resamplesIndices <- lapply(1:numR, function(i) sample(indice, replace = F))
      for (i in 1:numR)
      {
        ss <- mat2[sample(resamplesIndices[[i]])]
        ss <- matrix(ss, nrow = dim(mat2)[[1]], ncol = dim(mat2)[[2]])
        mat2ss <- cov(ss)
        ltM2ss <- mat2ss[col(mat2ss) <= row(mat2ss)]
        statSim[i] <- cor(ltM1, ltM2ss)
      }
      if (graph == TRUE)
      {
        plot(1, main = "resampled data density distribution", xlim = c(0, statObs+0.1), ylim = c(0,14))
        points(density(statSim), type="l", lwd=2)
        abline(v = statObs)
        text(10, 10, "observed corelation = ")
      }
      list( obs = statObs , sumFit = sum(statSim > statObs)/numR, max=max(statSim))
    }
    

    What I had run:

    > mat1 <- matrix(runif(25),5,5)
    > mat2 <- mat1+0.2
    > resamplerSimAlt(mat1, mat2, 10000)
    $obs
    [1] 1
    
    $sumFit
    [1] 0
    
    $max
    [1] 0.94463
    

    And with random mat2:

    > mat2 <- matrix(runif(25),5,5)
    > resamplerSimAlt(mat1, mat2, 10000)
    $obs
    [1] 0.31144
    
    $sumFit
    [1] 0.9124
    
    $max
    [1] 0.9231
    

    My answer might not be a real answer. If that would be the case, please give more details about the problem 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two questions: 1) There are several tables that are used as an archive for
If ordinary functions could be used as patterns it would save having to write
My intention is to write a graphical query language where users can drag and
I need to write two classes like this: class Item(Base, DBBase): __tablename__ = 'items'
I'm writing a CouchDB sample. My intention is to write a web app using
I have been designing a component-based game library, with the overall intention of writing
My intention is to write a byte[] to a file. Code snippet is below:
I am trying to write a php function to stop MySQL injection attempts. What
My intention is to have 4 main Nav-bars at a site. If the user
The intention is to build a wrapper to provide a consistent method of calling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.