我是一名动物行为学的硕士生,R语言的初学者。在为我的论文做统计分析时,我决定在使用PSPP之前尝试一下R,并参加了一个小型的R语言入门课程,使用了R Studio。
我写了一个非常基础的脚本,其中:
- 从csv文件中导入了51个个体的数据,并且还导入了一个csv文件来存储结果。
- 创建了一个子集,包含一个个体的数据,然后根据数据提出假设,运行二项检验并判断假设是否被拒绝。每个个体的假设、二项检验的p值以及假设的接受与否都被存储在结果文件中。
当我为每个个体运行它时,这个脚本是完全可用的。我很抱歉代码质量不好,但如果您想查看,请看这里的一个个体脚本:
## Import data and results file
odor_behavior <- read.csv("odor_behavior.csv")
percalf_binomial_odor_results <- read.csv("percalf_binomial_odor_results.csv")
# Create binomial dataset per calf (example here: 958)
pref958 <- subset(odor_behavior, Calf_ID == 958, c("Preference"), drop=FALSE)
# Count the number of testing sessions for this calf
n958 <- nrow(pref958)
# Calculate how many times a suckling behavior appears (Control=1 or Modified=3).
mat958 <- as.matrix(pref958)
fmat958 <- factor(mat958,levels=c(1,3),ordered=TRUE)
fpref958 <- table(fmat958)
data958 <- as.data.frame(fpref958)
# Hypothesize a preference according to the observations and save it.
# Also, save variables for the binomial test.
## Find the row in which to store the results
row958<- which(percalf_binomial_odor_results$Calf_ID == 958)
## Decide the hypothesis.
if (data958[1,2] > data958[2,2]) {
x958 <- data958[1,2]
percalf_binomial_odor_results[row958,c("H_preference")] <- "Control"
} else if (data958[1,2] < data958[2,2]) {
x958 <- data958[2,2]
percalf_binomial_odor_results[row958,c("H_preference")] <- "Odor"
} else if (data958[1,2] == data958[2,2]) {
x958 <- data958[1,2]
percalf_binomial_odor_results[row958,c("H_preference")] <- "Either"
} else {print("Check your data, Maria!")
}
# Run binomial test
binom.test (x=x958, n=n958, p=0.5, conf.level = 0.95)
# Save the p.value and decide whether the hypothesized preference is real or not
pbin958 <- binom.test (x=x958, n=n958, p=0.5, conf.level = 0.95)$p.value
percalf_binomial_odor_results[row958,c("Bin_odor_pvalue")] <- pbin958
if (pbin958 <0.05) {
percalf_binomial_odor_results[row958,c("Real_preference")] <- "Yes"
} else {
percalf_binomial_odor_results[row958,c("Real_preference")] <- "No"
}
如上所述,当我复制并粘贴代码并手动更改“Calf_ID”五十次时,每个人的脚本都能顺利运行。
问题:是否可能让脚本自动运行每个人?我尝试通过将所有51个“Calf_ID”放入矩阵中,并将脚本放入使用矩阵元素依次处理每个个体的函数中来实现。
Lapply返回正确的结果,但仅涉及假设是否被接受/拒绝(即脚本的最后一部分),这使我认为脚本确实为所有51个个体运行。然而,在过程中没有将任何结果存储在相应的文件中(很可能是因为我一开始就做错了某些事情)。当我进行手动复制-粘贴/编辑过程时,所有结果(每个个体的假设、p值和假设结果)都会正确地存储在结果文件中。
我能够通过复制-粘贴方法获得我的结果;现在只是一个效率问题,因为我有51个个体,但如果有一千个该怎么办?知道如何使它工作将是非常好的!
编辑: 根据评论的要求,这里是对958号个体运行脚本的结果:
dput(head(odor_behavior)):
structure(list(Calf_ID = c(958L, 958L, 958L, 958L, 958L, 958L), Preference = c(1L, 1L, 3L, 1L, 1L, 3L)), .Names = c("Calf_ID", "Preference"), row.names = c(NA, 6L), class = "data.frame")
和
dput(head(percalf_binomial_odor_results)):
structure(list(Calf_ID = c(958L, 7015L, 7017L, 959L, 7018L, 7019L), H_preference = c("Control", NA, NA, NA, NA, NA), Bin_odor_pvalue = c(0.453125, NA, NA, NA, NA, NA), Real_preference = c("No", NA, NA, NA, NA, NA)), .Names = c("Calf_ID", "H_preference", "Bin_odor_pvalue", "Real_preference"), row.names = c(NA, 6L), class = "data.frame")
更新
这里有另一个关于同一主题的例子,基于@alexis_laz为初始脚本提供的答案。在这种情况下,我正在导入一个由3列(Calf_ID、Control_time、Odor_time)组成的数据集,其中包括给定Calf ID的多个控制和气味时间测量值(就像在初始示例中的偏好一样)。该脚本尝试为每个Calf_ID的Control_time - Odor_time对进行Wilcoxon符号秩检验。以下是脚本的外观:
### Import data and load library with test
odor_times <- read.csv("odor_times.csv")
library(coin)
### Create a function that examines suckling preference
prefanalysis <- function(Control_time, Odor_time)
{
sessions <- length(Control_time)
#Run a Wilcoxon signed rank test. Set y ~ x as control and odor,
mypvalue <- pvalue(wilcoxsign_test(Control_time ~ Odor_time))
myzvalue <- statistic(wilcoxsign_test(Control_time ~ Odor_time))
#Decide if there is a true difference in suckling times according to p value, and save it
true_diff <- if(mypvalue < 0.05) "Yes" else "No"
# Create a data frame with hypothesis, p value and real preference
data.frame(sessions, myzvalue, mypvalue, true_diff, stringsAsFactors = FALSE)
}
### Run the function for all calves, with preference depending on calf ID. Save results.
results <- as.data.frame(do.call(data.frame, aggregate(cbind(Control_time, Odor_time) ~ Calf_ID, odor_times, prefanalysis)))
在这种情况下,我收到以下错误信息:
Error in eval(expr, envir, enclos) :
argument "Odor_time" is missing, with no default
当使用调试模式重新运行时,我可以看到脚本开始正确读取第一个个体(即具有特定Calf_ID的行)的Control_time。然而,它似乎无法对Odor_time执行相同操作。我无法理解自己在做什么错误的事情。
dput(head(odor_behavior))
和dput(head(percalf_binomial_odor_results))
的结果([编辑]您的问题以添加它们)。 - Tensibaif <- function(id) {# 在这里编写代码,将“958”替换为“id”}
,最后一行就是返回的内容,所以也许对于你来说它是list(p.value = pbin958, results = percalf_binomial_odor_results)
。然后使用lapply
。 - rawraggregate(Preference ~ Calf_ID, odor_behavior, function(x) table(factor(x, levels = c(1, 3), labels = c("control", "modified"))))
-- 也就是说,你通过分组应用了一个函数。因此,你可以构建一个函数 - 比如说f
- 它接受“Preferences”并返回结果,并将其用作aggregate(Preference ~ ID, odor_behavior, f)
或任何类似的分组函数(例如tapply
、by
、lapply(split(what, by), f)
)或具有这种功能的软件包。 - alexis_laz