Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. fastAssoc <- function(y, x)
  2. {
  3. index <- is.finite(y) & is.finite(x)
  4. n <- sum(index)
  5. y <- y[index]
  6. x <- x[index]
  7. vx <- var(x)
  8. bhat <- cov(y, x) / vx
  9. ahat <- mean(y) - bhat * mean(x)
  10. # fitted <- ahat + x * bhat
  11. # residuals <- y - fitted
  12. # SSR <- sum((residuals - mean(residuals))^2)
  13. # SSF <- sum((fitted - mean(fitted))^2)
  14.  
  15. rsq <- (bhat * vx)^2 / (vx * var(y))
  16. fval <- rsq * (n-2) / (1-rsq)
  17. tval <- sqrt(fval)
  18. se <- abs(bhat / tval)
  19.  
  20. # Fval <- (SSF) / (SSR/(n-2))
  21. # pval <- pf(Fval, 1, n-2, lowe=F)
  22. p <- pf(fval, 1, n-2, lowe=F)
  23. return(list(
  24. ahat=ahat, bhat=bhat, se=se, fval=fval, pval=p, n=n
  25. ))
  26. }
  27.  
  28. getFittedVals <- function(y, x)
  29. {
  30. n <- length(x)
  31. bhat <- cov(y, x) / var(x)
  32. ahat <- mean(y) - bhat * mean(x)
  33. fitted <- ahat + x * bhat
  34. return(fitted)
  35. }
  36.  
  37. runGwas <- function(x, y)
  38. {
  39. require(dplyr)
  40. y <- as.matrix(y)
  41. x <- as.matrix(x)
  42. ntrait <- ncol(y)
  43. nsnp <- ncol(x)
  44. l <- list()
  45. for(i in 1:ntrait)
  46. {
  47. message(i)
  48. res <- array(0, c(nsnp, 7))
  49. res[,1] <- 1:nsnp
  50. for(j in 1:nsnp)
  51. {
  52. res[j,2:7] <- unlist(fastAssoc(y[,i], x[,j]))
  53. }
  54. l[[i]] <- data.frame(res)
  55. names(l[[i]]) <- c("snp", "a", "b", "se", "fval", "pval", "n")
  56. l[[i]]$trait <- paste("trait", i)
  57.  
  58. }
  59. return(bind_rows(l))
  60. }
  61.  
  62.  
  63. makeGeno <- function(nid, nsnp, maf=0.5)
  64. {
  65. matrix(rbinom(nid * nsnp, 2, maf), nid, nsnp)
  66. }
  67.  
  68.  
  69. makePhen <- function(effs, indep, vy=1, vx=rep(1, length(effs)))
  70. {
  71. if(is.null(dim(indep))) indep <- cbind(indep)
  72. stopifnot(ncol(indep) == length(effs))
  73. stopifnot(length(vx) == length(effs))
  74. cors <- effs * vx / sqrt(vx) / sqrt(vy)
  75. stopifnot(sum(cors^2) <= 1)
  76. cors <- c(cors, sqrt(1-sum(cors^2)))
  77. indep <- t(t(scale(cbind(indep, rnorm(nrow(indep))))) * cors * c(vx, 1))
  78. y <- drop(scale(rowSums(indep)) * sqrt(vy))
  79. return(y)
  80. }
  81.  
  82. spreadGwas <- function(gwas, col)
  83. {
  84. require(tidyr)
  85. l <- list()
  86. stopifnot(all(col %in% names(gwas)))
  87. for(i in 1:length(col))
  88. {
  89. x <- select(gwas, snp, trait, get(col[i]))
  90. l[[i]] <- spread_(x, "trait", col[i])
  91. }
  92.  
  93. if(length(col)==1)
  94. {
  95. return(l[[i]])
  96. } else {
  97. names(l) <- col
  98. return(l)
  99. }
  100. }
  101.  
  102.  
  103. chooseEffects <- function(nsnp, totvar, sqrt=TRUE)
  104. {
  105. eff <- rnorm(nsnp)
  106. aeff <- abs(eff)
  107. sc <- sum(aeff) / totvar
  108. out <- eff / sc
  109. if(sqrt)
  110. {
  111. out <- sqrt(abs(out)) * sign(out)
  112. }
  113. return(out)
  114. }
  115.  
  116.  
  117.  
  118. # g <- makeGeno(1000, 20)
  119. # u <- makePhen(chooseEffects(20, 0.3), g)
  120.  
  121. # x <- makePhen(sqrt(0.6), u)
  122. # y <- makePhen(sqrt(0.2), u)
  123.  
  124. # gwas <- runGwas(g, cbind(x,y))
  125. # out <- spreadGwas(gwas, c("b"))
  126.  
  127. # plot(out[,3] ~ out[,2])
  128.  
  129.  
  130.  
  131.  
  132. # g <- makeGeno(10000, 20)
  133. # x <- makePhen(chooseEffects(20, 0.3), g)
  134. # u <- rnorm(10000)
  135. # y <- makePhen(sqrt(c(0.4, 0.02)), cbind(x,u))
  136.  
  137. # gwas <- runGwas(g, cbind(x,y))
  138. # out <- spreadGwas(gwas, c("b"))
  139.  
  140. # plot(out[,3] ~ out[,2])
  141.  
  142. # gwas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement