Error in mice package when trying to use subset of variables for imputation model

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Error in mice package when trying to use subset of variables for imputation model

ivmcphail
Hello,



I am attempting to create a multiple imputation model using the mice
package in R. Here are some details about what I am specifically trying to
do, and below is a subset of the data I am working with, the code I have
tried, and the error that I am getting.



In the dataset, labelled 'mturk.all', there are ~300 variables and around
1300 cases. For the multiple imputation model, I am trying to use only 33
of the ~300 variables; these 33 variables are dichotomous (coded as 0 and 1
in the dataset).



Following the mice code provided at
https://stefvanbuuren.name/fimd/sec-toomany.html (see section 9.1.6 on the
linked website), I have tried the following code, which resulted in an
error (also shown below):





```

>library(mice)

>pred <- quickpred(mturk.all, mincor = .1, minpuc = 0, inc=c("TSHS_1R",
"TSHS_2R", "TSHS_3R", "TSHS_4R", "TSHS_5R", "TSHS_6R", "TSHS_7R",
"TSHS_8R", "TSHS_9R", "TSHS_10R", "TSHS_11R", "TSHS_12R", "TSHS_13R",
"TSHS_14R", "TSHS_15R", "TSHS_16R", "TSHS_17R", "TSHS_18R", "TSHS_19R",
"TSHS_20R", "TSHS_21R", "TSHS_22R", "TSHS_23R", "TSHS_24R", "TSHS_25R",
"TSHS_26R", "TSHS_27R", "TSHS_28R", "TSHS_29R", "TSHS_30R", "TSHS_31R",
"TSHS_32R", "TSHS_33R"))

There were 14 warnings (use warnings() to see them)

> warnings()

Warning messages:

1: In data.matrix(data) : NAs introduced by coercion

2: In data.matrix(data) : NAs introduced by coercion

3: In data.matrix(data) : NAs introduced by coercion

4: In data.matrix(data) : NAs introduced by coercion

5: In data.matrix(data) : NAs introduced by coercion

6: In data.matrix(data) : NAs introduced by coercion

7: In data.matrix(data) : NAs introduced by coercion

8: In data.matrix(data) : NAs introduced by coercion

9: In data.matrix(data) : NAs introduced by coercion

10: In data.matrix(data) : NAs introduced by coercion

11: In data.matrix(data) : NAs introduced by coercion

12: In data.matrix(data) : NAs introduced by coercion

13: In data.matrix(data) : NAs introduced by coercion

14: In data.matrix(data) : NAs introduced by coercion

>mturk.all.imp <- mice(mturk.all, m = 40, method = 'logreg', pred = pred)

Error in parse(text = x, keep.source = FALSE) :

  <text>:1:1: unexpected '<'

1: <

    ^

```



Alternatively, I tried the following:



```

>inlist <- mturk.all[c("TSHS_1R", "TSHS_2R", "TSHS_3R", "TSHS_4R",
"TSHS_5R", "TSHS_6R", "TSHS_7R", "TSHS_8R", "TSHS_9R", "TSHS_10R",
"TSHS_11R", "TSHS_12R", "TSHS_13R", "TSHS_14R", "TSHS_15R", "TSHS_16R",
"TSHS_17R", "TSHS_18R", "TSHS_19R", "TSHS_20R", "TSHS_21R", "TSHS_22R",
"TSHS_23R", "TSHS_24R", "TSHS_25R", "TSHS_26R", "TSHS_27R", "TSHS_28R",
"TSHS_29R", "TSHS_30R", "TSHS_31R", "TSHS_32R", "TSHS_33R")]

>pred <- quickpred(mturk.all, mincor = .1, minpuc = 0, inc=inlist)

>mturk.all.imp <- mice(mturk.all, m = 40, method = 'logreg', pred = pred)

Error in parse(text = x, keep.source = FALSE) :

  <text>:1:1: unexpected '<'

1: <

    ^

```



I have also switched out the 'logreg' imputation method with 'pmm' and
received the same error message.



Here is a subset of the dataset, mturk.all, and the version of R Studio I
am using, plus the version of mice I am using.



