############################################################# ##---------------------------------------------------------## ## Inferential Statistics I - ## ## Hypothesis testing in the basic ## ## form of conditional probability / Bayes´ rule ## ## R - Script ## ## by Steffen Schwerdtfeger 07.2022 ## ##---------------------------------------------------------## ############################################################# ############################################################ ## 3 Computing conditional probability / Bayes' rule in R ## ############################################################ # This is a test, which will also be the name of the ‘object’ test = 2 + 5 # Execute this line! ###### Bayes' rule via single values: # Define you prior, e.g., .5 for heads. # Note that R is a case sensitive language (“prior” not same as “Prior”). prior = .5 # Likelihood likelihood = .5 # Joint probability: joint = prior*likelihood # Model evidence (note that R does not allow spacing within names!): model_evidence = .25 + .25 # Posterior: posterior = joint/model_evidence ###### Bayes' rule via probability vectors: # Define prior prior = c(.5, .5) # Likelihood likelihood = c(.5, .5) # Joint probability joint = likelihood*prior # Model evidence model_evidence = sum(joint) # Posterior posterior = joint / model_evidence ###### Vanessa Holmes example: # First iteration: joint = c(.25,.25,.25,25)*c(.33,.33,.33,0) model_evidence = sum(c(.25,.25,.25,.25)*c(.33,.33,.33,0)) posterior = joint/model_evidence # Second iteration (another suspect ruled out): joint = c(.33,.33,.33,0)*c(.5,.5,0,0) model_evidence = sum(c(.33,.33,.33,0)*c(.5,.5,0,0)) posterior = joint/model_evidence