首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查R代码和此调试错误:"}“中意外的'}‘

检查R代码和此调试错误:"}“中意外的'}‘
EN

Stack Overflow用户
提问于 2022-05-26 21:24:54
回答 1查看 93关注 0票数 0

我只需要对我编写的一个非常简单的代码进行一些反馈,它还在说错误:意外的"}",我想知道这是否是unicode问题?如有任何反馈,将不胜感激!

我的数据在这里:joint.sexdiff.csv

我是一个非常新的编码器,而R是我想要用于未来的统计,所以我不断学习和欣赏所有的反馈!

代码语言:javascript
复制
# Statistical test pipeline when trying to compare two groups ----
# Description: When trying to compare two groups, t-test is often used. Unpaired two-samples t-test can be used only under certain conditions:
# (1) when the two groups of samples (A and B), being compared, are normally distributed
# This can be checked using Shapiro-Wilk test
# (2) When the variances of the two groups are equal
# This can be checked using F-test

# Given a sample dataframe, a pipeline will be used to automatically determine whether a parametric or non-parametric test should be used, and get the p-value

# Packages to install and call
install.packages("dplyr")
install.packages("ggplot2")
library(dplyr)
library(ggplot2)

# Sample data named "joint.sexdiff.csv"
data1<-read.csv("*path*")


# Some descriptive stats
group_by(data1, Sex) %>%
  summarise(
    count=n(),
    mean=mean(JS.W, na.rm=TRUE)
  )

# Data visualization using ggplot2
bplot<-ggplot(
  data1, aes(x=Sex, y=JS.W, colour=Sex))+
    geom_boxplot(outlier.shape = NA)+
    geom_jitter()+
    ylab("Joint space width (mm)")+
    xlab("")+
  ggtitle("Male vs Female joint space width (JSW)")
bplot
  
# Now we have the data we need, we can run a two group comparison
# Use Shapiro-Wilk test to test if the joint space widths fall within a normal distribution, if p>0.05, we can assume normality
# If the distribution is not normal, we need to use a non-parametric test

# Non-parametric unpaired two-samples Wilcoxon test is used to compare two independent groups of samples that are not normally distributed
# http://www.sthda.com/english/wiki/unpaired-two-samples-wilcoxon-test-in-r

# The process is as follows:
# Shapiro test -> if normal distribution or p>0.05, do F-test -> if variances are equal or p>0.05, var.equal=TRUE will be assumed on the t-test
# -> if variances are unequal or p<0.05, var.equal=FALSE will be assumed
# IMPORTANT -- your t-test p-values will change depending on whether you assume the variances to be equal or not
# Shapiro -> if distribution is not normally distributed or p<0.05, use Wilcoxon test

# The test blocks of code will check your data and run an independent samples t-test or Wilcoxon test depending on whether sample is normally distributed and variances are normal

# Output = p.value

shapJSW<-shapiro.test(data1$JS.W)
if (shapJSW$p.value>0.05){
  ftestJSW<-var.test(JS.W ~ Sex, data = data1)
  if (ftestJSW$p.value>0.05){
    ttestJSW<-t.test(JS.W ~ Sex, data = data1, var.equal = TRUE)
    print(ttestJSW$p.value)
  } else {
    ttestJSW<-t.test(JS.W ~ Sex, data = data1, var.equal = FALSE)
    print(ttestJSW$p.value)
} else {
  wtestJSW<-wilcox.test(JS.W ~ Sex, data = data1)
  print(wtestJSW)
  wtestJSW<-wilcox.test(JS.W ~ Sex, data = data1)
  print(wtestJSW)
}
}

把你的智慧撒给我!:<3

EN

回答 1

Stack Overflow用户

发布于 2022-05-27 02:41:00

此错误是打印具有不等方差的t检验的p值后缺少的} .测试部分可以重写如下:

代码语言:javascript
复制
test_sex <- function(d) {
  if(shapiro.test(d$JS.W)$p.value>0.05){
    equal_var = var.test(JS.W~Sex,data=d)$p.value>0.05
    ttestJSW = t.test(JS.W ~ Sex, data = d, var.equal = equal_var )
    return(list(dist = "normal", test="t-test", equal_var=equal_var, test_result = ttestJSW))
  }
  list(dist = "non-normal", test="wilcox", test_result = wilcox.test(JS.W ~ Sex, data = d))
}

用法:

代码语言:javascript
复制
test_sex(data1)

输出:

代码语言:javascript
复制
$dist
[1] "non-normal"

$test
[1] "wilcox"

$test_result

    Wilcoxon rank sum exact test

data:  JS.W by Sex
W = 100, p-value = 0.01075
alternative hypothesis: true location shift is not equal to 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72398166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档