```

> dput(mturk.all[425:434, 1:33])

structure(list(TSHS_1R = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 1), TSHS_2R = c(0,

0, 0, 1, 0, 0, 0, 0, 0, 0), TSHS_3R = c(0, 1, 0, 0, 0, 0, 0,

0, 1, 0), TSHS_4R = c(0, 1, 0, 0, 0, 0, 1, 0, 1, 0), TSHS_5R = c(0,

0, 0, 1, 0, 0, 0, 0, 1, 0), TSHS_6R = c(0, 0, NA, NA, 0, 0, 1,

0, 0, 0), TSHS_7R = c(0, 0, 0, 1, 0, 1, 1, 0, 0, 0), TSHS_8R = c(1,

1, 0, 0, 1, 1, 1, 1, 0, 1), TSHS_9R = c(0, 0, 0, 0, 0, 0, 0,

1, 1, 0), TSHS_10R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_11R = c(0,

0, NA, 0, 0, 0, 0, 1, 0, 0), TSHS_12R = c(1, 0, 1, 0, 1, 0, 0,

0, 0, 0), TSHS_13R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), TSHS_14R = c(0,

0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_15R = c(0, 0, 1, 0, 0, 0, 0,

0, 0, 0), TSHS_16R = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0), TSHS_17R = c(0,

0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_18R = c(0, 0, 0, 0, 1, 0, 0,

0, 0, 0), TSHS_19R = c(0, 0, 0, NA, 0, 0, 0, 0, 0, 0), TSHS_20R = c(0,

0, 0, 1, 0, 0, 0, 0, 0, 0), TSHS_21R = c(0, 0, 0, 0, 1, 0, 0,

0, 0, 0), TSHS_22R = c(0, 1, 0, 0, NA, 1, 0, 0, 0, 0), TSHS_23R = c(0,

0, 0, 0, NA, 1, 0, 0, 0, 1), TSHS_24R = c(0, 0, 0, 1, NA, 1,

0, 1, 1, 0), TSHS_25R = c(1, 1, 1, 0, 1, 1, 1, 1, 1, 1), TSHS_26R = c(1,

0, 1, 0, 0, 0, 0, 1, 0, 1), TSHS_27R = c(1, 0, 0, 1, 0, 1, 1,

0, 0, 1), TSHS_28R = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0), TSHS_29R = c(1,

0, 0, 0, 1, 0, 0, 0, 0, 0), TSHS_30R = c(0, 0, 0, 0, 0, 0, 0,

0, 0, 0), TSHS_31R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_32R = c(0,

0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_33R = c(0, 0, 0, 0, 0, 0, 0,

0, 0, 0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",

"data.frame"))

> rstudioapi::versionInfo()

$`citation`



To cite RStudio in publications use:



  RStudio Team (2018). RStudio: Integrated Development for R. RStudio,
Inc., Boston, MA URL http://www.rstudio.com/.



A BibTeX entry for LaTeX users is



  @Manual{,

    title = {RStudio: Integrated Development Environment for R},

    author = {{RStudio Team}},

    organization = {RStudio, Inc.},

    address = {Boston, MA},

    year = {2018},

    url = {http://www.rstudio.com/},

  }





$`mode`

[1] "desktop"



$version

[1] ‘1.2.1335’



> packageVersion("mice")

[1] ‘3.8.0’

```



Any help identifying what I am doing wrong in the code would be appreciated!



Ian

--
Ian McPhail, M.A.
Clinical Psychology Graduate Student
Department of Psychology, University of Saskatchewan

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Error in mice package when trying to use subset of variables for imputation model

John Kane-3
I wonder if you have a non-ascii symbol in there somewhere? That may be
what this is trying to tell you.

Error in parse(text = x, keep.source = FALSE) :

  <text>:1:1: unexpected '<'


I have no dea what "mice" is supposed to output but using your data,
renamed dat1,
I did

library(mice)
pred <- quickpred(dat1, mincor = .1, minpuc = 0, inc=inlist)

and got
         TSHS_1R TSHS_2R TSHS_3R TSHS_4R TSHS_5R TSHS_6R TSHS_7R TSHS_8R
TSHS_1R        0       0       0       0       0       0       0       0
TSHS_2R        0       0       0       0       0       0       0       0
TSHS_3R        0       0       0       0       0       0       0       0
TSHS_4R        0       0       0       0       0       0       0       0
TSHS_5R        0       0       0       0       0       0       0       0
TSHS_6R        1       1       1       1       1       0       1       1
TSHS_7R        0       0       0       0       0       0       0       0
TSHS_8R        0       0       0       0       0       0       0       0
TSHS_9R        0       0       0       0       0       0       0       0
TSHS_10R       0       0       0       0       0       0       0       0
TSHS_11R       1       1       1       1       1       1       1       1
TSHS_12R       0       0       0       0       0       0       0       0
TSHS_13R       0       0       0       0       0       0       0       0
TSHS_14R       0       0       0       0       0       0       0       0
TSHS_15R       0       0       0       0       0       0       0       0
TSHS_16R       0       0       0       0       0       0       0       0
TSHS_17R       0       0       0       0       0       0       0       0
TSHS_18R       0       0       0       0       0       0       0       0
TSHS_19R       1       1       1       1       1       0       1       1
TSHS_20R       0       0       0       0       0       0       0       0
TSHS_21R       0       0       0       0       0       0       0       0
TSHS_22R       1       1       1       1       1       1       1       1
TSHS_23R       1       1       1       1       1       1       1       1
TSHS_24R       1       1       1       1       1       1       1       1
TSHS_25R       0       0       0       0       0       0       0       0
TSHS_26R       0       0       0       0       0       0       0       0
TSHS_27R       0       0       0       0       0       0       0       0
TSHS_28R       0       0       0       0       0       0       0       0
TSHS_29R       0       0       0       0       0       0       0       0
TSHS_30R       0       0       0       0       0       0       0       0
etc.

