我试图用纵向设计来估算数据集中的数据.有两个预测因子(实验组和时间)和一个结果变量(分数).聚类变量是id.
这是玩具数据
set.seed(345)
A0 <- rnorm(4,2,.5)
B0 <- rnorm(4,2+3,.5)
A1 <- rnorm(4,6,.5)
B1 <- rnorm(4,6+2,.5)
A2 <- rnorm(4,10,.5)
B2 <- rnorm(4,10+1,.5)
A3 <- rnorm(4,14,.5)
B3 <- rnorm(4,14+0,.5)
score <- c(A0,B0,A1,B1,A2,B2,A3,B3)
id <- rep(1:8,times = 4, length = 32)
time <- rep(0:3, each = 8, length = 32)
group <- rep(c("A","B"), times =2, each = 4, length = 32)
df <- data.frame(id = id, group = group, time = time, score = score)
# plots
(ggplot(df, aes(x = time, y = …
Run Code Online (Sandbox Code Playgroud) 我在工作流程方面遇到了问题,因为我在rmarkdown中采购了多个脚本,其中一些需要包dplyr
,其中一些使用plyr
.
问题是该rename
函数存在于两个包中,如果dplyr
当前连接,则该rename
函数plyr
将不起作用.
如何在我的脚本中包含一个检查是否dplyr
附加的函数,如果是,则将其分离?
我知道如何通过分离包detach("package:dplyr", unload = TRUE)
.我不知道的是如何检查是否附加了包裹.
我开始探索包boot()
中功能的局限性boot
。我刚刚弄清楚如何从一次调用中提取多个统计信息,但我不知道如何对它们进行索引。
bs <- function(formula, data, indices) {
d <- data[indices,] # allows boot to select sample
fit <- lm(formula, data=d)
return(c(coef(fit), summary(fit)$r.squared)) # four stats extracted
}
Run Code Online (Sandbox Code Playgroud)
现在引导 1000 次复制。
(results <- boot(data = mtcars, statistic = bs, R = 1000, formula = mpg ~ wt + disp))
Run Code Online (Sandbox Code Playgroud)
现在,如果我们绘制并尝试获得该对象的 95% CI,我们只能获得第一个统计数据,在本例中为模型的截距。
plot(results)
boot.ci(results, type="bca")
# BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
# Based on 1000 bootstrap replicates
#
# CALL :
# boot.ci(boot.out = results, type = "bca") …
Run Code Online (Sandbox Code Playgroud) 在这个关于交叉验证的优秀文章中,一个答案提到,纠正多重比较的置信区间相对容易。
我想知道这是否可以使用 R 中的 multcomp 包
下面是一个使用来自基础 R 的数据集的示例模型,这是一个从预测变量列表中mtcars
预测每加仑英里数 ( ) 的多元回归。mpg
set.seed(1234)
df <- mtcars
# which variables predict miles per gallon?
sumMod <- summary(mod <- lm(formula = mpg ~ cyl + disp + hp + drat + wt + qsec + vs + gear,
data = df))
Run Code Online (Sandbox Code Playgroud)
multcomp
现在使用该函数通过包运行模型glht
。
# adjust using multcomp
library(multcomp)
K <- length(coefficients(mod))
modGLHT <- glht(model = mod,
linfct = diag(K))
Run Code Online (Sandbox Code Playgroud)
您可以使用包函数中的参数选择multcomp
调整方法test = adjusted(type …
根据这篇文章中的建议,我试图将衬线字体(或字体“系列”)安装到 R 中,以便我可以将 ggplots 保存为 .eps 文件。尽管提供的建议有效,但我想尝试解决该问题以供将来使用。
这是生成问题的代码。
library(bayesplot)
df <- data.frame(xVar = rnorm(1e4,0,1), yVar = rnorm(1e4,2,1), zVar = rnorm(1e4,4,1))
t <- bayesplot::mcmc_trace(df)
t
Run Code Online (Sandbox Code Playgroud)
现在当我去保存这个数字时......
ggplot2::ggsave(filename = "tPlot.eps",
plot = t,
device = "eps",
dpi = 1200,
width = 15,
height = 10,
units = "cm")
Run Code Online (Sandbox Code Playgroud)
...它抛出错误
Error in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
family 'serif' not included in postscript() device
Run Code Online (Sandbox Code Playgroud)
在上一篇文章中,回答者建议我下载该extrafont
软件包。
我跑了
View(fonttable())
Run Code Online (Sandbox Code Playgroud)
但衬线字体似乎没有安装。
然后我尝试了
font_addpackage(pkg = "serif")
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
Error in font_addpackage(pkg = "serif") :
Unknown font …
Run Code Online (Sandbox Code Playgroud) 我需要使用希腊符号作为构面标签保存在 ggplot 中创建的图形。
这是图形的代码
library(bayesplot)
library(ggplot2)
df <- data.frame(var1 = rnorm(1e4, 0, 1),
var2 = rnorm(1e4, 6, 4),
var3 = rnorm(1e4, 10, 3))
names(df) <- c("\u03C3", "\u03B4", "\u03BC")
tp <- bayesplot::mcmc_trace(df)
tp
Run Code Online (Sandbox Code Playgroud)
...希腊符号很好地出现。它还可以很好地保存到图像文件中。但是如果我尝试保存到 eps 或 pdf ......
ggplot2::ggsave(filename = "plotMu.eps",
plot = tp,
device = "eps",
dpi = 1200,
width = 15,
height = 10,
units = "cm")
Run Code Online (Sandbox Code Playgroud)
...这些希腊符号以双点形式出现 ..
那么我如何让这些符号显示在 eps/pdf 上。我尝试通过编码和选择进行保存,ASCII
但这种策略仅适用于 plotmath 表达式和expression()
语句,这对我的情况没有真正帮助,因为这些都不适用于列标题。
我真的很感激有人概述了完成这项工作的过程。网上的所有解释都倾向于假设一些我没有的知识。
我想根据这些参数的先验来绘制来自 stan 模型的参数估计的直方图。我曾尝试通过在 stan 中运行模型,用 ggplot2 绘制它,然后使用 R 的随机生成器函数(例如rnorm()
,rbinom()
)覆盖先验分布的近似值来做到这一点,但我遇到了许多缩放问题,使图形无法查看对。
我在想一个更好的方法是直接从先验分布中采样,然后根据参数估计绘制这些样本,但是运行一个完整的单独模型只是为了从先验中采样似乎非常耗时。我想知道是否有一种方法可以在现有模型内或与之并行执行此操作。
这是一个示例脚本。
# simulate linear model
a <- 3 # intercept
b <- 2 # slope
# data
x <- rnorm(28, 0, 1)
eps <- rnorm(28, 0, 2)
y <- a + b*x + eps
# put data into list
data_reg <- list(N = 28, x = x, y = y)
# create the model string
ms <- "
data {
int<lower=0> N;
vector[N] x; …
Run Code Online (Sandbox Code Playgroud) 我正在尝试增加作为 grob 添加的第二个轴标题的字体大小(出于显而易见的原因)。这是一些要绘制图表的玩具数据
library(Hmisc)
library(dplyr)
# Plot power vs. n for various odds ratios
(n <- seq(10, 1000, by=10)) # candidate sample sizes
(OR <- as.numeric(sort(c(seq(1/0.90,1/0.13,length.out = 9), 2.9)))) # candidate odds ratios, spanning the 95% CI centered around what we got (OR=2.9)
alpha <- c(.001, .01, .05)
# put all of these into a dataset and calculate power
powerDF <- data.frame(expand.grid(OR, n, alpha)) %>%
rename(OR = Var1, num = Var2, alph = Var3) %>%
arrange(OR) %>%
mutate(power = as.numeric(bpower(p1=.29, …
Run Code Online (Sandbox Code Playgroud) 我确信答案很简单,但目前我却想不起来。我想使用 制作折线图stat_summary()
,在每个 x 轴刻度(代表单独的时间点)处,每个组(代表实验条件)具有不同的形状。
这是数据
set.seed(124)
ID <- rep(1:12, times = 3)
Group <- rep(c("A", "B", "C"), times = 12)
score <- rnorm(36, 25, 3)
session <- rep(c("s1","s2", "s3"), each = 12)
df <- data.frame(ID, Group, session, score)
Run Code Online (Sandbox Code Playgroud)
现在我可以通过为每个时间点制作一个均值表来实现这一目标。就像这样。
gMeans <- aggregate(score ~ session + Group, data = df, mean)
Run Code Online (Sandbox Code Playgroud)
然后像这样绘制它。
pMeans <- ggplot(data = gMeans, aes(x = session, y = score, group = Group, shape = Group)) +
geom_line(aes(linetype = Group), size = 1) +
geom_point(size = …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有任何简单的方法来指定summarise
in报告的位数dplyr
,理想情况下使用本机dplyr
或其他tidyverse
函数?
这是一些玩具数据
library(dplyr)
df <- data.frame(group = rep(letters[1:2], each = 10, length.out = 40),
large = rnorm(40, 100, 15),
small = rnorm(40, 0.5, 0.02))
Run Code Online (Sandbox Code Playgroud)
如果我们然后总结通过
df %>% group_by(group) %>% summarise(mL = mean(large), mS = mean(small))
Run Code Online (Sandbox Code Playgroud)
我们得到
# group mL mS
# <fct> <dbl> <dbl>
# 1 a 104. 0.496
# 2 b 97.6 0.506
Run Code Online (Sandbox Code Playgroud)
请注意,在没有指定任何舍入的情况下,具有较高平均值的变量已四舍五入到小数点后 1 位,而具有较小平均值的变量已四舍五入为 3。
现在是否希望将具有较大平均值的变量也报告到小数点后 3 位?如果我们包含一个像这样舍入的命令
df %>% group_by(group) %>% summarise(mL = round(mean(large),3), mS = mean(small))
Run Code Online (Sandbox Code Playgroud)
输出没有变化 …