如何解决R中的“非数值矩阵范围"错误? [英] How to counter the 'non-numeric matrix extent' error in R?

查看:1820
本文介绍了如何解决R中的“非数值矩阵范围"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用标准随机方程从学生的t分布生成模拟值的数据框.我使用的功能如下:

I'm trying to generate a data frame of simulated values from the student's t distribution using the standard stochastic equation. 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);
}

如果不清楚,我正在尝试创建一个空的数据框,该数据框采用cols列数和500行的值.这些值是数字,当然,R在第9行告诉我:

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. The values are numeric, of course, and R tells me that in the 9th row:

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

有一个错误:非数字矩阵范围".

There's an error: 'non-numeric matrix extent'.

我记得在过去使用as.data.frame()将矩阵转换为数据帧,并且工作非常顺利.即使有数字.不过,我已经失去联系了一段时间,似乎无法回忆或在网上找到解决此问题的方法.我尝试使用is.numeric()as.numeric(),0代替那里的NA,但没有任何效果.

I remember using as.data.frame() to convert matrices into data frames in the past, and it worked quite smoothly. 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.

推荐答案

正如Roland指出的那样,一个问题是col似乎不是数字.请检查平均值是否为数据框或矩阵,例如str(意味着).如果是这样,您的代码不应导致错误:非数字矩阵范围".

As Roland pointed out, one problem is, that col doesn't seem to be numeric. Please check if means is a dataframe or matrix, e.g. str(means). 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)

我希望这会有所帮助. P.S.您不需要;"在R中的每一行的末尾

I hope this helps. P.S. You don't need ";" at the end of every line in R

这篇关于如何解决R中的“非数值矩阵范围"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