0

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

chemdork123
  • 12,369
  • 2
  • 16
  • 32
Andrew Liebman
  • 1
  • 1
  • 1
  • 2
  • 1
    It would be easier for others to help you if you provide a minimal reproducible example :) – user213544 Apr 16 '21 at 11:51
  • Sorry, I shortened the code to be easier to reproduce, and included the dataset. – Andrew Liebman Apr 16 '21 at 12:47
  • Please dont post links to data, and instead provide a sample of your data using `dput` - ie, `dput(head(dataset, 20))`. This is for both posterity (links become broken, etc) and getting better help (fewer people will click on links for security or other reasons) – jpsmith Dec 30 '22 at 05:05

1 Answers1

1

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

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • This worked for me, thanks Daniel. It is weird read_dta worked when I was not using any function but not with functions but sjlabelled::read_stata worked well regardless. – JourneyDS Jun 19 '23 at 02:19