gpt4 book ai didi

r - 仅当 R 中的条件为真时才打印

转载 作者:行者123 更新时间:2023-12-04 08:25:48 26 4
gpt4 key购买 nike

此解决方案 Automate Seed as a Vector Instead of an Integer in R

library(forecast)
SEED_vector <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)
arima_order_results = data.frame()
for (my_seed in SEED_vector){
set.seed(my_seed)
ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic ="aicc")

arima_order = arimaorder(ar2)
arima_order = t(as.data.frame(arima_order))
# Print the arima order.
print(arima_order)
# This line of code is just if you want to store the results in a dataframe
arima_order_results = rbind(arima_order_results,arima_order)
}
我只想要 arimaorder的 (1, 0, 0) 要打印而不是全部
我试过这个并被错误卡住:
SEED_vector <- 1:100
arima_order_results <- data.frame()
while(TRUE){for (my_seed in SEED_vector) {set.seed(my_seed)
ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic ="aicc")
if(all(arimaorder(ar2)==c(1,0,0))) print(arima_order_results)
arima_order = arimaorder(ar2)
arima_order = t(as.data.frame(arima_order))
# Print the arima order.
print(arima_order)
# This line of code is just if yo uwant to store the results in a dataframe
arima_order_results = rbind(arima_order_results,arima_order)

}}
我想要这样的东西:
#arima_order14 1 0 0 
#arima_order39 1 0 0
#arima_order47 1 0 0

最佳答案

也许,这有帮助

SEED_vector <- 1:200
arima_order_results <- data.frame()
flag <- TRUE
i <- 1
seed_out <- c()
while(flag){

set.seed(SEED_vector[i])
ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic ="aicc")
if(all(arimaorder(ar2)==c(1,0,0))) {

#print(arima_order_results)
print(paste0('arimaorder', SEED_vector[i], ' ' ,
paste(arimaorder(ar2), collapse=" ")))
seed_out <- c(seed_out, SEED_vector[i])

}

arima_order = arimaorder(ar2)
arima_order = t(as.data.frame(arima_order))


arima_order_results = rbind(arima_order_results,arima_order)

i <- i+1
if(i == length(SEED_vector)) {

flag <- FALSE
}

}
-输出
#[1] "arimaorder14 1 0 0"
#[1] "arimaorder152 1 0 0"
#[1] "arimaorder165 1 0 0"
现在,我们检查向量
seed_out
#[1] 14 152 165
它可以用 write.table 写入文件或 cat
cat(seed_out, file = 'seed_file.txt')

关于r - 仅当 R 中的条件为真时才打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65270719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com