[R] using mean substitution

Michael Parent michael.parent at ufl.edu
Tue Oct 18 01:02:18 CEST 2011


That worked perfectly, thanks!!

I'd assumed that it wasn't necessary for me to type out "...based on a MV normal distribution that I made into 1-7 integers by blah blah blah"; the distribution is normal-ish after I transform it around for the purposes I need for the simulations. I'll be sure to be more accurate/specific in any future posts ;-) 

Thanks!
Mike

On Oct 17, 2011, at 6:24 PM, Rolf Turner wrote:

> On 18/10/11 10:35, Michael Parent wrote:
>> Hi, all,
>> 
>> I'm running a monte carlo simulation with missing data. The data are arranged such that there are k columns and n rows over a set number of simulations (set to 10 right now so it runs fast while I set everything up). The data are integers, numbers 1-7 only (normal distribution).
> 
>    For CRYING OUT LOUD.  This sort of blithering nonsense makes me
>    want to SCREAM!!!  The normal distribution is a continuous distribution.
>    It does not take on (exclusively) integer values.
> 
>> The simulations are set up and run without a hitch, including imposing NA missing values at a specified prevalence semi-randomly (there are not allowed to be any completely empty rows).
>> 
>> I'd like to replace the missing values ("NA") with the mean for the non-missing items items *on that row*.  I want to go through all the monte carlo simulation runs that I already did (so that I'm using the same data) and replace NA with the mean (e.g., if k=5 and a row has values of 3 3 NA 5 5, I want to put a 4 in for NA). I also want the imputed mean values to be rounded to the nearest integer.
>> 
>> Does anyone have an idea for how I'd set that up? I feel like there's a fairly easy way to set up searching out those NAs and replacing the the row mean that is not coming to me.
> 
> Let your matrix of values be "m".
> 
> rv <- round(apply(m,1,mean,na.rm=TRUE))
> ij <- which(is.na(m),arr.ind=TRUE)
> m[ij] <- rv[ij[,1]]
> 
>    cheers,
> 
>        Rolf Turner
> 



More information about the R-help mailing list