【问题标题】:how can I change my x and y values to suit properly如何更改我的 x 和 y 值以适合
【发布时间】:2020-07-17 10:22:01
【问题描述】:

这是我的代码 所以问题现在代码有效,但只有在行中 lyst[[elementname]] <- rgamma(10000,i,j) 10000 设置为 100 并在行中 plot(y=sample_mean_q4[,j],x=1:7000,xlab="n value",ylab="Values", main=paste("Alpha-Lambda:",colnames(lyst[,j]),type="l")) x=1:1000 的值设置为 x=1:700 否则我会收到错误“xy.coords(x, y, xlabel, ylabel, log) 中的错误: 'x' 和 'y' 长度不同"

我需要值为 10000 和 x=1=1:1000



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

lyst = matrix(unlist(lyst), ncol = 7, byrow = F) 
colnames(lyst) = c("100-0.1","100-0.5","100-1","100-2","100-5","100-10","100-100")
#Question 4
q4mean <- 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)
}

sample_mean_q4 <- q4mean(lyst)
pdf("Question4.pdf",width=15,height=10)
for (i in 1:7)
{
  for (j in 1:7)
  {
    plot(y=sample_mean_q4[,j],x=1:1000,xlab="n value",ylab="Values", main=paste("Alpha-Lambda:",colnames(lyst[,j]),type="l"))
  }
}
dev.off()

【问题讨论】:

    标签: r list matrix plot


    【解决方案1】:

    请不要在一篇文章中添加很多问题。确定问题并在每个帖子中添加一个。您的代码中的错误与问题 4 有关。

    # Select names of the column to be included in the plot
    selected_cols <- c("100-0.1","100-0.5","100-1","100-2","100-5","100-10","100-100")
    
    # this is equivalent to: `lyst = matrix(unlist(lyst), ncol = 7, byrow = F) `
    # I used abc as name instead of lyst
    abc <- sapply(selected_cols, function(x) lyst[[x]])   
    
    sample_mean_q4 <- q4mean(abc)
    
    # draw plot
    for(x in selected_cols){
      pdf(paste0("Question4", "_", x, ".pdf"),width=15,height=10)
      plot( x=1:1000, y = sample_mean_q4[1:1000, x], 
            xlab="n value", ylab="Values", main=bquote(alpha - lambda == .(x) ), type="l")
      dev.off()
    }
    

    数据:

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

    【讨论】:

    • 当我运行这一行时abc &lt;- sapply(selected_cols, function(x) lyst[[x]]) 我得到了 lyst[x] 下标超出范围的错误
    • 使用来自问题 1 的 lyst 数据。不要运行任何其他程序来更改此数据。
    【解决方案2】:

    你知道 R 的回收功能吗?你试过把问题分解成更小的块吗?

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 2022-11-20
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多