0

我正在做一个研究项目并尝试可视化 OLS 模型。这是我的代码:

#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)

当我尝试运行“绘图(已注册)”时,出现错误

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

当我运行 rlang::last_error() 时,它说问题是

<error/vctrs_error_incompatible_type>

这是我的数据的链接,所以你们都可以尝试复制它:

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

4

1 回答 1

0

在避风港似乎是一个奇怪的问题?试试sjlabelled::read_stata()吧,然后它会起作用(对我来说):

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)

在此处输入图像描述

于 2021-04-25T07:21:40.953 回答