背景:我提出以下问题的最终目标是将以下内容转化为函数.简而言之,我有一条曲线放在多个同心椭圆(椭圆形)上方.核心问题是如何匹配这些椭圆上方的曲线,因此如果曲线发生变化,则椭圆会相应地改变.
细节:
更具体地说,我希望有11个同心椭圆(见下图).在这11个椭圆的外边缘,我需要13个线段才能连接到曲线(见下图).下面显示的曲线是Cauchy分布(参见图像下方的R代码的最后一行).但是该曲线可以是任何其他对称曲线(例如,正态分布,t分布).
题:
如何确定(匹配)椭圆的坐标与必须精确连接到椭圆外边缘的线段?
注意:很可能,问题与省略号后面的数学有关.
(我已经尝试了一些东西并在下面提供了带注释的R代码).
这是我的R代码:
if(!require(library(plotrix))){install.packages('plotrix') }
library(plotrix) ## A package for drawing ellipses ##
# In the "plotrix":
# "x" and "y" are the coordinates of the center.
# "a" and "b" give the radii of the ovals.
plot(1, ty='n', ann = F, xlim = c(-4, 6), ylim = c(-3, 1.5) ) ## A platform for ellipses
draw.ellipse(x = rep(1, 11), y = rep(-1.2, 11),
a = seq(1, …
Run Code Online (Sandbox Code Playgroud) 在我下面的图中,我有两个单独的data
(dat
和dat2
)源,用于两个不同的geom_smooth()
调用,生成黑色和红色回归线(见下图)。
是否可以手动添加另一个legend
显示调用黑线"Between"
和调用红线的方法"Within"
?
library(tidyverse)
dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/cw2.csv')
dat$groups <- factor(dat$groups)
dat2 <- dat %>% group_by(groups) %>% summarize(mean_x = mean(x),
mean_y = mean(y),
.groups = 'drop')
dat %>%
ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse(level = .6) +
geom_point(data = dat2,
mapping = aes(x = mean_x, y = mean_y,fill = factor(groups)),
size = 4, show.legend = F,shape=21) …
Run Code Online (Sandbox Code Playgroud) 使用Base R,我想知道我是否可以确定曲线posterior
下面95%的面积如下所示?
更具体地说,我想从mode
(绿色虚线)向尾部移动,然后当我覆盖95%的曲线区域时停止.所需的x轴值是这95%面积的极限,如下图所示?
prior = function(x) dbeta(x, 15.566, 7.051)
likelihood = function(x) dbinom(55, 100, x)
posterior = function(x) prior(x)*likelihood(x)
mode = optimize(posterior, interval = c(0, 1), maximum = TRUE, tol = 1e-12)[[1]]
curve(posterior, n = 1e4)
Run Code Online (Sandbox Code Playgroud)
PS换句话说,非常希望这样的间隔是可能的最短95%间隔.
我是 R 包装新手mice
。但我正在尝试从中估算 5 个数据集popmis
,然后为每个数据集拟合一个模型,最后在它们之间拟合一个lmer()
模型。with()
pool()
我认为pool()
中的函数mice()
不适用于lmer()
来自lme4
包的调用,对吧?
如果是这种情况,有没有办法编写一个定制的函数,其作用类似于pool()
我下面的案例?
library(mice)
library(lme4)
imp <- mice(popmis, m = 5) # `popmis` is a dataset from `mice`
fit <- with(imp, lme4::lmer(popular ~ sex + (1|school))) # works fine.
pool(fit) # BUT this one fails, should I loop here?
Run Code Online (Sandbox Code Playgroud) 显然,mtext()
在R中不支持srt
其作业是旋转一段文本的参数.
我需要在我的移动绘图的第4面上mtext()
创建一个轴标题(即,要绘制的值来自一个函数,因此它们会改变,绘图轴值也会改变).我想知道的话,我有什么选择,旋转180度这个侧4轴标题?
下面是一个例子:
curve(dnorm(x),-3,3)
mtext("Strength",side=4,srt=180)
Run Code Online (Sandbox Code Playgroud) 我在 R 图中有一个“曲线”和一个直立定位的“箭头”(请参阅下面的 R 代码)。
我想知道是否有可能在“图例”中显示一个实际的箭头而不是一条平滑的直线来区分我的箭头和曲线?
这是我的 R 代码:
curve(dnorm(x), -4, 4, lwd = 3)
arrows(2, 0, 2, .3, code = 2, lwd = 3, col = 'red4')
legend("topleft", legend = c("Curve", "Arrow"), lwd = 3, col = c(1, "red4"))
Run Code Online (Sandbox Code Playgroud) 背景:
通常,R 给出众所周知的分布的分位数。在这些分位数中,较低的 2.5% 到较高的 97.5% 覆盖了这些分布下 95% 的面积。
问题:
假设我有一个 F 分布(df1 = 10,df2 = 90)。在 R 中,如何确定此分布下的 95% 区域,使得该 95% 仅覆盖高密度区域,而不是 R 通常给出的 95%(请参阅下面的 R 代码)?
注意:显然,最高密度是“模式”(下图中的虚线)。所以我想,人们必须从“模式”转向尾部。
这是我的 R 代码:
curve(df(x, 10, 90), 0, 3, ylab = 'Density', xlab = 'F value', lwd = 3)
Mode = ( (10 - 2) / 10 ) * ( 90 / (90 + 2) )
abline(v = Mode, lty = 2)
CI = qf( c(.025, .975), 10, …
Run Code Online (Sandbox Code Playgroud) 我一直认为set.seed()
只让随机变量生成器(例如,rnorm
)为任何特定的输入值集生成唯一的序列。
但是,我想知道,为什么当我们设置set.seed()
, 然后函数sample()
不能正确完成它的工作?
具体来说,鉴于下面的例子,有没有一种方法可以set.seed
在之前使用,rnorm
但如果多次运行,sample
仍然会从中产生新的随机样本?rnorm
sample
set.seed(123458)
x.y = rnorm(1e2)
sampled = sample(x = x.y, size = 20, replace = TRUE)
plot(sampled)
Run Code Online (Sandbox Code Playgroud) 在ggplot
下面的内容中,我尝试更改facet_wrap
使用的 10 个方面标签labeller(sch.id=paste0("sch.id:", unique(ten$sch.id)))
。
然而,该图显示NA
而不是正确的方面标签,我想知道修复是什么?
library(ggplot2)
hsb <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/hsb.csv')
ten <- subset(hsb, sch.id %in% unique(sch.id)[1:10])
p <- ten %>% ggplot() + aes(ses, math) + geom_point() +
facet_wrap(~sch.id) + geom_smooth(method = "lm", se = FALSE)
p + facet_wrap(~sch.id, labeller = labeller(sch.id=paste0("sch.id:", unique(ten$sch.id)))) ## HERE ##
Run Code Online (Sandbox Code Playgroud) 我想知道如何让R告诉我SD(作为R中构建的qnorm()中的参数 )对于正常分布,其95%的极限值已知?
举个例子,我知道我的法线的两个95%极限值分别是158和168.因此,在 下面的R代码中, SD显示为"x".如果 "y"(这个简单的qnorm()函数的答案)需要(158,168),那么 R能告诉我x应该是什么吗?
y <- qnorm(c(.025,.975), 163, x)
Run Code Online (Sandbox Code Playgroud) r ×10
plot ×5
distribution ×2
ggplot2 ×2
statistics ×2
bayesian ×1
dataframe ×1
function ×1
imputation ×1
integrate ×1
lme4 ×1
missing-data ×1
optimization ×1
quantile ×1
r-mice ×1
random ×1
resampling ×1
sampling ×1
tidyverse ×1