Reputation: 11
I have created an array of vectors in R and am supposed to find each one of their F-hat values. Is there a line of code that can do this? See mine below.
V <- rbinom(40, 7, .1)
G <- as.data.frame(replicate(100, rbinom(40, 7, .1)))
Now, I wish to extract a specific value from each random sample within G. Any and all help is appreciated.
Upvotes: 0
Views: 868
Reputation: 93761
The R ecdf
function takes a data vector and returns a function that will provide ecdf values for that data vector. To get the actual cumulative probability values of the ecdf for given values within the range of the data vector, you would run the function returned by ecdf
. For example:
set.seed(2)
G <- as.data.frame(replicate(100, rbinom(40, 7, .1)))
ecdf for the first column of G
:
G1 = ecdf(G[ , 1])
Now we can use this function G1
to get the actual empirical cumulative probabilities for each possible value of the data (which happens to be 0 through 7 in this case):
G1(0:7)
[1] 0.500 0.850 0.975 1.000 1.000 1.000 1.000 1.000
This makes sense if we look at the data in column 1 of G
. Note that 50% of the values are zero and 85% (34/40) are zero or 1:
table(G[,1])
0 1 2 3 20 14 5 1
To get the ecdf for every column, you can do:
ecdfG = sapply(G, function(x) ecdf(x)(0:7))
Here are the first 10 columns of ecdfG
. Note that column 1 has the ecdf values that we calculated above.
ecdfG[ , 1:10]
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 [1,] 0.500 0.500 0.575 0.575 0.525 0.450 0.450 0.475 0.525 0.550 [2,] 0.850 0.800 0.825 0.850 0.825 0.775 0.825 0.900 0.850 0.875 [3,] 0.975 0.925 0.950 0.975 0.950 0.950 0.975 0.975 0.975 0.975 [4,] 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 0.975 [5,] 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 [6,] 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 [7,] 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 [8,] 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
Upvotes: 1