微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 错误:“错误:无法组合 `..1` <character> 和 `..2` <double>”

如何解决R 错误:“错误:无法组合 `..1` <character> 和 `..2` <double>”

我正在开展一个研究项目并尝试可视化 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)

当我尝试运行“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

解决方法

似乎是避风港的一个奇怪问题?试试 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)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。