get_value_to_impute_discrete
C++ Function Reference
1 Signature
int BigDataStatMeth::get_value_to_impute_discrete(std::map< double, double > probMap)2 Description
Generates a discrete value for imputation based on probability distribution.
3 Parameters
probMap(std::map< double, double >): Map containing value-frequency pairs
4 Returns
int Generated value for imputation
5 Details
This function takes a map of value-probability pairs and generates a random value according to the probability distribution, excluding NA values (3).
6 Caller Graph
7 Source Code
NoteImplementation
File: inst/include/hdf5Utilities/hdf5ImputeData.hpp • Lines 49-82
inline int get_value_to_impute_discrete(std::map<double, double> probMap)
{
try
{
std::vector <double> probs;
// Get values and counts for each map element
for( auto it = probMap.begin(); it != probMap.end(); ++it )
probs.push_back( it->second );
// remove last element (corresponds to 3=<NA>)
probs.erase(probs.end() - 1);
// Get total count
double totalSNPS = std::accumulate(probs.begin(), probs.end(), decltype(probs)::value_type(0));
// Get probabilities without <NA>
for (std::vector<double>::iterator it = probs.begin() ; it != probs.end(); ++it)
*it = *it/totalSNPS;
// Generate value with given probabilities
std::random_device rd;
std::mt19937 gen(rd());
std::discrete_distribution<> d(probs.begin(), probs.end());
return (d(gen));
} catch(const std::exception& e) {
Rf_error( "c++ exception get_value_to_impute_discrete : %s", e.what());
// std::cerr << e.what() << '\n';
return(3);
}
}8 Usage Example
#include "BigDataStatMeth.hpp"
// Example usage
auto result = get_value_to_impute_discrete(...);