Home > Mobile >  How to fit it `Error in hist.default(res) : 'x' must be numeric`?
How to fit it `Error in hist.default(res) : 'x' must be numeric`?

Time:11-29

Following this question: How to get the value of `t` so that my function `h(t)=epsilon` for a fixed `epsilon`?

I first sampling 500 eigenvectors v of a random matrix G and then generate 100 different random vectors initial of dimension 500. I normalized them in mats.

#make this example reproducible
set.seed(100001)
n <- 500
#Sample GOE random matrix
A <- matrix(rnorm(n*n, mean=0, sd=1), n, n) 
G <- (A   t(A))/sqrt(2*n)
ev <- eigen(G)
l <- ev$values
v <- ev$vectors

#size of multivariate distribution
mean <- rep(0, n) 
var <- diag(n)

#simulate bivariate normal distribution
initial <- MASS::mvrnorm(n=1000, mu=mean, Sigma=var) #ten random vectors
#normalized the first possible initial value, the initial data uniformly distributed on the sphere
xmats <- lapply(1:1000, function(i) initial[i, ]/norm(initial[i, ], type="2"))

Then I compute res


h1t <- function(t,x_0) {
  h10 <- c(x_0 %*% v[, n])
  denom <- vapply(t, function(.t) {
    sum((x_0 %*% v)^2 * exp(-4*(l - l[n]) * .t))
  }, numeric(1L))
  abs(h10) / sqrt(denom)
}

find_t <- function(x, epsilon = 0.01, range = c(-50, 50)) {
  uniroot(function(t) h1t(t, x) - epsilon, range,
          tol = .Machine$double.eps)$root
}

I want to get res:

res <- lapply(xmats, find_t)

However, it shows error that Error in uniroot(function(t) h1t(t, x) - epsilon, range, tol = .Machine$double.eps) : f() values at end points not of opposite sign

CodePudding user response:

res is a list. I run hist(unlist(res)) and it worked well.

  •  Tags:  
  • r
  • Related