mina mina - 2 months ago 16
R Question

replace NA by truncated normal distribution values in r

I am trying to replace NAs by truncated normal distribution values.
First I used

sample
as follows and the function worked:

v.new <- replace(vector,v, sample(8,length(v),replace =FALSE))


However when I try to use
rtnorm
it seems not to work. I got any error messages and it takes ages to replace the NAs by the desired interval. Any suggestion to make this work?

library(msm)

# Some data
data("airquality")
airquality$Ozone

# My function
add.trunc.to.NAvector <- function(vector){
v <- NULL
for(i in 1:length(vector)){
if(is.na(vector[i])==TRUE)
v <- append(v, i)
}
mean.val <- mean(vector)
sd.val <- sd(vector)
min.val <- mean.val - 4 * sd.val
max.val <- mean.val + 4 * sd.val
v.new <- replace(vector,v, rtnorm(length(v), lower = min.val, upper = max.val))


return(v.new)
}

Answer

Should not this work?

v <- airquality$Ozone
v.new <- v
indices <- which(is.na(v))
m <- mean(v[-indices])
s <- sd(v[-indices])
v.new[indices] <- rtnorm(length(indices), lower = m-4*s, upper = m+4*s)
Comments