R/genVoterLevelDataFrame.R

#' genVoterLevelDataFrame
#' 
#' Takes the information from a group level data frame generated by genGroupLevelDataframe(), and draws ui and ei for the groups of voters based on it.
#' @param groupDataFrame A data frame generated by genGroupLevelDataframe.
#' @return voterDataFrame A data frame with a row for each, containing the following columns: groupID, ui and ei.
#' @export

genVoterLevelDataFrame<- function(groupDataFrame){
  groupID <- NULL # needed to pass R CMD CHK
  numberOfGroups <- nrow(groupDataFrame)          # Extracts the number of groups from the group data frame.
  numberOfVoters <- sum(groupDataFrame$groupSize) # Calculates the total number of voters from the group data frame.
  # Create Data Frame and Assign Group Numbers
  groupIDnumbers <- vector(mode = "numeric", length = 0) # Empty vector to put ID numbers in
  for (i in 1:numberOfGroups){
    groupIDnumbers <- c(groupIDnumbers,rep(i,groupDataFrame$groupSize[i]))
    voterDataFrame <- data.frame(groupID=factor(groupIDnumbers))
  }
  for (i in 1:numberOfGroups){    # This loop draws ui values
    aGroup <- subset(voterDataFrame,groupID==i)                    # First subset the data frame for group 
    sizeOfAGroup <- nrow(aGroup)                                   # determine the number of voters
    aGroupUtility <- drawNormalValues(groupDataFrame$groupSize[i],groupDataFrame$utilityDistributionParam1[i],groupDataFrame$utilityDistributionParam2[i])    # Draw the values from the normal.
    voterDataFrame$ui[voterDataFrame$groupID==i] <- aGroupUtility # Now place these utilities in the data frame
  } # Ends the ui draw loop 
  for (i in 1:numberOfGroups){                            # This loop draws ei values
    aGroup <- subset(voterDataFrame,groupID==i)           # First subset the data frame for group 
    sizeOfAGroup <- nrow(aGroup)                          # determine the number of voters
    if (groupDataFrame$errorDistribution[i]=="normal"){                      # IF "normal" check.
      aGroupError <- drawNormalValues(groupDataFrame$groupSize[i],groupDataFrame$errorDistributionParam1[i],groupDataFrame$errorDistributionParam2[i])    # Draw the values from the normal()
    }
    voterDataFrame$ei[voterDataFrame$groupID==i] <- aGroupError # Now place these utilities in the data frame
  } # Ends the ei draw loop
  voterDataFrame
}
codeForReviewer/kMajorityRule documentation built on May 13, 2019, 8:47 p.m.