简体   繁体   中英

What is this non numeric matrix extent error in R?

Im trying to apply a function onto my list but it returns this error

"non numeric matrix extent error"

here's my code

the error occurs in the last few lines the code works fine up till the end, and because of this, im unable to plot my graphs I've searched online but couldnt find anything that helps, and I cant see what's wrong with the code



#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()


The problem with your code is that the data format of the input for the nmean function according to the lines

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)
}

is a matrix and you want feed it vectors of gamma-distributed values as specified in the following lines

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)
  }
}

For x that have type vector, the function ncol(x) and nrow(x) return NULL . Besides, there is also no application of ncol(x) possible.

If you want to save your approach you need to either think about transforming your data into matrix format or alternatively, use the vector format but use the vector-compatible functions length(x) for the length of the vector and names(lyst) for the names.


Update:

The code in the comments works but you got to change the lapply -statement as you now have a matrix that you can use as input for the nmean function directly. The following code works for generating sampleMeans and avoids the original error message of your question. In order to cut down runtime it only takes 100 samples.

#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)

Note also that you need to adjust the code for Q4, that is, the plot generation. The following code works.

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()

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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