[英]Loop through columns in S4 objects in R
我正在尝试使用snpStats包执行关联。
我有一个名为'plink'的snp矩阵,其中包含我的基因型数据(作为$ genotypes,$ map,$ fam的列表),而plink $ genotype具有:SNP名称作为列名称(2个SNP),主题标识符作为行名:
plink$genotype
SnpMatrix with 6 rows and 2 columns
Row names: 1 ... 6
Col names: 203 204
可以复制以下ped和map文件来复制plink数据集,并将它们分别保存为“ plink.ped”和“ plink.map”:
plink.ped:
1 1 0 0 1 -9 A A G G
2 2 0 0 2 -9 G A G G
3 3 0 0 1 -9 A A G G
4 4 0 0 1 -9 A A G G
5 5 0 0 1 -9 A A G G
6 6 0 0 2 -9 G A G G
plink.map:
1 203 0 792429
2 204 0 819185
然后以这种方式使用plink:
./plink --file plink --make-bed
@----------------------------------------------------------@
| PLINK! | v1.07 | 10/Aug/2009 |
|----------------------------------------------------------|
| (C) 2009 Shaun Purcell, GNU General Public License, v2 |
|----------------------------------------------------------|
| For documentation, citation & bug-report instructions: |
| http://pngu.mgh.harvard.edu/purcell/plink/ |
@----------------------------------------------------------@
Web-based version check ( --noweb to skip )
Recent cached web-check found...Problem connecting to web
Writing this text to log file [ plink.log ]
Analysis started: Tue Nov 29 18:08:18 2011
Options in effect:
--file /ugi/home/claudiagiambartolomei/Desktop/plink
--make-bed
2 (of 2) markers to be included from [ /ugi/home/claudiagiambartolomei/Desktop /plink.map ]
6 individuals read from [ /ugi/home/claudiagiambartolomei/Desktop/plink.ped ]
0 individuals with nonmissing phenotypes
Assuming a disease phenotype (1=unaff, 2=aff, 0=miss)
Missing phenotype value is also -9
0 cases, 0 controls and 6 missing
4 males, 2 females, and 0 of unspecified sex
Before frequency and genotyping pruning, there are 2 SNPs
6 founders and 0 non-founders found
Total genotyping rate in remaining individuals is 1
0 SNPs failed missingness test ( GENO > 1 )
0 SNPs failed frequency test ( MAF < 0 )
After frequency and genotyping pruning, there are 2 SNPs
After filtering, 0 cases, 0 controls and 6 missing
After filtering, 4 males, 2 females, and 0 of unspecified sex
Writing pedigree information to [ plink.fam ]
Writing map (extended format) information to [ plink.bim ]
Writing genotype bitfile to [ plink.bed ]
Using (default) SNP-major mode
Analysis finished: Tue Nov 29 18:08:18 2011
我还有一个表型数据框,其中包含要与基因型相关联的结果(结果1,结果2,...):
ID<- 1:6
sex<- rep(1,6)
age<- c(59,60,54,48,46,50)
bmi<- c(26,28,22,20,23, NA)
ldl<- c(5, 3, 5, 4, 2, NA)
pheno<- data.frame(ID,sex,age,bmi,ldl)
当我这样做时,该关联仅适用于单个术语:(使用公式“ snp.rhs.test”):
bmi<-snp.rhs.tests(bmi~sex+age,family="gaussian", data=pheno, snp.data=plink$genotype)
我的问题是,如何遍历结果? 这类数据似乎与其他所有数据都不一样,因此我在处理数据时遇到了麻烦,因此,如果您对一些教程的建议可以帮助我理解如何执行此操作以及其他操作(例如设置snp.matrix),也将不胜感激例如数据。
这是我尝试过的循环:
rhs <- function(x) {
x<- snp.rhs.tests(x, family="gaussian", data=pheno,
snp.data=plink$genotype)
}
res_ <- apply(pheno,2,rhs)
Error in x$terms : $ operator is invalid for atomic vectors
然后我尝试了这个:
for (cov in names(pheno)) {
association<-snp.rhs.tests(cov, family="gaussian",data=pheno, snp.data=plink$genotype)
}
Error in eval(expr, envir, enclos) : object 'bmi' not found
像往常一样感谢您的帮助! -F
snpStats的作者是David Clayton。 尽管包装说明中列出的网站是错误的,但他仍然在该域中,并且可以使用具有以下规范的Google的高级搜索功能来搜索文档:
snpStats site:https://www-gene.cimr.cam.ac.uk/staff/clayton/
访问困难的可能原因是这是一个S4程序包,并且访问方法不同。 代替打印方法,S4对象通常具有显示方法。 包装上有一个小插图: https : //www-gene.cimr.cam.ac.uk/staff/clayton/courses/florence11/practicals/practical6.pdf ,并且他的整个短期课程目录向访问: https : //www-gene.cimr.cam.ac.uk/staff/clayton/courses/florence11/
很明显,可以使用“ [”(使用第7页上所示的序列号或名称)使用“ [”访问从snp.rhs.tests返回的对象。您可以获取名称:
# Using the example on the help(snp.rhs.tests) page:
> names(slt3)
[1] "173760" "173761" "173762" "173767" "173769" "173770" "173772" "173774"
[9] "173775" "173776"
您可能正在调用的列可能是“插槽”
> getSlots(class(slt3))
snp.names var.names chisq df N
"ANY" "character" "numeric" "integer" "integer"
> str(getSlots(class(slt3)))
Named chr [1:5] "ANY" "character" "numeric" "integer" "integer"
- attr(*, "names")= chr [1:5] "snp.names" "var.names" "chisq" "df" ...
> names(getSlots(class(slt3)))
[1] "snp.names" "var.names" "chisq" "df" "N"
但是没有[i,j]
方法可以循环这些插槽名称。 相反,您应该转到帮助页面?"GlmTests-class"
,其中列出了为该S4类定义的方法。
做初始海报所需的正确方法是:
for (i in ncol(pheno)) {
association <- snp.rhs.tests(pheno[,i], family="gaussian", snp.data=plink$genotype)
}
snp.rhs.tests()
的文档说,如果缺少数据,则从父框架获取表型-或用相反的措辞来表述:如果指定了数据,则在指定data.frame
评估表型data.frame
。
这是一个更清晰的版本:
for (i in ncol(pheno)) {
cc <- pheno[,i]
association <- snp.rhs.tests(cc, family="gaussian", snp.data=plink$genotype)
}
该文件说, data=parent.frame()
是在默认snp.rhs.tests()
apply()
代码中有一个明显的错误-请不要执行x <- some.fun(x)
,因为它做的很不好。 请尝试以下操作-删除data=
,并使用其他变量名。
rhs <- function(x) {
y<- snp.rhs.tests(x, family="gaussian", snp.data=plink$genotype)
}
res_ <- apply(pheno,2,rhs)
最初的发布者的问题也具有误导性。
plink $ genotype是一个S4对象,pheno是一个data.frame(一个S3对象)。 您确实只想选择S3 data.frame中的列,但是snp.rhs.tests()如何查找列(如果给定了data.frame)或向量表型(如果为以纯向量形式给出-即在父框架或您的“当前”框架中,因为子例程在“子”框架中进行计算!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.