R Error: "Error: Can't combine `..1` <character> and `..2` <double>."

12.8k views Asked by At

I am working on a research project and trying to visualize an OLS model. Here is my code:

#install.packages("dplyr")
library(foreign)
#install.packages("tidyverse", dependencies = TRUE)
library(tidyverse)
library(haven)
library(stargazer)
library(ggeffects)
library(gridExtra)

anes_timeseries_cdf <- read_dta("anes_timeseries_cdf.dta")
head(anes_timeseries_cdf)

dataset <- anes_timeseries_cdf %>% 
  filter(!is.na(VCF0114), !is.na(VCF0118), !is.na(VCF0140a), !is.na(VCF0146), !is.na(VCF0148), !is.na(VCF0301), !is.na(VCF0302), !is.na(VCF0310), VCF0310!=9, !is.na(VCF0703), !is.na(VCF0742), !is.na(VCF0803), !is.na(VCF0624)) %>%
  select(VCF0004, VCF0114, VCF0118, VCF0140a, VCF0146, VCF0148, VCF0301, VCF0302, VCF0310, VCF0703, VCF0742, VCF0803, VCF0624)
view(dataset)

test <- lm(VCF0114 ~ VCF0703 + VCF0803, data = dataset)

Registered <- ggpredict(test, terms = c("VCF0703"))
plot(Registered)

When I try to run 'plot(Registered)', I get the error

Error: Can't combine `..1` <character> and `..2` <double>.
Run `rlang::last_error()` to see where the error occurred.

And when I run rlang::last_error(), it says the problem is

<error/vctrs_error_incompatible_type>

Here is the link to my data so y'all can try reproducing it:

https://drive.google.com/file/d/1yKYtD6heBfZcL87YNJtzRcAF9eF4PiFd/view?usp=sharing

1

There are 1 answers

1
Daniel On

Seems to be a weird issue in haven? Try sjlabelled::read_stata() instead, then it works (for me):

library(tidyverse)
library(haven)
library(ggeffects)

anes_timeseries_cdf <- sjlabelled::read_stata("d:/Downloads/anes_timeseries_cdf.dta")

dataset <- anes_timeseries_cdf %>% 
  filter(!is.na(VCF0114), !is.na(VCF0118), !is.na(VCF0140a), !is.na(VCF0146), !is.na(VCF0148), !is.na(VCF0301), !is.na(VCF0302), !is.na(VCF0310), VCF0310!=9, !is.na(VCF0703), !is.na(VCF0742), !is.na(VCF0803), !is.na(VCF0624)) %>%
  select(VCF0004, VCF0114, VCF0118, VCF0140a, VCF0146, VCF0148, VCF0301, VCF0302, VCF0310, VCF0703, VCF0742, VCF0803, VCF0624)
view(dataset)

test <- lm(VCF0114 ~ VCF0703 + VCF0803, data = dataset)

Registered <- ggpredict(test, terms = c("VCF0703"))
plot(Registered)

enter image description here