# Using conditional probability to calculate sentiment score probability

Sorry, maybe this is a bit of a rookie question, but I would like to find out the probability of A(tweet sentiment being negative or positive) based B (the length of the tweet).

This to me sounds like a conditional probability problem and know that the formula translates to P(A|B) = P(A and B)/P(B), but I have no idea how to implement in on my R dataframe. Just to be clear I am trying to calculate the probability of a sentiment being positive or negative if it contains over 35 words

This is a sample of my dataframe

Data

Sorry for not producing an example of what I have done, kind of stumped on what to do

In order to calculate $$P(A|B)$$ using a dataframe in R, you'll want to find the proportion of cases where $$A$$ is true and $$B$$ is also true and then divide by the number of observations where $$B$$ is true. So for example, let's say we have this dataset:

> ##Generate some sample data
> set.seed(3434)
> n<-100
> A<-rbinom(n, 1, .25)
> B<-rbinom(n, 1, 0.6)
> mydata<-data.frame(A=A, B=B)
> rm("A", "B")
A B
1 0 1
2 1 0
3 0 0
4 0 1
5 1 0
6 0 0


Then you can calculate $$P(A|B)$$ in one of two ways:

Method 1

You could use the formula you mentioned in your post and obtain $${P(AB)\over{P(B)}}$$ by summing all the observations where both $$A$$ and $$B$$ are true (or equal 1 as I'm assuming 1=TRUE here) and dividing by the total number of observations in the dataframe ($$n=100$$) to obtain the proportion or probability $$P(AB)$$, and then dividing by the proportion of those cases where $$B$$ is true to obtain $$P(B)$$.

> ##Method 1
> A_true_count <- sum(mydata$$A) > B_true_count <- sum(mydata$$B)
> A_and_B_true_count <- sum(mydata$$A + mydata$$B>1)
> P_of_A <- A_true_count/n
> P_of_B <- B_true_count/n
> P_of_A_and_B <- A_and_B_true_count/n
> P_of_A_given_B_m1 <- P_of_A_and_B/P_of_B
> P_of_A_given_B_m1
[1] 0.2807018


Method 2

You could obtain $$P(A|B)$$ directly by considering $$P(A|B)$$ as a reduced sample space. So you simply determine those cases where $$B$$ is true and then for each of those, find the proportion where A is also true. This will yield the same result as method 1.

> ##Method 2
> #For the reduced sample space where B is True, What proportion have A=True?
> P_of_A_given_B_m2 <- sum(mydata$$A[mydata$$B==1])/sum(mydata\$B==1)
> P_of_A_given_B_m2
[1] 0.2807018


So, in this example, $$P(A|B)=0.2807018$$. Of course, there are several ways to go about getting these more quickly using various functions in R, but this shows the computation in a way that's hopefully easier for you to understand the probability concepts as well as the computation in R.