Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

'R', 'mice', missing variable imputation - how to only do one column in sparse matrix

$
0
0

I have a matrix that is half-sparse. Half of all cells are blank (na) so when I try to run the 'mice' it tries to work on all of them. I'm only interested in a subset.

Question: In the following code, how do I make "mice" only operate on the first two columns? Is there a clean way to do this using row-lag or row-lead, so that the content of the previous row can help patch holes in the current row?

set.seed(1)

#domain
x <- seq(from=0,to=10,length.out=1000)

#ranges
y <- sin(x) +sin(x/2) + rnorm(n = length(x))
y2 <- sin(x) +sin(x/2) + rnorm(n = length(x))

#kill 50% of cells
idx_na1 <- sample(x=1:length(x),size = length(x)/2)
y[idx_na1] <- NA

#kill more cells
idx_na2 <- sample(x=1:length(x),size = length(x)/2)
y2[idx_na2] <- NA

#assemble base data
my_data <- data.frame(x,y,y2)

#make the rest of the data
for (i in 3:50){


     my_data[,i] <- rnorm(n = length(x))
     idx_na2 <- sample(x=1:length(x),size = length(x)/2)
     my_data[idx_na2,i] <- NA

}

#imputation
est <- mice(my_data)

data2 <- complete(est)

str(data2[,1:3])

Places that I have looked for answers:


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>