On Tue, 21 Jul 2020 at 11:09, Ian McPhail <[hidden email]> wrote:

> Hello,
>
>
>
> I am attempting to create a multiple imputation model using the mice
> package in R. Here are some details about what I am specifically trying to
> do, and below is a subset of the data I am working with, the code I have
> tried, and the error that I am getting.
>
>
>
> In the dataset, labelled 'mturk.all', there are ~300 variables and around
> 1300 cases. For the multiple imputation model, I am trying to use only 33
> of the ~300 variables; these 33 variables are dichotomous (coded as 0 and 1
> in the dataset).
>
>
>
> Following the mice code provided at
> https://stefvanbuuren.name/fimd/sec-toomany.html (see section 9.1.6 on the
> linked website), I have tried the following code, which resulted in an
> error (also shown below):
>
>
>
>
>
> ```
>
> >library(mice)
>
> >pred <- quickpred(mturk.all, mincor = .1, minpuc = 0, inc=c("TSHS_1R",
> "TSHS_2R", "TSHS_3R", "TSHS_4R", "TSHS_5R", "TSHS_6R", "TSHS_7R",
> "TSHS_8R", "TSHS_9R", "TSHS_10R", "TSHS_11R", "TSHS_12R", "TSHS_13R",
> "TSHS_14R", "TSHS_15R", "TSHS_16R", "TSHS_17R", "TSHS_18R", "TSHS_19R",
> "TSHS_20R", "TSHS_21R", "TSHS_22R", "TSHS_23R", "TSHS_24R", "TSHS_25R",
> "TSHS_26R", "TSHS_27R", "TSHS_28R", "TSHS_29R", "TSHS_30R", "TSHS_31R",
> "TSHS_32R", "TSHS_33R"))
>
> There were 14 warnings (use warnings() to see them)
>
> > warnings()
>
> Warning messages:
>
> 1: In data.matrix(data) : NAs introduced by coercion
>
> 2: In data.matrix(data) : NAs introduced by coercion
>
> 3: In data.matrix(data) : NAs introduced by coercion
>
> 4: In data.matrix(data) : NAs introduced by coercion
>
> 5: In data.matrix(data) : NAs introduced by coercion
>
> 6: In data.matrix(data) : NAs introduced by coercion
>
> 7: In data.matrix(data) : NAs introduced by coercion
>
> 8: In data.matrix(data) : NAs introduced by coercion
>
> 9: In data.matrix(data) : NAs introduced by coercion
>
> 10: In data.matrix(data) : NAs introduced by coercion
>
> 11: In data.matrix(data) : NAs introduced by coercion
>
> 12: In data.matrix(data) : NAs introduced by coercion
>
> 13: In data.matrix(data) : NAs introduced by coercion
>
> 14: In data.matrix(data) : NAs introduced by coercion
>
> >mturk.all.imp <- mice(mturk.all, m = 40, method = 'logreg', pred = pred)
>
> Error in parse(text = x, keep.source = FALSE) :
>
>   <text>:1:1: unexpected '<'
>
> 1: <
>
>     ^
>
> ```
>
>
>
> Alternatively, I tried the following:
>
>
>
> ```
>
> >inlist <- mturk.all[c("TSHS_1R", "TSHS_2R", "TSHS_3R", "TSHS_4R",
> "TSHS_5R", "TSHS_6R", "TSHS_7R", "TSHS_8R", "TSHS_9R", "TSHS_10R",
> "TSHS_11R", "TSHS_12R", "TSHS_13R", "TSHS_14R", "TSHS_15R", "TSHS_16R",
> "TSHS_17R", "TSHS_18R", "TSHS_19R", "TSHS_20R", "TSHS_21R", "TSHS_22R",
> "TSHS_23R", "TSHS_24R", "TSHS_25R", "TSHS_26R", "TSHS_27R", "TSHS_28R",
> "TSHS_29R", "TSHS_30R", "TSHS_31R", "TSHS_32R", "TSHS_33R")]
>
> >pred <- quickpred(mturk.all, mincor = .1, minpuc = 0, inc=inlist)
>
> >mturk.all.imp <- mice(mturk.all, m = 40, method = 'logreg', pred = pred)
>
> Error in parse(text = x, keep.source = FALSE) :
>
>   <text>:1:1: unexpected '<'
>
> 1: <
>
>     ^
>
> ```
>
>
>
> I have also switched out the 'logreg' imputation method with 'pmm' and
> received the same error message.
>
>
>
> Here is a subset of the dataset, mturk.all, and the version of R Studio I
> am using, plus the version of mice I am using.
>
>
>
> ```
>
> > dput(mturk.all[425:434, 1:33])
>
> structure(list(TSHS_1R = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 1), TSHS_2R = c(0,
>
> 0, 0, 1, 0, 0, 0, 0, 0, 0), TSHS_3R = c(0, 1, 0, 0, 0, 0, 0,
>
> 0, 1, 0), TSHS_4R = c(0, 1, 0, 0, 0, 0, 1, 0, 1, 0), TSHS_5R = c(0,
>
> 0, 0, 1, 0, 0, 0, 0, 1, 0), TSHS_6R = c(0, 0, NA, NA, 0, 0, 1,
>
> 0, 0, 0), TSHS_7R = c(0, 0, 0, 1, 0, 1, 1, 0, 0, 0), TSHS_8R = c(1,
>
> 1, 0, 0, 1, 1, 1, 1, 0, 1), TSHS_9R = c(0, 0, 0, 0, 0, 0, 0,
>
> 1, 1, 0), TSHS_10R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_11R = c(0,
>
> 0, NA, 0, 0, 0, 0, 1, 0, 0), TSHS_12R = c(1, 0, 1, 0, 1, 0, 0,
>
> 0, 0, 0), TSHS_13R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1), TSHS_14R = c(0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_15R = c(0, 0, 1, 0, 0, 0, 0,
>
> 0, 0, 0), TSHS_16R = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0), TSHS_17R = c(0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_18R = c(0, 0, 0, 0, 1, 0, 0,
>
> 0, 0, 0), TSHS_19R = c(0, 0, 0, NA, 0, 0, 0, 0, 0, 0), TSHS_20R = c(0,
>
> 0, 0, 1, 0, 0, 0, 0, 0, 0), TSHS_21R = c(0, 0, 0, 0, 1, 0, 0,
>
> 0, 0, 0), TSHS_22R = c(0, 1, 0, 0, NA, 1, 0, 0, 0, 0), TSHS_23R = c(0,
>
> 0, 0, 0, NA, 1, 0, 0, 0, 1), TSHS_24R = c(0, 0, 0, 1, NA, 1,
>
> 0, 1, 1, 0), TSHS_25R = c(1, 1, 1, 0, 1, 1, 1, 1, 1, 1), TSHS_26R = c(1,
>
> 0, 1, 0, 0, 0, 0, 1, 0, 1), TSHS_27R = c(1, 0, 0, 1, 0, 1, 1,
>
> 0, 0, 1), TSHS_28R = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0), TSHS_29R = c(1,
>
> 0, 0, 0, 1, 0, 0, 0, 0, 0), TSHS_30R = c(0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0), TSHS_31R = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_32R = c(0,
>
> 0, 0, 0, 0, 0, 0, 0, 0, 0), TSHS_33R = c(0, 0, 0, 0, 0, 0, 0,
>
> 0, 0, 0)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
>
> "data.frame"))
>
> > rstudioapi::versionInfo()
>
> $`citation`
>
>
>
> To cite RStudio in publications use:
>
>
>
>   RStudio Team (2018). RStudio: Integrated Development for R. RStudio,
> Inc., Boston, MA URL http://www.rstudio.com/.
>
>
>
> A BibTeX entry for LaTeX users is
>
>
>
>   @Manual{,
>
>     title = {RStudio: Integrated Development Environment for R},
>
>     author = {{RStudio Team}},
>
>     organization = {RStudio, Inc.},
>
>     address = {Boston, MA},
>
>     year = {2018},
>
>     url = {http://www.rstudio.com/},
>
>   }
>
>
>
>
>
> $`mode`
>
> [1] "desktop"
>
>
>
> $version
>
> [1] ‘1.2.1335’
>
>
>
> > packageVersion("mice")
>
> [1] ‘3.8.0’
>
> ```
>
>
>
> Any help identifying what I am doing wrong in the code would be
> appreciated!
>
>
>
> Ian
>
> --
> Ian McPhail, M.A.
> Clinical Psychology Graduate Student
> Department of Psychology, University of Saskatchewan
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
John Kane
Kingston ON Canada

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.