2017-02-28 70 views
0

我目前在此网站上使用蒙特卡洛方法http://www.quantpsy.org/medmc/medmc.htm。虽然代码(有一些小的调整)与我的2x2或3x3矩阵的工作,我不断收到以下错误代码为我的4x4矩阵:R矩阵中的代码错误:'dimnames'必须是列表

错误矩阵(C(0.0461705,0,0,0,0, 0.0028639,0,0,0,0,0.0740766,: 'dimnames' 必须是一个列表

什么我做错了,我该如何解决这一错误消息

################################################ 
# This code can be edited in this window and # 
# submitted to Rweb, or for faster performance # 
# and a nicer looking histogram, submit  # 
# directly to R.        # 
################################################ 
require(MASS) 
a=1.1727132      
b=0.2171818 
c=1.3666784 
d=0.1850852 
rep=20000 
conf=95 
pest=c(a,b,c,d) 
acov <- matrix(c(
0.0461705, 0, 0, 0, 
0, 0.0028639, 0, 0, 
0, 0, 0.0740766, 0, 
0, 0, 0, 0.0013694 
),4,4,4,4) 
mcmc <- mvrnorm(rep,pest,acov,empirical=FALSE) 
abcd <- mcmc[,1]*mcmc[,2]*mcmc[,3]*mcmc[,4] 
low=(1-conf/100)/2 
upp=((1-conf/100)/2)+(conf/100) 
LL=quantile(abcd,low) 
UL=quantile(abcd,upp) 
LL4=format(LL,digits=4) 
UL4=format(UL,digits=4) 
################################################ 
# The number of columns in the histogram can # 
# be changed by replacing 'FD' below with  # 
# an integer value.       # 
################################################ 
hist(abcd,breaks='FD',col='skyblue',xlab=paste(conf,'% Confidence Interval ','LL',LL4,' UL',UL4), 
main='Distribution of Indirect Effect') 

谢谢

?!
+1

'matrix'的参数过多:应该是'matrix(c(...),4,4)' –

回答

0

正如@Remko告诉的,请指定参数correc TLY。 R矩阵可以创建为:

acov <- matrix(c(
0.0461705, 0, 0, 0, 
0, 0.0028639, 0, 0, 
0, 0, 0.0740766, 0, 
0, 0, 0, 0.0013694 
),nrow = 4, ncol = 4, byrow = T,dimnames = list(c("r","o","w","s"),c("c","o","l","s"))) 

如果您希望数据按列明智排列,那么您可以设置byrow = F。 rownames和colnames向量的长度必须分别与行数和列数匹配。

相关问题