3
$\begingroup$

Say I have below example data, where rows are observations and columns are variables, and NAs stand for missing values.

 1  2 NA  4  5 6 14 5  2
 6 13  7  1 11 4 NA 9  6
15 12  3 12 NA 8  3 7 12
 8  1 NA  7  8 9  4 6  1

I want to impute the missing values by regression (I know I can impute by means, but I need to see how regression performs). There is a CRAN package named 'Amelia' for imputation by regression, but it gives an error for above data saying that #observations is smaller than #variables. 'mi' package also gives an error. I can code myself, but I do not want to reinvent the wheel since I am sure there is already a package for that which would work faster than the one I write (Speed is important since I will run this imputation for thousands of variables and hundreds of observations with lots of missing values). So, does anybody know about a package which would impute the values above by regression? Thanks.

$\endgroup$

closed as off-topic by Nick Cox, gung, John, Juho Kokkala, Peter Flom Nov 3 '16 at 12:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Nick Cox, gung, John, Juho Kokkala, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ regressionImp in package VIM $\endgroup$ – rcs Jun 2 '14 at 10:59
  • 3
    $\begingroup$ Also give the R Hmisc package aregImpute function a try. But your sample size for testing all this is not adequate for any analysis. $\endgroup$ – Frank Harrell Jun 2 '14 at 11:44
  • $\begingroup$ Amelia gave me the best results: gking.harvard.edu/amelia $\endgroup$ – t0x1n Oct 18 '14 at 12:24
  • 1
    $\begingroup$ You cannot use Amelia where the number of observations is smaller than the number of variables, as in the example above (#variables=9 and #observations=4). $\endgroup$ – user5054 Oct 19 '14 at 20:28
8
$\begingroup$

Even though this thread is a bit old, I am sure some people are still trying to find a solution in this thread. Therefore I want to add an example how you could use the mice package for regression imputation:

library("mice")

# Example data
data <- data.frame(x1 = c(1, 6, 15, 8, 5, 1, 7, 4),
                   x2 = c(2, 13, 12, 1, 6, 6, 1, 2),
                   x3 = c(NA, 7, 3, NA, 1, 2, 7, 3),
                   x4 = c(4, 1, 12, 7, 12, 1, 6, 6),
                   x5 = c(5, 11, NA, 8, 8, 11, 5, 6),
                   x6 = c(6, 4, 8, 9, 3, 9, 6, 12),
                   x7 = c(14, NA, 3, 4, 12, 5, 10, 10),
                   x8 = c(5, 9, 7, 6, 12, 2, 6, 3),
                   x9 = c(2, 6, 12, 1, 2, 2, 7, 1))

# Deterministic regression imputation via mice
imp <- mice(data, method = "norm.predict", m = 1)

# Store data
data_imp <- complete(imp)

With a larger dataset, You could also add a stochastic error term to the imputed values with the "norm.nob" method:

# Stochastic regression imputation
imp <- mice(data, method = "norm.nob", m = 1)

You can find further information on regression imputation in the following two links:

Deterministic vs. stochastic regression imputation & examples in R

Flexible Imputation of Missing Data of Stef van Buuren, the author of mice

$\endgroup$
  • $\begingroup$ I don't think this method is deterministic. Try: complete(mice(nhanes, method='norm.predict', m=1, seed=123)) == complete(mice(nhanes, method='norm.predict', m=1, seed=456)) $\endgroup$ – AdamO Nov 17 '17 at 19:06
  • $\begingroup$ @AdamO This difference results from the chained equations approach of mice, i.e. in the forefront of the deterministic imputation missing values are replaced by a random draw from your data. The imputation that is conducted based on this filled data is completely deterministic. If you want to keep the starting data fixed, you can use the argument data.init. See also this thread for more details. $\endgroup$ – JSP Nov 24 '17 at 8:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.