R foundation for, while, the custom function (proof of the central limit theorem)

Later proved the Central Limit Theorem custom fun not get to know ,,, 
### 6.R language and Statistics #### ### 7. cycling with a custom function #### # 7.1 for ### cycles # Example ( "for") for (I in. 1:. 5) Print (. 1: I) # number of loops through each, each taken a number, is printed a vector for (n in c (2,5,10,20 , 50)) {# vectors not loop through a number x <- stats :: rnorm (n ) # n to generate a normal distribution having a number, n being the number of that loop through the above cat (n, ":" , sum (x ^ 2), "\ n") # corresponding to each number n, the normal distribution of the square array and generating } #cat serving as the connection, the embodiment: number: squares of x and line f <- factor (sample (letters [1: 5], 10, replace = TRUE)); f # samples, generating a sequence of letters for (i in unique (f) ) print (i) # sequentially an extraction sequence in each of a value unique to # 7.2 while statement #### V <- C ( "the Hello", "the while Loop") CNT <- 2 the while (CNT <. 7) { Print (V) CNT = CNT +. 1 } a <- c (1:5) i<-15) while(a[i]<5){ Print (A [I]) I = I +. 1 } the last element of the amount of orientation # B [length (B)] tail (B,. 1) # 7.3 custom function #### myfun_cv <-function (X) { function in R # is an object cv <-sd (x) / mean (x) # function used to calculate the coefficient of variation thereof statement return (cv) # cv return value function execution is completed, the value is the coefficient of variation } # test the function a <-c (1,2,5,8,9,6) # generates a vector myfun_cv (a) # call a custom function to calculate the coefficient of variation # 7.4 by circulating and custom function to verify the central limit Theorem #### myfun <-function (a) { X <-1: Mr # 100 as a sequence of 1 to 100, these values can be changed later, corresponds to overwrite the original value x <-data.frame (x ) a <-data.frame (a) for (I in. 1: 100) {# set loop that 100 samples extracted, and assign the calculated mean of the data frame to the variables x c <-a [sample ( nrow (A), 1000),] m = Mean (C) X $ X [I] <- m } Windows (1280,720); PAR (mfrow = C (1,2)) Plot (Density (A $ A), main = "This is the original distribution") Plot (Density (x $ x), main = " this is the mean distribution of the extracted sample") } ### 7.4 .1 normal #### a <-rnorm (10000,0,1) myfun (a) 7.4.2 exponential distribution ### #### B <-rexp (100000,1) myfun (B) ### 7.4.3t distribution #### C <-rt (1000,3) myfun (C) # ## 7.4.4F distribution #### D <-rchisq (100000,1) myfun (D)

  

Guess you like

Origin www.cnblogs.com/super-yb/p/11037266.html