statistics - Conditional Probability in R -
i using prob
package in r calculate conditional probability.
my data set
q1 q2 q3 q4 1 1 0 0 0 0 0 0 0 1 0 1 0 1 0 1
i want calculate prob(q2 =1 given q4=1), per knowledge should 1. when use following command in r
prob(a,q2==1,q4==1)
return 0.5
how come return 0.5? 0.5, right? doubting answer.
the second question if change data set
q1 q2 q3 q4 1 1 0 0 1 0 1 0 0 1 0 1 1 1 1 1
when use above data , calculate above probability returns 1. how come probability changes when not changing q2 , q4.
thinking should same 1 in both cases.
how come changes change in other parameter q1 , q3. think should change p(q2=1 / q4=1) independent of q1 , q3.
the problem prob
uses intersect
excludes duplicates. calculation sum(intersect(a, b)$probs)/sum(b$probs)
0.25/0.5=0.5.
if want correct calculation, have use exclusive probabilities (the 3rd line has probability of 50%):
a <-read.table(text="q1 q2 q3 q4 1 1 0 0 0 0 0 0 0 1 0 1",header=true,stringsasfactors=false) a$probs <-c(0.25,0.25,0.5) prob(a,event=q2==1,given=q4==1) [1] 1
as second question, prob
working correctly because intersect
not removing duplicates because line 3 , 4 different.
Comments
Post a Comment