problem

  1. generateData: takes as input the test results number, probability of positive results, and name of the test results file, and outputs the results file
  2. loadData: takes as input the test results file and outputs the results as a dataframe
  3. bayesianInference: takes as input the dataframe, prior, false positive rate, and hit rate, and outputs the vector of posterior probabilities
  4. plotProsterior: takes as input the vector of posterior probabilities and name of the figure (where x axis is the number of data and y axis the corresponding posterior probabilities), and outputs the figure

generateData

#generateData
generateData<-function(testResultsNum, PosTestResultsProb, testResultsFile){
  set.seed(1) #set a random seed for sample function
  # Generate a random sample of N(True=1, False=0), applying testResultsNum and PosTestResultsProb
  Test_results=sample( x=c(0,1), prob=c(1-PosTestResultsProb,PosTestResultsProb), size=testResultsNum, replace=TRUE )  
  data=data.frame(c(1:testResultsNum),Test_results) #generate the dataframe ,define name of the test results
  return(write.csv(data,file=testResultsFile)) # outputs the result file
}

loadData

#loadData
loadData<-function(testResultsFile){
  return(data.frame(testResultsFile=read.csv(testResultsFile))) #input the test results file and outputs the results as a dataframe
}


#experiment for loadData function using 'testResult.csv'
testResults=loadData('testResults.csv')

bayesianInference

#bayesianInference
bayesianInference<-function(testResults, prior, fpr, hr){
  n=nrow(testResults)
  posteriorProb<- vector("numeric", 10L)
  for (i in c(1:n)){
    if (testResults$testResultsFile.Test_results[[i]]==1){
      posteriorProb[[i]]=hr*prior/(hr*prior+fpr*(1-prior)) #according to bayes rule
      i=i+1
    }
    else{
      posteriorProb[[i]]=(1-hr)*prior/((1-hr)*prior+(1-fpr)*(1-prior))
      i=i+1
    }
  }
  return(posteriorProb) #output
  
  
}

#experiment for bayesianInference function using 'testResult.csv'
posteriors=bayesianInference(testResults,0.01,0.05,0.99)
posteriors
##   [1] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##   [6] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [11] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.0001063151
##  [16] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [21] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [26] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.0001063151
##  [31] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [36] 0.1666666667 0.1666666667 0.0001063151 0.1666666667 0.1666666667
##  [41] 0.1666666667 0.1666666667 0.1666666667 0.0001063151 0.1666666667
##  [46] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [51] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [56] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [61] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.0001063151
##  [66] 0.1666666667 0.1666666667 0.1666666667 0.0001063151 0.1666666667
##  [71] 0.0001063151 0.0001063151 0.1666666667 0.1666666667 0.1666666667
##  [76] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [81] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.0001063151
##  [86] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667
##  [91] 0.1666666667 0.1666666667 0.0001063151 0.1666666667 0.0001063151
##  [96] 0.1666666667 0.1666666667 0.1666666667 0.1666666667 0.1666666667

plotProsterior

#plotProsterior function
plotProsterior<- function(posteriors, posteriorsFig){
  plot(posteriors, xlab="the number of data", ylab="posterior probabilities", main=posteriorsFig,type='l',col='red',lwd=4)
}


#experiment for plotProsterior function using 'testResult.csv'
plotProsterior(posteriors,'posteriorsFig')