1. 魂断蓝桥的起因

一个两天的bug,终于解决了,竟然是很基础的bug,花费了我两天时间,不免感叹:为什么相爱的人不能在一起。

事情是这个样子的:

最近测试一个R包breedR的动物模型的功能,用了它的测试数据:

> library(breedR)
> data("globulus")
> ped <- globulus[,1:3]
> str(ped)
'data.frame': 1021 obs. of  3 variables:$ self: int  69 70 71 72 73 74 75 76 77 78 ...$ dad : int  0 0 0 0 0 0 0 0 0 4 ...$ mum : int  64 41 56 55 22 50 67 59 49 8 ...
> res <- remlf90(  fixed = phe_X ~ gg,genetic = list(model = 'add_animal',pedig = ped,id = 'self'),data = globulus)
Using default initial variances given by default_initial_variance()
See ?breedR.getOption.> summary(res)
Formula: phe_X ~ 0 + gg + pedigree Data: globulus AIC  BIC logLik5799 5809  -2898Parameters of special components:Variance components:Estimated variances  S.E.
genetic                3.397 1.595
Residual              14.453 1.529Estimate    S.E.
Heritability   0.1887 0.08705

这里,Va为3.39,Ve为14.45,然后我使用asreml-r作为对比:

> library(asreml)
> head(dd)self dad mum gen gg bl  phe_X  x y
1   69   0  64   1 14 13 15.756  0 0
2   70   0  41   1  4 13 11.141  3 0
3   71   0  56   1 14 13 19.258  6 0
4   72   0  55   1 14 13  4.775  9 0
5   73   0  22   1  8 13 19.099 12 0
6   74   0  50   1 14 13 19.258 15 0
> dd$self = as.factor(dd$self)
> ainv = asreml.Ainverse(ped)$ginv
> mod1.as = asreml(phe_X ~ gg , random = ~ ped(self),ginverse = list(self = ainv), data=dd)
LogLikelihood Converged
> summary(mod1.as)$varcompgamma component std.error  z.ratio constraint
ped(self)!ped 0.2349996  3.396488  1.595445 2.128865   Positive
R!variance    1.0000000 14.453164  1.529262 9.451070   Positive

结果是一样的。

2. 当你以为一帆风顺时,生活来了

于是我用另外一个数据集,进行测试,数据是使用的我编写的R包:learnasreml中的数据:

> library(learnasreml)
> dat = animalmodel.dat
> ped = animalmodel.ped
> # asreml
> ainv = asreml.Ainverse(ped)$ginv
> mod2.as = asreml(BWT ~ SEX, random = ~ ped(ANIMAL), ginverse = list(ANIMAL = ainv), data=dat)
LogLikelihood Converged
> summary(mod2.as)$varcompgamma component std.error   z.ratio constraint
ped(ANIMAL)!ped 0.2160062  2.494254 0.9180669  2.716855   Positive
R!variance      1.0000000 11.547140 0.9386043 12.302458   Positive

方差组分Va为2.49,Ve为11.54。

使用breedR进行测试:

> dd2 = dat
> mod2.br = remlf90(BWT ~ SEX, genetic =  list(model = "add_animal",pedigree = ped, id="ANIMAL"),data=dd2)
> summary(mod2.br)
Formula: BWT ~ 0 + SEX + pedigree Data: dd2 AIC  BIC logLik5941 5951  -2968
Parameters of special components:
Variance components:Estimated variances   S.E.
genetic               0.6651 0.6728
Residual             13.3380 0.8602

纳尼?方差组分Va为0.66,Ve为13.33,这是什么鬼?和asreml不一样,据我对asreml的熟练程度,只有一种可能:那肯定是breedR有错误。

3. 你大爷永远是你大爷

于是我找到breedR的github中的issue:
上面问题描述:

Hi there,
I recently tried to fit an animal model using the remlf90() function. My model was simple and contained 4 fixed effects, 1 random non-genetic effect and the genetic additive effect (pedigree). I compared the results (h2 + se) to those of BLUPF90 (airemlf90) and they were the same as they should be. Then, I changed the class of the ‘id’ variable in the genetic part of the model from integer to factor and I re-ran the model. The h2 was considerabley different from that I got when the ‘id’ was of class integer.

