Search code examples
rstaticnormal-distributionnested-for-loop

In R-studio I want to know how I would save a loop result without deleting the previous loop result


In this code I want to calculate the trimmed mean and variance of trimmed mean for normal distribution in many value of alpha ( I want to calculate the value of the each alpha from 1 to 13 ) and storage the result in the data.frame then print the all result But the problem is that the new result storages over the previous result and at the end i end up getting only the last result of alpha value.

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)

Solution

  • Edited Sept 30 12:23 hours

    It's still not totally clear to me what you're trying to accomplish with x since you use it in confusing ways, but using most of your code and simplifying drastically but still using a for loop

    Create an empty dataframe without the matrix contortions.

    ProDistFun <- data.frame(
       x = integer(0), 
       Alpha = numeric(0), 
       Trimmed_Mean = numeric(0), 
       Variance_Of_Trimmed_Mean = numeric(0)
       )
    

    Assign your two constants. IMPORTANT NOTE -- throughout your code please stop treating assignment <- as the same thing as = it will cause problems down the road.

    mu <- 7    # Mean Value
    sigma2 <- 4   # Variance value
    

    Pull your two functions out of the loops (no point in rerunning them every iteration). I have no idea if they are correct but they seem to work. It also seems to me that in these functions you want the x value to be the value of i so I've made that change. If you don't you get 13 iterations of 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)))
    }
    

    Now we run the nested loops of alpha and i. We build a row to add to our empty dataframe ProDist_df and then last step is rbind the newrow to it. Note as stated in the help file for integrate we need to 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