在 R-studio 中,我想知道如何在不删除之前的循环结果的情况下保存循环结果
In R-studio I want to know how I would save a loop result without deleting the previous loop result
在这段代码中,我想计算 alpha 的许多值(我想计算从 1 到 13 的每个 alpha 的值)的正态分布的截尾均值和截尾均值的方差,并将结果存储在data.frame 然后打印所有结果但问题是新结果存储在以前的结果之上,最后我最终只得到最后一个 alpha 值的结果。
ProDistFun<- data.frame(matrix(nrow=91, ncol=4))
colnames(ProDistFun)<-c("x","Alpha","Trimmed Mean","Variance Of Trimmed Mean")
mu=7 # Mean Value
sigma2=4 # Variance value
for (alpha in c(0.001,0.01,0.025,0.05,0.1,0.25,0.375))
{
for(i in 1:13)
{
ProDistFun[i,1]<-i
ProDistFun[i,2]<-alpha
# The trimmed mean
a=qnorm(alpha, mean=mu, sd=sqrt(sigma2))
b=qnorm(1-alpha, mean=mu, sd=sqrt(sigma2))
fun_TM <- function(x) ((x*exp(-0.5*((x-mu)/sqrt(sigma2))^2))/((1-2*alpha)*(sqrt(2*pi*sigma2))))
MT1 <- integrate(fun_TM, a, b)
MT <-MT1$value
ProDistFun[i,3]<-MT
# The variance of trimmed mean
fun_VTM <- function(x) ((((x-MT)^2)*exp(-0.5*((x-mu)/sqrt(sigma2))^2))/(sqrt(2*pi*sigma2)))
fVTM <- integrate(fun_VTM, a, b)
fV <- fVTM$value
VT=((fV+(alpha*(a-MT)^2)+(alpha*(b-MT)^2))/((1-2*alpha)^2))
ProDistFun[i,4]<-VT
}
}
print(ProDistFun)
保存循环中生成的所有原子值的一般方法是在循环之前创建一个空向量,然后将新值附加到循环内的向量。例如,
output <- vector()
for (i in 1:5) {
newvalue <- i
output <- c(output, newvalue)
}
9 月 30 日编辑 12:23 小时
我仍然不完全清楚你想用 x 完成什么,因为你以令人困惑的方式使用它,但使用你的大部分代码并大幅简化但仍然使用 for loop
创建一个没有矩阵扭曲的空数据框。
ProDistFun <- data.frame(
x = integer(0),
Alpha = numeric(0),
Trimmed_Mean = numeric(0),
Variance_Of_Trimmed_Mean = numeric(0)
)
分配你的两个常量。 重要提示——在你的整个代码中,请停止将赋值 <-
与 =
视为相同的东西,它会在未来导致问题。
mu <- 7 # Mean Value
sigma2 <- 4 # Variance value
将你的两个函数从循环中拉出来(没有必要在每次迭代中重新运行它们)。我不知道它们是否正确,但它们似乎有效。在我看来,在这些函数中,您希望 x 值成为 i
的值,因此我进行了更改。如果你不这样做,你会得到 identical
的 13 次迭代
fun_TM <- function(x) {
((i*exp(-0.5*((i-mu)/sqrt(sigma2))^2))/((1-2*alpha)*(sqrt(2*pi*sigma2))))
}
fun_VTM <- function(x) {
((((i-MT)^2)*exp(-0.5*((i-mu)/sqrt(sigma2))^2))/(sqrt(2*pi*sigma2)))
}
现在我们 运行 alpha
和 i
的嵌套循环。我们构建一行以添加到我们的空数据框 ProDist_df
,然后最后一步是 rbind
到它的 newrow
。请注意,如 integrate
的帮助文件中所述,我们需要 Vectorize
.
for (alpha in c(0.001,0.01,0.025,0.05,0.1,0.25,0.375)) {
for (i in 1:13) {
a <- qnorm(alpha, mean = mu, sd = sqrt(sigma2))
b <- qnorm(1 - alpha, mean = mu, sd = sqrt(sigma2))
MT1 <- integrate(Vectorize(fun_TM), a, b)
MT <- MT1$value
fVTM <- integrate(Vectorize(fun_VTM), a, b)
fV <- fVTM$value
VT <- ((fV+(alpha*(a-MT)^2)+(alpha*(b-MT)^2))/((1-2*alpha)^2))
newrow <- data.frame(x = i,
Alpha = alpha,
Trimmed_Mean = MT,
Variance_Of_Trimmed_Mean = VT)
ProDist_df <- rbind(ProDist_df, newrow)
}
}
ProDist_df
#> x Alpha Trimmed_Mean Variance_Of_Trimmed_Mean
#> 1 1 0.001 0.02744577 0.2003378
#> 2 2 0.001 0.21710028 0.5148306
#> 3 3 0.001 1.00307392 1.4849135
#> 4 4 0.001 3.20833233 0.6092747
#> 5 5 0.001 7.49244239 9.4048587
#> 6 6 0.001 13.08172721 109.7134117
#> 7 7 0.001 17.29412878 262.6203021
#> 8 8 0.001 17.44230295 195.0733274
#> 9 9 0.001 13.48639629 30.3828344
#> 10 10 0.001 8.02083083 3.2269398
#> 11 11 0.001 3.67793771 18.0605860
#> 12 12 0.001 1.30260169 12.5886402
#> 13 13 0.001 0.35679506 4.5613376
#> 14 1 0.010 0.02104086 1.4856627
#> 15 2 0.010 0.16643643 1.7087503
#> 16 3 0.010 0.76899043 2.5612272
#> 17 4 0.010 2.45961619 2.3689151
#> 18 5 0.010 5.74396001 1.1324626
#> 19 6 0.010 10.02889499 28.3270524
#> 20 7 0.010 13.25826466 76.9619814
#> 21 8 0.010 13.37185999 50.5144290
#> 22 9 0.010 10.33912801 2.7851234
#> 23 10 0.010 6.14904048 9.7709433
#> 24 11 0.010 2.81963158 18.3179999
#> 25 12 0.010 0.99861856 11.4783190
#> 26 13 0.010 0.27353117 4.8704116
#> 27 1 0.025 0.01828687 3.5703608
#> 28 2 0.025 0.14465195 3.7170106
#> 29 3 0.025 0.66833905 4.3472611
#> 30 4 0.025 2.13768272 4.1121498
#> 31 5 0.025 4.99214637 1.0747081
#> 32 6 0.025 8.71623613 12.2965566
#> 33 7 0.025 11.52292107 37.4315915
#> 34 8 0.025 11.62164818 22.0916831
#> 35 9 0.025 8.98586347 1.0699878
#> 36 10 0.025 5.34420680 13.1972079
#> 37 11 0.025 2.45057652 19.1385400
#> 38 12 0.025 0.86791169 12.3691446
#> 39 13 0.025 0.23772931 6.5199633
#> 40 1 0.050 0.01619943 7.3749079
#> 41 2 0.050 0.12813995 7.4154400
#> 42 3 0.050 0.59204825 7.6768541
#> 43 4 0.050 1.89366655 6.8889173
#> 44 5 0.050 4.42229360 2.4843697
#> 45 6 0.050 7.72127906 5.6367092
#> 46 7 0.050 10.20758133 19.2763445
#> 47 8 0.050 10.29503875 10.2078727
#> 48 9 0.050 7.96012848 2.5125393
#> 49 10 0.050 4.73416638 16.5558668
#> 50 11 0.050 2.17084357 21.3087060
#> 51 12 0.050 0.76883970 15.1092609
#> 52 13 0.050 0.21059254 9.9710784
#> 53 1 0.100 0.01419911 17.3206584
#> 54 2 0.100 0.11231710 17.1281634
#> 55 3 0.100 0.51894156 16.5102647
#> 56 4 0.100 1.65983477 13.8052303
#> 57 5 0.100 3.87622451 6.3261279
#> 58 6 0.100 6.76784806 2.9011142
#> 59 7 0.100 8.94713932 9.2952208
#> 60 8 0.100 9.02379741 4.8107658
#> 61 9 0.100 6.97720412 6.0182204
#> 62 10 0.100 4.14958694 22.3456468
#> 63 11 0.100 1.90278571 28.0669010
#> 64 12 0.100 0.67390263 23.5641219
#> 65 13 0.100 0.18458837 19.4835253
#> 66 1 0.250 0.01195695 101.3283283
#> 67 2 0.250 0.09458127 99.3524957
#> 68 3 0.250 0.43699624 91.6992768
#> 69 4 0.250 1.39773265 71.1428700
#> 70 5 0.250 3.26413547 35.4870897
#> 71 6 0.250 5.69914690 7.1958771
#> 72 7 0.250 7.53430941 4.8250199
#> 73 8 0.250 7.59886254 4.6624497
#> 74 9 0.250 5.87544385 18.9156520
#> 75 10 0.250 3.49433163 57.7975362
#> 76 11 0.250 1.60231955 87.6386891
#> 77 12 0.250 0.56748763 98.7559156
#> 78 13 0.250 0.15544029 101.2808652
#> 79 1 0.375 0.01129729 591.0212507
#> 80 2 0.375 0.08936330 578.6087319
#> 81 3 0.375 0.41288753 529.2387893
#> 82 4 0.375 1.32062094 401.4184815
#> 83 5 0.375 3.08405591 197.9457725
#> 84 6 0.375 5.38472985 37.5416149
#> 85 7 0.375 7.11864801 5.0996822
#> 86 8 0.375 7.17963979 7.6766516
#> 87 9 0.375 5.55130064 59.4024907
#> 88 10 0.375 3.30155234 228.2708772
#> 89 11 0.375 1.51392096 415.5768786
#> 90 12 0.375 0.53617983 529.7332481
#> 91 13 0.375 0.14686478 575.9244272
在这段代码中,我想计算 alpha 的许多值(我想计算从 1 到 13 的每个 alpha 的值)的正态分布的截尾均值和截尾均值的方差,并将结果存储在data.frame 然后打印所有结果但问题是新结果存储在以前的结果之上,最后我最终只得到最后一个 alpha 值的结果。
ProDistFun<- data.frame(matrix(nrow=91, ncol=4))
colnames(ProDistFun)<-c("x","Alpha","Trimmed Mean","Variance Of Trimmed Mean")
mu=7 # Mean Value
sigma2=4 # Variance value
for (alpha in c(0.001,0.01,0.025,0.05,0.1,0.25,0.375))
{
for(i in 1:13)
{
ProDistFun[i,1]<-i
ProDistFun[i,2]<-alpha
# The trimmed mean
a=qnorm(alpha, mean=mu, sd=sqrt(sigma2))
b=qnorm(1-alpha, mean=mu, sd=sqrt(sigma2))
fun_TM <- function(x) ((x*exp(-0.5*((x-mu)/sqrt(sigma2))^2))/((1-2*alpha)*(sqrt(2*pi*sigma2))))
MT1 <- integrate(fun_TM, a, b)
MT <-MT1$value
ProDistFun[i,3]<-MT
# The variance of trimmed mean
fun_VTM <- function(x) ((((x-MT)^2)*exp(-0.5*((x-mu)/sqrt(sigma2))^2))/(sqrt(2*pi*sigma2)))
fVTM <- integrate(fun_VTM, a, b)
fV <- fVTM$value
VT=((fV+(alpha*(a-MT)^2)+(alpha*(b-MT)^2))/((1-2*alpha)^2))
ProDistFun[i,4]<-VT
}
}
print(ProDistFun)
保存循环中生成的所有原子值的一般方法是在循环之前创建一个空向量,然后将新值附加到循环内的向量。例如,
output <- vector()
for (i in 1:5) {
newvalue <- i
output <- c(output, newvalue)
}
9 月 30 日编辑 12:23 小时
我仍然不完全清楚你想用 x 完成什么,因为你以令人困惑的方式使用它,但使用你的大部分代码并大幅简化但仍然使用 for loop
创建一个没有矩阵扭曲的空数据框。
ProDistFun <- data.frame(
x = integer(0),
Alpha = numeric(0),
Trimmed_Mean = numeric(0),
Variance_Of_Trimmed_Mean = numeric(0)
)
分配你的两个常量。 重要提示——在你的整个代码中,请停止将赋值 <-
与 =
视为相同的东西,它会在未来导致问题。
mu <- 7 # Mean Value
sigma2 <- 4 # Variance value
将你的两个函数从循环中拉出来(没有必要在每次迭代中重新运行它们)。我不知道它们是否正确,但它们似乎有效。在我看来,在这些函数中,您希望 x 值成为 i
的值,因此我进行了更改。如果你不这样做,你会得到 identical
fun_TM <- function(x) {
((i*exp(-0.5*((i-mu)/sqrt(sigma2))^2))/((1-2*alpha)*(sqrt(2*pi*sigma2))))
}
fun_VTM <- function(x) {
((((i-MT)^2)*exp(-0.5*((i-mu)/sqrt(sigma2))^2))/(sqrt(2*pi*sigma2)))
}
现在我们 运行 alpha
和 i
的嵌套循环。我们构建一行以添加到我们的空数据框 ProDist_df
,然后最后一步是 rbind
到它的 newrow
。请注意,如 integrate
的帮助文件中所述,我们需要 Vectorize
.
for (alpha in c(0.001,0.01,0.025,0.05,0.1,0.25,0.375)) {
for (i in 1:13) {
a <- qnorm(alpha, mean = mu, sd = sqrt(sigma2))
b <- qnorm(1 - alpha, mean = mu, sd = sqrt(sigma2))
MT1 <- integrate(Vectorize(fun_TM), a, b)
MT <- MT1$value
fVTM <- integrate(Vectorize(fun_VTM), a, b)
fV <- fVTM$value
VT <- ((fV+(alpha*(a-MT)^2)+(alpha*(b-MT)^2))/((1-2*alpha)^2))
newrow <- data.frame(x = i,
Alpha = alpha,
Trimmed_Mean = MT,
Variance_Of_Trimmed_Mean = VT)
ProDist_df <- rbind(ProDist_df, newrow)
}
}
ProDist_df
#> x Alpha Trimmed_Mean Variance_Of_Trimmed_Mean
#> 1 1 0.001 0.02744577 0.2003378
#> 2 2 0.001 0.21710028 0.5148306
#> 3 3 0.001 1.00307392 1.4849135
#> 4 4 0.001 3.20833233 0.6092747
#> 5 5 0.001 7.49244239 9.4048587
#> 6 6 0.001 13.08172721 109.7134117
#> 7 7 0.001 17.29412878 262.6203021
#> 8 8 0.001 17.44230295 195.0733274
#> 9 9 0.001 13.48639629 30.3828344
#> 10 10 0.001 8.02083083 3.2269398
#> 11 11 0.001 3.67793771 18.0605860
#> 12 12 0.001 1.30260169 12.5886402
#> 13 13 0.001 0.35679506 4.5613376
#> 14 1 0.010 0.02104086 1.4856627
#> 15 2 0.010 0.16643643 1.7087503
#> 16 3 0.010 0.76899043 2.5612272
#> 17 4 0.010 2.45961619 2.3689151
#> 18 5 0.010 5.74396001 1.1324626
#> 19 6 0.010 10.02889499 28.3270524
#> 20 7 0.010 13.25826466 76.9619814
#> 21 8 0.010 13.37185999 50.5144290
#> 22 9 0.010 10.33912801 2.7851234
#> 23 10 0.010 6.14904048 9.7709433
#> 24 11 0.010 2.81963158 18.3179999
#> 25 12 0.010 0.99861856 11.4783190
#> 26 13 0.010 0.27353117 4.8704116
#> 27 1 0.025 0.01828687 3.5703608
#> 28 2 0.025 0.14465195 3.7170106
#> 29 3 0.025 0.66833905 4.3472611
#> 30 4 0.025 2.13768272 4.1121498
#> 31 5 0.025 4.99214637 1.0747081
#> 32 6 0.025 8.71623613 12.2965566
#> 33 7 0.025 11.52292107 37.4315915
#> 34 8 0.025 11.62164818 22.0916831
#> 35 9 0.025 8.98586347 1.0699878
#> 36 10 0.025 5.34420680 13.1972079
#> 37 11 0.025 2.45057652 19.1385400
#> 38 12 0.025 0.86791169 12.3691446
#> 39 13 0.025 0.23772931 6.5199633
#> 40 1 0.050 0.01619943 7.3749079
#> 41 2 0.050 0.12813995 7.4154400
#> 42 3 0.050 0.59204825 7.6768541
#> 43 4 0.050 1.89366655 6.8889173
#> 44 5 0.050 4.42229360 2.4843697
#> 45 6 0.050 7.72127906 5.6367092
#> 46 7 0.050 10.20758133 19.2763445
#> 47 8 0.050 10.29503875 10.2078727
#> 48 9 0.050 7.96012848 2.5125393
#> 49 10 0.050 4.73416638 16.5558668
#> 50 11 0.050 2.17084357 21.3087060
#> 51 12 0.050 0.76883970 15.1092609
#> 52 13 0.050 0.21059254 9.9710784
#> 53 1 0.100 0.01419911 17.3206584
#> 54 2 0.100 0.11231710 17.1281634
#> 55 3 0.100 0.51894156 16.5102647
#> 56 4 0.100 1.65983477 13.8052303
#> 57 5 0.100 3.87622451 6.3261279
#> 58 6 0.100 6.76784806 2.9011142
#> 59 7 0.100 8.94713932 9.2952208
#> 60 8 0.100 9.02379741 4.8107658
#> 61 9 0.100 6.97720412 6.0182204
#> 62 10 0.100 4.14958694 22.3456468
#> 63 11 0.100 1.90278571 28.0669010
#> 64 12 0.100 0.67390263 23.5641219
#> 65 13 0.100 0.18458837 19.4835253
#> 66 1 0.250 0.01195695 101.3283283
#> 67 2 0.250 0.09458127 99.3524957
#> 68 3 0.250 0.43699624 91.6992768
#> 69 4 0.250 1.39773265 71.1428700
#> 70 5 0.250 3.26413547 35.4870897
#> 71 6 0.250 5.69914690 7.1958771
#> 72 7 0.250 7.53430941 4.8250199
#> 73 8 0.250 7.59886254 4.6624497
#> 74 9 0.250 5.87544385 18.9156520
#> 75 10 0.250 3.49433163 57.7975362
#> 76 11 0.250 1.60231955 87.6386891
#> 77 12 0.250 0.56748763 98.7559156
#> 78 13 0.250 0.15544029 101.2808652
#> 79 1 0.375 0.01129729 591.0212507
#> 80 2 0.375 0.08936330 578.6087319
#> 81 3 0.375 0.41288753 529.2387893
#> 82 4 0.375 1.32062094 401.4184815
#> 83 5 0.375 3.08405591 197.9457725
#> 84 6 0.375 5.38472985 37.5416149
#> 85 7 0.375 7.11864801 5.0996822
#> 86 8 0.375 7.17963979 7.6766516
#> 87 9 0.375 5.55130064 59.4024907
#> 88 10 0.375 3.30155234 228.2708772
#> 89 11 0.375 1.51392096 415.5768786
#> 90 12 0.375 0.53617983 529.7332481
#> 91 13 0.375 0.14686478 575.9244272