dat399 a n i m a l < − a s . i n t e r g e r ( d a t 399 animal <- as.interger(dat399 animal<−as.interger(dat399animal) # h2 = 0.44, se = 0.012 (correct)
dat399 a n i m a l < − f a c t o r ( d a t 399 animal <- factor(dat399 animal<−factor(dat399animal) # h2 = 0.08, se = 0.006 (wrong)
dat399 a n i m a l < − a s . i n t e r g e r ( a s . c h a r a c t e r ( d a t 399 animal <- as.interger(as.character(dat399 animal<−as.interger(as.character(dat399animal)) # h2 = 0.44, se = 0.012 (correct again)

So, is this normal, should the ‘id’ part of the genetic effect be always coded as integer or there is a bug that needs to be corrected?

作者回答,breedR需要个体的ID是数字型,如果是因子的话,会报错提醒啊。。。

Hi Nabeel.
Yes. The variables encoding individuals (i.e., id and progenitors) should be integers.
However, the pedigree-building function should have raisen an error whenever the user tries with a factor.
How did you specify the pedigree in the model?
Thanks for your report and help.

然后作者问了一句开发者经常问道的问题:你这个bug是如何得到的。。。

然而,有时候个体是factor时,真的没有报错,我也想提交一个issue,算了,还是自己解决吧!

我就把因子转化为了数字,运行breedR:

> dd2$ANIMAL = as.numeric(dd2$ANIMAL)
> mod2.br = remlf90(BWT ~ SEX, genetic =  list(model = "add_animal",pedigree = ped, id="ANIMAL"),data=dd2)
> summary(mod2.br)
Formula: BWT ~ 0 + SEX + pedigree Data: dd2 AIC  BIC logLik5941 5951  -2968Parameters of special components:Variance components:Estimated variances   S.E.
genetic               0.6651 0.6728
Residual             13.3380 0.8602

这个。。。也太真实了,结果不变,依旧是错误的。

4. 黯然销魂掌

于是,我陷入了深深的职业自我怀疑中:我是爱它的,为什么相爱的人不能在一起?

我左看右看,上看下看,还是没有找到问题的所在,我翻遍了breedR的issue,发现了这么一句话:

Regarding the issue of the variable type of the animal id, note that in the genetic component, the pedigree is taken from ped399, where the variable animal is presumably integer or numeric. However, if you change the corresponding variable in dat399 as you have been doing, this breaks the correspondence between the animal codes in the pedigree and the dataset.

大意就是说,breedR中,系谱和个体ID需要是数字,因为系谱的数据会在breedR中重新编码,如果你改变了数据中ID的编码,那么系谱构建的矩阵就和数据中的ID对应不了,结果就可能是错误的。

这一段正确的废话,并没有激起我什么想法,我还是继续沉浸于深深的自我怀疑中,一定是我不够好,所以它才想要逃。。。

5. 梦里传来你的呼唤

灵感总是在梦中醒来,半夜忽然一个想法,是不是我转化数字的时候,变了?
但是数据中本来就是数字的因子类型啊,我把它转化为数字的数字类型时会变么?
我早知道R中有这种坑,在factor转化为number时,一定要通过character,否则会有各种不可预知的坑
难道
难道说
这个坑被我遇到了么???

第二天上班,我迫切的测试了一下:

> tt = dat
> head(tt$ANIMAL)
[1] 1029 1299 643  1183 1238 891
1084 Levels: 1 2 3 5 6 7 8 9 10 11 12 14 15 16 17 20 21 22 24 25 26 27 28 29 30 32 33 34 35 36 37 38 40 41 42 43 44 47 48 49 50 51 52 ... 1309
> head(as.numeric(tt$ANIMAL))
[1]  864 1076  549  989 1030  751

可以看到,变得面目全非,本来是1029,现在是864,本来是1299,现在是1076。

6. 恍然大迷瞪

看完之后,我激动的心无法平静,竟然想起了“为何相爱的人不能在一起的旋律”,我也太难了,竟然是这个原因。。。

脑子里想起祥林嫂的语句:
我早知道,R中factor转化为number时有可能出错。。。

然后我用character作为中间元素,再测试了一下:

> tt = dat
> head(tt$ANIMAL)
[1] 1029 1299 643  1183 1238 891
1084 Levels: 1 2 3 5 6 7 8 9 10 11 12 14 15 16 17 20 21 22 24 25 26 27 28 29 30 32 33 34 35 36 37 38 40 41 42 43 44 47 48 49 50 51 52 ... 1309
> head(as.numeric(as.character(tt$ANIMAL)))
[1] 1029 1299  643 1183 1238  891

这就是对的了!

最后我用正确的形式,测试breedR中的动物模型:

> dd2 = dat
> dd2$ANIMAL = as.numeric(as.character(dd2$ANIMAL))
> mod2.br = remlf90(BWT ~ SEX, genetic =  list(model = "add_animal",pedigree = ped, id="ANIMAL"),data=dd2)
> summary(mod2.br)
Formula: BWT ~ 0 + SEX + pedigree Data: dd2 AIC  BIC logLik5931 5941  -2964Parameters of special components:Variance components:Estimated variances   S.E.
genetic                2.494 0.9181
Residual              11.547 0.9386

终于看到了正确的结果,Va为2.49,Ve为11.54.

7. 多少人爱你青春欢畅的时辰

多么痛的领悟啊!

R中factor和number相互转化时,一定要经过character,这不是二手车市场,一定要有中间商赚差价!!!

为什么相爱的人不能在一起?相关推荐

  1. 真心相爱的人必做的21件事

    真心相爱的人必做的21件事 1 在大庭广众下来一个拥抱,或者一个KISS 2 两个人在一张床上睡一晚,但除了抱抱.亲亲什么都不干 3 选一天为彼此做一顿饭,然后面对面看着对方吃完 4 为他写日记,不管 ...

  2. 一辈子这么长,你得找个相爱的人在一起。

    一个人自愈的能力越强,才越有可能接近幸福.做一个寡言,却心有一片海的人,不伤人害己,于淡泊中,平和自在. 真正的好朋友,互损不会翻脸,疏远不会猜疑,出钱不会计较,地位不分高低,成功无需巴结,失败不会离 ...

  3. 为什么相爱的人不能在一起

    偶然间找来一首歌-----为什么相爱的人不能在一起.....就算忘记时间,也忘记你,也忘不了我们有过的甜蜜...我真的好伤悲好难受... 想起了我和他的以前,只剩片片回忆了,不知道他现在好不好,是不是 ...

  4. 为什么相爱的人不能在一起呢?

    "我们分手吧!" 这是女孩见到男孩说的第一句话!男孩沉默了一下,点点头:"好!" 女孩愣住了,打了男孩一巴掌,然后很生气的离开女孩走后,男孩抱着头躲在厕所大哭- ...

  5. 场面话大全,绝对受用一生

    ◆ 父母生日祝酒辞  尊敬的各位领导.各们长辈.各们亲朋好友:大家好!  在这喜庆的日子里,我们高兴地迎来了敬爱的父亲(母亲)XX岁的生日.今天,我们欢聚一堂,举行父亲(母亲)XX华诞庆典.这里,我代 ...

  6. 范登读书解读《亲密关系》(婚姻、爱情) 笔记

    来源:邀请你看<樊登解读<亲密关系>(已婚人士必看)>,https://url.cn/5HJvLk5?sf=uri 人们在童年的时候始终追寻着两种东西,第一种叫做归属感,第二叫 ...

  7. 说到心里的哲理个性签名 学生时代的恋爱无非就是陪伴二字

    学生时代的恋爱无非就是陪伴二字 也许因为得不到所以空想总是美好 . 让一个男人哭了 没错你赢了 但是你玩大了 曾经我们都那样嚣张后来怎么也学会了退让. 爱一个人成为习惯就会失去放手的勇敢. 有时沉默并 ...

  8. 宋仲基宋慧乔没能找到对的人,算法能帮我们找到么?

    By 超神经 场景描述:寻找能够相伴一生的灵魂伴侣是很多人的美好愿望,但现实往往残酷.为此,基于大数据,机器学习,AI 算法的婚恋网站和应用纷纷出招,它们能够帮助广大单身男女解决这个问题吗? 关键词: ...

  9. localhost❤matrix6

    第一次听见localhost这个名字是从ex那里听来的.我很喜欢写blog,每次我写完blog他就回给我评论已阅.我当时看见这两个字的时候我觉得又气又好笑.感觉就像古时候的皇帝批阅奏章,然后盖个章,又 ...

最新文章

  1. 一次服务器CPU占用率高的定位分析
  2. SAP ABAP实用技巧介绍系列之 使用simple transformation的mapping功能
  3. 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 1
  4. DB2 sql复制error sqlcode2038
  5. autoline 手册
  6. Intellij idea创建maven项目并配置tomcat
  7. Tarjan算法——强连通分量
  8. react 中加载静态word文档(或加载静态的html文件)
  9. 【Windows】PPT播放视频提示媒体不可用的解决方法
  10. 银行客户用户画像_客户分类、客户标签与用户画像,怎样助力转化?
  11. 反爬虫破解——裁判文书网
  12. 基于python机票预定系统_机票预订系统课程设计.doc
  13. python假设检验--两个总体参数的检验(方差)
  14. python实现账号密码登录
  15. iOS在图片上添加文字或图片
  16. neo4j 图数据库初步调研 三元组、属性图、图模型、超图、RDF-f
  17. 诗歌(2)—定风波(莫听)
  18. c#开发Windows服务程序及部署
  19. 金富瑞UCML2.0应用框架平台 for Asp.Net WEB 开发平台
  20. STM32使用硬件SPI驱动RC522门禁模块

热门文章

  1. 实施CRM前的五个要领
  2. Matlab中bin2dec函数使用
  3. springboot模板整合(四)邮箱验证
  4. Android实践--如何提高Android模拟器的运行速度
  5. VirTex: Learning Visual Representations from Textual Annotations
  6. 机器学习-近9年双色球开奖数据的频繁项集
  7. C语言中signed和unsigned理解
  8. 安装ORB-SLAM3教程
  9. “中国股票第一博”带头大哥777涉嫌非法经营被调查
  10. scanf和putchar