简体   繁体   English

如何处理R中的“非数字矩阵范围”错误?

[英]How to counter the 'non-numeric matrix extent' error in R?

I'm trying to generate a data frame of simulated values from the student's t distribution using the standard stochastic equation. 我正在尝试使用标准随机方程从学生的t分布生成模拟值的数据框。 The function I use is as follows: 我使用的功能如下:

matgen<-function(means,chi,covariancematrix)
{
 cols<-ncol(means);
 normals<-mvrnorm(n=500,mu=means,Sigma = covariancematrix);
 invgammas<-rigamma(n=500,alpha=chi/2,beta=chi/2);
 gen<-as.data.frame(matrix(data=NA,ncol=cols,nrow=500));
 i<-1;
 while(i<=500)
 {
   gen[i,]<-t(means)+normals[i,]*sqrt(invgammas[i]);
   i<=i+1;
 }
return(gen);
}

If it's not clear, I'm trying to create an empty data frame, that takes in values in cols number of columns and 500 rows. 如果不清楚,我正在尝试创建一个空数据框,它接受cols列数和500行的值。 The values are numeric, of course, and R tells me that in the 9th row: 当然,值是数字,R告诉我在第9行:

gen<-as.data.frame(matrix(data=NA,ncol=cols,nrow=500));

There's an error: 'non-numeric matrix extent'. 有一个错误:'非数字矩阵范围'。

I remember using as.data.frame() to convert matrices into data frames in the past, and it worked quite smoothly. 我记得过去使用as.data.frame()将矩阵转换为数据帧,并且工作得很顺利。 Even with numbers. 即使有数字。 I have been out of touch for a while, though, and can't seem to recollect or find online a solution to this problem. 不过,我已经失去了一段时间,似乎无法回想起或在网上找到解决这个问题的方法。 I tried is.numeric() , as.numeric() , 0s instead of NA there, but nothing works. 我尝试了is.numeric()as.numeric() ,0s而不是NA,但没有任何效果。

As Roland pointed out, one problem is, that col doesn't seem to be numeric. 正如罗兰所指出的,一个问题是,col似乎不是数字。 Please check if means is a dataframe or matrix, eg str(means). 请检查means是否是数据框或矩阵,例如str(均值)。 If it is, your code should not result in the error: 'non-numeric matrix extent'. 如果是,您的代码不应导致错误:'非数字矩阵范围'。

You also have some other issues in your code. 您的代码中还有其他一些问题。 I created a simplified example and pointed out the bugs I found as comments in the code: 我创建了一个简化的示例,并指出我在代码中发现的错误:

library(MASS)
library(LearnBayes)

means <- cbind(c(1,2,3),c(4,5,6))
chi <- 10

matgen<-function(means,chi,covariancematrix)
{
  cols <- ncol(means) # if means is a dataframe or matrix, this should work

  normals <- rnorm(n=20,mean=100,sd=10) # changed example for simplification
  # normals<-mvrnorm(n=20,mu=means,Sigma = covariancematrix) 
  # input to mu of mvrnorm should be a vector, see ?mvrnorm; but this means that ncol(means) is always 1 !?

  invgammas<-rigamma(n=20,a=chi/2,b=chi/2) # changed alpha= to a and beta= to b

  gen<-as.data.frame(matrix(data=NA,ncol=cols,nrow=20))

  i<-1
  while(i<=20)
  {
    gen[i,]<-t(means)+normals[i]*sqrt(invgammas[i]) # changed normals[i,] to normals [i], because it is a vector
    i<-i+1 # changed <= to <- 
  }
  return(gen)
}

matgen(means,chi,covariancematrix)

I hope this helps. 我希望这有帮助。 PS You don't need ";" PS你不需要“;” at the end of every line in R 在R的每一行的末尾

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

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