我试图将一个函数应用到我的列表中,但它返回了这个错误
"非数字矩阵扩展错误">
这是我的代码
错误出现在最后几行代码一直运行到最后,因此,我无法绘制图形我在网上搜索过,但找不到任何有用的东西,也看不出代码有什么问题
#Question 1
set.seed(10000)
v <- c(0.1,0.5,1,2,5,10,100)
lyst <- list()
for(i in v)
{
for(j in v)
{
elementname <- paste0(as.character(i),"-",as.character(j))
print(elementname)
lyst[[elementname]] <- rgamma(10000,i,j)
}
}
#Question 2
pdf("Question2.pdf",width = 20, height = 10)
par(mfcol=c(7,7))
for(x in names(lyst))
{
hist(lyst[[x]],
xlab = "Value",
main = paste("Alpha-Lambda:",x))
}
dev.off()
#Question 3
theoretical_mean <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
theoretical_var <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
for (i in 1:7)
{
for (j in 1:7)
{
theoretical_mean[j,i] <- as.character(v[i]/v[j])
theoretical_var[j,i] <- as.character(v[i]/(v[j]^2))
}
}
sample_mean <-lapply(lyst, mean)
sample_mean <- as.data.frame(matrix(unlist(sample_mean),nrow = 7, ncol = 7, byrow = T))
sample_mean <- round(sample_mean,digits = 3)
sample_mean <- data.matrix(sample_mean, rownames.force = NA)
sample_var <-lapply(lyst, var)
sample_var <- as.data.frame(matrix(unlist(sample_var),nrow = 7, ncol = 7, byrow = T))
sample_var <- round(sample_var,digits = 3)
sample_var <- data.matrix(sample_var, rownames.force = NA)
theor_sample_mean <- matrix(paste(theoretical_mean, sample_mean, sep=" - "),nrow=7,dimnames = dimnames(theoretical_var))
theor_sample_var <- matrix(paste(theoretical_var, sample_var, sep=" - "),nrow=7,dimnames= dimnames(theoretical_var))
sink("Q3.txt")
cat("Theoretical Mean vs. Sample Mean:n")
print(as.table(theor_sample_mean))
cat("n")
cat("Theoretical Variance vs. Sample Variance:n")
print(as.table(theor_sample_var))
sink()
#Question 4
nmean <- function(x)
{
m <- matrix(nrow=nrow(x))
for (j in 1:ncol(x))
{
v <- c()
for(i in 1:nrow(x))
{
v <- c(v,mean(x[1:i,j]))
}
m <- cbind(m,v)
}
m <- m[,-1]
colnames(m) <- colnames(x)
rownames(m) <- NULL
return(m)
}
sequentialMeans <- lapply(lyst,nmean)
pdf("Question4.pdf",width=15,height=10)
for (i in 1:7)
{
for (j in 1:7)
{
plot(y=sequentialMeans[[i]][,j],x=1:10000,xlab="n value",ylab="Values", main=paste("Alpha-Lambda:",colnames(lyst[[i]])[j]),type="l")
}
}
dev.off()
代码的问题是nmean
函数的输入数据格式根据行
nmean <- function(x)
{
m <- matrix(nrow=nrow(x))
for (j in 1:ncol(x))
{
v <- c()
for(i in 1:nrow(x))
{
v <- c(v,mean(x[1:i,j]))
}
m <- cbind(m,v)
}
m <- m[,-1]
colnames(m) <- colnames(x)
rownames(m) <- NULL
return(m)
}
是一个矩阵,您希望将以下行中指定的伽玛分布值的矢量馈送给它
lyst <- list()
for(i in v)
{
for(j in v)
{
elementname <- paste0(as.character(i),"-",as.character(j))
print(elementname)
lyst[[elementname]] <- rgamma(10000,i,j)
}
}
对于具有类型vector的x
,函数ncol(x)
和nrow(x)
返回NULL
。此外,ncol(x)
的应用也是不可能的。
如果你想保存你的方法,你需要考虑将数据转换为矩阵格式,或者使用向量格式,但使用向量兼容函数length(x)
表示向量的长度,names(lyst)
表示名称。
更新:
注释中的代码有效,但您必须更改lapply
-语句,因为您现在有了一个矩阵,可以直接用作nmean
函数的输入。以下代码适用于生成sampleMeans
,并避免了问题的原始错误消息。为了减少运行时间,它只需要100个样本。
#Question 1
set.seed(10000)
v <- c(0.1,0.5,1,2,5,10,100)
lyst <- list()
for(i in v)
{
for(j in v)
{
elementname <- paste0(as.character(i),"-",as.character(j))
print(elementname)
lyst[[elementname]] <- rgamma(100,i,j)
}
}
#Question 2
pdf("Question2.pdf",width = 20, height = 10)
par(mfcol=c(7,7))
for(x in names(lyst))
{
hist(lyst[[x]],
xlab = "Value",
main = paste("Alpha-Lambda:",x))
}
dev.off()
#Question 3
theoretical_mean <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
theoretical_var <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
for (i in 1:7)
{
for (j in 1:7)
{
theoretical_mean[j,i] <- as.character(v[i]/v[j])
theoretical_var[j,i] <- as.character(v[i]/(v[j]^2))
}
}
sample_mean <-lapply(lyst, mean)
sample_mean <- as.data.frame(matrix(unlist(sample_mean),nrow = 7, ncol = 7, byrow = T))
sample_mean <- round(sample_mean,digits = 3)
sample_mean <- data.matrix(sample_mean, rownames.force = NA)
sample_var <-lapply(lyst, var)
sample_var <- as.data.frame(matrix(unlist(sample_var),nrow = 7, ncol = 7, byrow = T))
sample_var <- round(sample_var,digits = 3)
sample_var <- data.matrix(sample_var, rownames.force = NA)
theor_sample_mean <- matrix(paste(theoretical_mean, sample_mean, sep=" - "),nrow=7,dimnames = dimnames(theoretical_var))
theor_sample_var <- matrix(paste(theoretical_var, sample_var, sep=" - "),nrow=7,dimnames= dimnames(theoretical_var))
sink("Q3.txt")
cat("Theoretical Mean vs. Sample Mean:n")
print(as.table(theor_sample_mean))
cat("n")
cat("Theoretical Variance vs. Sample Variance:n")
print(as.table(theor_sample_var))
sink()
lyst = matrix(unlist(lyst), ncol = 7, byrow = TRUE)
colnames(lyst) = c("100-0.1","100-0.5","100-1","100-2","100-5","100-10","100-100")
#Question 4
nmean <- function(x)
{
m <- matrix(nrow=nrow(x))
for (j in 1:ncol(x))
{
v <- c()
for(i in 1:nrow(x))
{
v <- c(v,mean(x[1:i,j]))
}
m <- cbind(m,v)
}
m <- m[,-1]
colnames(m) <- colnames(x)
rownames(m) <- NULL
return(m)
}
sequentialMeans <- nmean(lyst)
还要注意,您需要调整Q4的代码,即绘图生成。以下代码有效。
pdf("Question4.pdf",width=15,height=10)
for (i in 1:7)
{
for (j in 1:7)
{
plot(y=sequentialMeans[,j],x=1:700,xlab="n value",ylab="Values", main=paste("Alpha-Lambda:",colnames(lyst[,j]),type="l"))
}
}
dev.off()
如果这有帮助,请告诉我。