r(包 extRemes)中的嵌套 for 循环错误
Nested for-loop error in r (package extRemes)
我正在尝试在 r 中制作一个二维向量,然后使用命令 bloxplot()
.
绘制它
numbers <- c(10,100,1000,10000)
for (i in 1:length(numbers)) {
e[i] <- c()
for(j in 1:100) {
a <- revd(numbers[i], loc = 0, scale = 1, shape = 0, type ="GEV")
b <- fevd(x=a, type="Gumbel")
c <- as.numeric(unname(b$results$par[2]))
d <- append(d,c)
}
e[i] <- append(e[i],d)
}
boxplot(e[i])
"revd"生成随机变量,"number[i]"有不同的场景,"fevd"返回参数值(location,scale和shape),它们都来自包"extRemes".
我正在尝试将矢量附加到矢量(二维矢量),但错误消息显示 Error: object 'e' not found
和 Error in boxplot(e[i]) : object 'e' not found
。
我也试过把"e <- c()"放在开头:
numbers <- c(10,100,1000,10000)
e <- c()
for (i in 1:length(numbers)) {
e[i] <- c()
for(j in 1:100) {
a <- revd(numbers[i], loc = 0, scale = 1, shape = 0, type ="GEV")
b <- fevd(x=a, type="Gumbel")
c <- as.numeric(unname(b$results$par[2]))
d <- append(d,c)
}
e[i] <- append(e[i],d)
}
boxplot(e[i])
然后错误消息增加到:
Error in e[i] <- c() : replacement has length zero
In addition: Warning message:
In e[i] <- append(e[i], d) :
number of items to replace is not a multiple of replacement length
和
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) :
need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
是否有其他方法可以不出错地执行此操作?
如果我做对了,这就是一种方法。至少对我而言,而不是使用 append()
,使用矩阵或向量的索引似乎更 R。如果您想多次使用同一个函数,replicate()
也很方便。
library("tidyr")
library("extRemes")
library("ggplot2")
library("reshape2")
numbers <- c(10, 100, 1000, 10000)
num_reps <- 100
out_mat <- matrix(NA, nrow=num_reps, ncol=length(numbers))
scale_out <- function(data){
mod <- revd(data, loc=0, scale=1, shape=0, type="GEV") %>% fevd(type="Gumbel")
return(mod$results$par[[2]])
}
for (i in 1:length(numbers)){
out_mat[, i] <- replicate(num_reps, scale_out(numbers[i]))
}
ggplot(melt(out_mat), aes(x=Var2, y=value)) +
geom_boxplot(aes(group=Var2))
我正在尝试在 r 中制作一个二维向量,然后使用命令 bloxplot()
.
numbers <- c(10,100,1000,10000)
for (i in 1:length(numbers)) {
e[i] <- c()
for(j in 1:100) {
a <- revd(numbers[i], loc = 0, scale = 1, shape = 0, type ="GEV")
b <- fevd(x=a, type="Gumbel")
c <- as.numeric(unname(b$results$par[2]))
d <- append(d,c)
}
e[i] <- append(e[i],d)
}
boxplot(e[i])
"revd"生成随机变量,"number[i]"有不同的场景,"fevd"返回参数值(location,scale和shape),它们都来自包"extRemes".
我正在尝试将矢量附加到矢量(二维矢量),但错误消息显示 Error: object 'e' not found
和 Error in boxplot(e[i]) : object 'e' not found
。
我也试过把"e <- c()"放在开头:
numbers <- c(10,100,1000,10000)
e <- c()
for (i in 1:length(numbers)) {
e[i] <- c()
for(j in 1:100) {
a <- revd(numbers[i], loc = 0, scale = 1, shape = 0, type ="GEV")
b <- fevd(x=a, type="Gumbel")
c <- as.numeric(unname(b$results$par[2]))
d <- append(d,c)
}
e[i] <- append(e[i],d)
}
boxplot(e[i])
然后错误消息增加到:
Error in e[i] <- c() : replacement has length zero
In addition: Warning message:
In e[i] <- append(e[i], d) :
number of items to replace is not a multiple of replacement length
和
Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs) :
need finite 'ylim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
是否有其他方法可以不出错地执行此操作?
如果我做对了,这就是一种方法。至少对我而言,而不是使用 append()
,使用矩阵或向量的索引似乎更 R。如果您想多次使用同一个函数,replicate()
也很方便。
library("tidyr")
library("extRemes")
library("ggplot2")
library("reshape2")
numbers <- c(10, 100, 1000, 10000)
num_reps <- 100
out_mat <- matrix(NA, nrow=num_reps, ncol=length(numbers))
scale_out <- function(data){
mod <- revd(data, loc=0, scale=1, shape=0, type="GEV") %>% fevd(type="Gumbel")
return(mod$results$par[[2]])
}
for (i in 1:length(numbers)){
out_mat[, i] <- replicate(num_reps, scale_out(numbers[i]))
}
ggplot(melt(out_mat), aes(x=Var2, y=value)) +
geom_boxplot(aes(group=Var2))