【问题标题】:Need help using subset within lineplot.CI function in R需要帮助在 R 中的 lineplot.CI 函数中使用子集
【发布时间】:2014-05-20 01:42:45
【问题描述】:

我是一名统计学家,也是一名 R 新手,正在学习很多关于使用 RStudio 的知识。我有一个导入的数据框,其中包含长格式的数据,用于纵向平衡研究设计的混合效应方差分析。我的数据有以下标题:治疗、主题、日期、年龄和数量。我可以使用以下代码单独按年龄组或单独按治疗组进行绘图:

lineplot.CI(mri$Date,mri$Les.V.PD, mri$Age,main="Mean Lesion Volume by Age Group on PD Sequences",xlab="MRI timepoints (months post treatment)", ylab="Lesion Volume(cm^3)")

我想使用 lineplot.CI 绘制 x 轴上的日期,4 行:2 年治疗、6 年治疗、2 年对照和 6 年对照。此代码仅按年龄组生成折线图:

lineplot.CI(mri$Date,mri$Les.V.PD,mri$Age,subset=mri$Treatment %in% c("MSC","Control"),main="Mean Lesion Volume by Age Group on PD Sequences", xlab="MRI timepoints (months post treatment)", ylab="Lesion Volume(cm^3)")

此代码给出与上述代码相同的线图:

lineplot.CI(mri$Date,mri$Les.V.PD,mri$Age,subset= .(mri$Treatment == "MSC" | mri$Treament == "Control"),main="Mean Lesion Volume by Age Group on PD Sequences",xlab="MRI timepoints (months post treatment)", ylab="Lesion Volume(cm^3)")

我也尝试过这段代码的各种版本:

lineplot.CI(mri$Date,mri$Les.V.PD,mri$Age,subset(mri,Treatment == "MSC"|Treatment =="Control"),main="Mean Lesion Volume by Age Group on PD Sequences",xlab="MRI timepoints (months post treatment)", ylab="Lesion Volume(cm^3)")

lineplot.CI(mri$Date,mri$Les.V.PD,mri$Age,subset(mri, !(Treatment == "MSC"|Treatment == "Control")),main="Mean Lesion Volume by Age Group on PD Sequences",xlab="MRI timepoints (months post treatment)", ylab="Lesion Volume(cm^3)")

并得到以下错误:

Error in subset.default(mri$Treatment == "MSC" | mri$Treament == "Control") : 

参数“子集”丢失,没有默认值

Error in match.arg(type) : 'arg' must be NULL or a character vector

我知道子集包含在 lineplot.CI 中的 sciplot 包文档,但我看到的所有示例都显示子集=NULL。我宁愿继续使用 lineplot.CI,因为会自动插入误差线,而且我不熟悉 ggplot2。

谢谢

【问题讨论】:

    标签: r subset


    【解决方案1】:

    你可以使用:

    lineplot.CI(xField, yField, group=gField, data=subset(dataSource, 
                                                   field1=="value1" | field2=="value2"))
    

    问题的作者尝试使用带有参数“子集”的 lineplot.CI 命令。取而代之的是,可以将参数“data”与子集的值一起使用。

    所以,而不是使用

    lineplot.CI (..., subset=(datasource, select expression), ...)

    这个想法是使用:

    lineplot.CI(..., data=subset(datasource, select expression), ...)

    这种替代方式对我有用。

    【讨论】:

    • 欢迎来到这个网站,@hvescovi。您介意扩展一下并提供一些解释吗?什么是“[]”?
    【解决方案2】:

    我遇到了同样的问题...经过一些研究后,我发现lineplot.CI documentation 不仅缺少很多有用的信息,而且还提供了一些功能,就好像它们是其中的一部分一样,当那些是 外部的方法 实际上,可以与 lineplot.CI 结合 以达到特定的结果。 subset 是 R 环境的基础函数,因此它不依赖于 lineplot.CI。 This link 提供了有关它的更多信息,但可以在 R 包中提供的离线文档中找到更好/更深入的描述。在你的 R 命令行类型上

    help(subset)
    

    您会看到将其应用于数据帧的有趣方式。

    将您新创建的子集分配给 lineplot.CI 的 data 参数,您将获得所需的结果。例如(根据您的数据):

    df <- subset(
            mri, 
            Treatment %in% c("MSC", "Control"), 
            select = c( Treatment, Subject, Date, Age, Volume )
          )
    lineplot.CI(
       data = df,
       x.factor = Date, 
       response = Les.V.PD,
       group = Age,
       main="Mean Lesion Volume by Age Group on PD Sequences",
       xlab="MRI timepoints (months post treatment)", 
       ylab="Lesion Volume(cm^3)"
    )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2014-07-24
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多