Estimate a probability from elements of a list in R

南楼画角 提交于 2019-12-06 13:37:16

问题


I have a list of 100,000 simulated numbers of T in R (min: 1.5, max 88.8) and I want to calculate the probability of T being between 10 and 50.

I sumulated 100,000 numbers of T, where T is t(y) %*% M %*% y where M is a 8x8 matrix of constant values and y is a 8x1 matrix. The element in the i-th row if y, is equal to: a_i + b_i where a is a vector of constants and b is a vector whose elements follow a normal (0,sd=2) distribution (each element is a different simulated number of N(0,2) )


回答1:


Is it in a vector or a list? If it's a vector, the following should work. If it's in a list, you may use unlist() to convert it to a vector.

mylist <- runif(100000,1.5,88.8) #this is just to generate a random number vector 
length(which(mylist>=10 & mylist<=50))/length(mylist)



回答2:


set.seed(42)
myrandoms <- rnorm(100000, mean=5, sd=2)
mydistr <- ecdf(myrandoms)

#probability of beeing between 1 and 3:
diff(mydistr(c(1, 3)))
#[1] 0.13781

#compare with normal distribution
diff(pnorm(c(1, 3), mean=5, sd=2))
#[1] 0.1359051

If you really have a list, use myrandoms <- do.call(c, mylist) to make it a vector.



来源:https://stackoverflow.com/questions/20843627/estimate-a-probability-from-elements-of-a-list-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!