繁体   English   中英

如何适应`hist.default(res) 中的错误:'x' 必须是数字`?

[英]How to fit it `Error in hist.default(res) : 'x' must be numeric`?

遵循这个问题: 如何获取 `t` 的值,以便我的 function `h(t)=epsilon` 用于固定的 `epsilon`?

我首先对随机矩阵G的 500 个特征向量v进行采样,然后生成 100 个不同的initial维度为500的随机向量。 我在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"))

然后我计算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
}

我想得到res

res <- lapply(xmats, find_t)

但是,它显示错误Error in uniroot(function(t) h1t(t, x) - epsilon, range, tol =.Machine$double.eps): f() values at end points not of opposite sign

res是一个list 我运行hist(unlist(res))并且运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM