Skip to content

Instantly share code, notes, and snippets.

View RanaivosonHerimanitra's full-sized avatar
🏠
Working from home

Ranaivoson RanaivosonHerimanitra

🏠
Working from home
  • Québec
View GitHub Profile
@RanaivosonHerimanitra
RanaivosonHerimanitra / impute.cpp
Last active January 4, 2016 02:29
This code written with the 'Rcpp' package replaces NA's values of numeric/integer columns by their mean. It's designed to work with big dataframe with thousand of columns and rows.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List modiframe(DataFrame& df ) {
//nrow and ncol of the dataframe:
int nrow = df.nrows(), ncol= df.size() ;
double moy(0);
//define an empty list~dataframe
@RanaivosonHerimanitra
RanaivosonHerimanitra / GetSampleO-SRS.R
Last active January 3, 2016 16:39
After the determination of the sample size of each Strata, this function built with 'data.table' package samples units.
require(data.table)
mysample=alloc_opti_data.table()
get_echantillon_data.table=function(
dataset=yourDataset,
alea=1435,
set.alea=TRUE,
strate="strate2013",
eff=mysample[,list(strate2013,nh=round(nh))])
{
if (set.alea==TRUE) {
@RanaivosonHerimanitra
RanaivosonHerimanitra / StratifiedRandomSampling.R
Last active January 3, 2016 13:29
built with 'data.table' package, this function aims to determine the number of sample to be surveyed in each strata using Optimal Stratified Random Sampling ( à la Neyman). Inputs include the raw dataset that contains information about strata, the name of the strata variable in this dataset and another dataset containing standard deviation of a …
require(data.table)
alloc_opti_data.table=function(n=1000
,dataset=ese_op2013_reste
,strate="strate2013"
,sd=ecart_type )
{
#count obs per strata to form Nh:
dataset=data.table(dataset)
output=dataset[,.N,by=strate]