gpt4 book ai didi

r - 使用 geom_smooth 的最小绝对偏差

转载 作者:行者123 更新时间:2023-12-04 07:22:09 25 4
gpt4 key购买 nike

我有如下数据:

library(quantreg)
library(ggplot2)

data <- structure(list(country_mean_rep = structure(c(73.6995708154506,
93.5501285347044, 85.1529051987768, 91.1017369727047, 79.5562130177515,
84.6751054852321, 89.8, 86.8826405867971, 94.2247191011236, 70.2321428571429,
88.4107142857143), label = "label", format.stata = "%9.2f"),
country_mean_crime = c(0.0944206008583691, 0.0565552699228792,
0.0336391437308868, 0.205955334987593, 0.130177514792899,
0.282700421940928, 0.220512820512821, 0.415647921760391,
0.387640449438202, 0.200892857142857, 0.292207792207792),
country_name = structure(c(1L, 2L, 3L, 4L, 5L, 7L, 11L, 12L,
14L, 16L, 20L), .Label = c("Albania", "Armenia", "Azerbaijan",
"Belarus", "Bosnia and Herzegovina", "Brazil", "Bulgaria",
"Cambodia", "Chile", "CostaRica", "Croatia", "Czech", "Ecuador",
"Estonia", "FYROM", "Georgia", "Germany", "Greece", "Guyana",
"Hungary", "Ireland", "Kazakhstan", "Kenya", "Kyrgyzstan",
"Latvia", "Lithuania", "Malawi", "Mali", "Moldova", "Philippines",
"Poland", "Portugal", "Romania", "Russia", "Senegal", "Serbia&Montenegro",
"Slovakia", "Slovenia", "South Africa", "South Korea", "Spain",
"SriLanka", "Tajikistan", "Turkey", "Ukraine", "Uzbekistan",
"Vietnam"), class = "factor")), row.names = c(NA, -11L), class = c("data.table",
"data.frame"))

# On which I like to run the following code:

ggplot(data, aes(x=country_mean_rep, y=country_mean_crime)) +
geom_point() +
geom_smooth(aes(colour="linear", fill="linear"),
method="lm",
formula=y ~ x, ) +
labs(colour="Functional Form", fill="Functional Form") +
geom_text(aes(label=country_name), nudge_y=0.02) +
theme_bw()
我想添加一条使用最小绝对偏差的回归线,所以我尝试了:
ggplot(data, aes(x=country_mean_rep, y=country_mean_crime)) + 
geom_point() +
geom_smooth(aes(colour="linear", fill="linear"),
method="lm",
formula=y ~ x, ) +
geom_smooth(aes(colour="least absolute deviation", fill="least absolute deviation"),
method="qr",
formula=y ~ x, ) +
labs(colour="Functional Form", fill="Functional Form") +
geom_text(aes(label=country_name), nudge_y=0.02) +
theme_bw()
我收到以下错误:
Computation failed in `stat_smooth()`:
invalid first argument
谁能帮我解决这个问题?

最佳答案

您的尝试没有成功,因为您指定了“qr”作为 geom_smooth 的方法而不是“rq”,它为您提供最小绝对偏差的最佳拟合线。

ggplot(data, aes(x=country_mean_rep, y=country_mean_crime)) + 
geom_point() +
geom_smooth(aes(colour="Linear", fill="Linear"),
method="lm",
formula=y ~ x,
se = F) +
geom_smooth(aes(colour="Least Absolute Deviation", fill="Least Absolute Deviation"),
method="rq",
se = F,
formula = y ~ x) +
labs(colour="Functional Form", fill="Functional Form") +
geom_text(aes(label=country_name), nudge_y=0.02) +
theme_bw()
输出:
Linear and Least Absolute Deviation Output
此解决方案的警告是,要使其正常工作,您必须设置 se = F对于最小绝对偏差线,这意味着您无法绘制此模型的置信区间。如果愿意,您仍然可以拥有线性模型的置信区间;刚刚设置 se = T在相应的 geom_smooth 调用中。

关于r - 使用 geom_smooth 的最小绝对偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68426779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com