Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R运行提速优化 #53

Open
Captain-Pam opened this issue Jun 8, 2020 · 9 comments
Open

R运行提速优化 #53

Captain-Pam opened this issue Jun 8, 2020 · 9 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@Captain-Pam
Copy link
Collaborator

Captain-Pam commented Jun 8, 2020

关于因变量,我有200多个因变量,自变量有两种,第一种A,大概335个,第二种B,401个,每一个因变量,都要进行一下操作:

  1. 首先确定335个变量的rank,方法是线性回归算AIC,AIC小的排在前面;同理第二种变量也要这样确定他们的rank;
  2. 计算按照rank 添加自变量,每添加一个都要做一次80%train-20%test 的,并计算预测的值,再计算预测自与真实值相关系数的平方,也就是R2;
  3. 在第一种变量的R2中确定加入多少自变量时最大,最大第一种变量的个数,然后在这基础上用AIC确定第二种变量的rank,在算两种变量组合后R2。

我写好了处理的函数,但是用for循环实现太慢了,主要原因是在for循环里有两个自定义函数,且变量较多,算每次算AIC确定rank非常慢。我觉得两种变量各自AIC确定rank 能并行,R2的计算能并行应该提速。当然AIC确定的顺序提速也行。数据和代码我发你邮箱里


@Cobbybaby 我修改了下描述。

@JiaxiangBU JiaxiangBU self-assigned this Jun 9, 2020
@JiaxiangBU JiaxiangBU added the good first issue Good for newcomers label Jun 9, 2020
@JiaxiangBU
Copy link
Owner

@Cobbybaby

library(tidyverse)
library(fs)

我看了下整体代码,你做这样的修改

fun <- function(x,y) print(x,y)
for (i in 1:3) {
    for (j in 1:3) {
        fun(i,j)
    }
}
## [1] 1
## [1] 1
## [1] 1
## [1] 2
## [1] 2
## [1] 2
## [1] 3
## [1] 3
## [1] 3
i <- 1:3
j <- 1:3
map2_chr(i,j,fun)
## [1] 1
## [1] 2
## [1] 3

## [1] "1" "2" "3"

JiaxiangBU added a commit that referenced this issue Jun 9, 2020
@Captain-Pam
Copy link
Collaborator Author

Captain-Pam commented Jun 9, 2020 via email

@JiaxiangBU
Copy link
Owner

@Cobbybaby

这一段我没有看懂你的代码,你能够这样处理出来么?

df <- data.frame(arg1 = ..., arg2)
# 取一行
fun <- function(x,y) {...}
# 取第一行两个参数函数 fun 能跑通
fun(df[1,1],df[1,2])
# 离线保存 df 给我?
df %>% write_excel_csv("path ...")

@Captain-Pam
Copy link
Collaborator Author

家翔哥,我试着更新了一下map,在我给你的代码里改为map(files,rank_R2,phe_snp_cpg=phe_snp_cpg)
files你那边可能看不到,其实就是以phe_res数据框的列名命名的文件夹,每个列名是一个因变量。我执行起来了,只是遇到了snp_add_cpg_R2<-getR2(phe_snp_cpg,x,snp_add_cpg,sc=TRUE),这步出错,找不到snp_R2这个数据框,这个数据框是在这个函数之外生成的,正常应该向上查找才对吧。
家翔哥选取,snp,cpg的小子集试试,将files选两个phe_res的列名就行。

@Captain-Pam
Copy link
Collaborator Author

Captain-Pam commented Jun 9, 2020 via email

@JiaxiangBU
Copy link
Owner

家翔哥,我试着更新了一下map,在我给你的代码里改为map(files,rank_R2,phe_snp_cpg=phe_snp_cpg)
files你那边可能看不到,其实就是以phe_res数据框的列名命名的文件夹,每个列名是一个因变量。我执行起来了,只是遇到了snp_add_cpg_R2<-getR2(phe_snp_cpg,x,snp_add_cpg,sc=TRUE),这步出错,找不到snp_R2这个数据框,这个数据框是在这个函数之外生成的,正常应该向上查找才对吧。
家翔哥选取,snp,cpg的小子集试试,将files选两个phe_res的列名就行。

嗯,我需要一个 minimal 的例子,一共两列,一列是参数1,一列是参数2,你存为一个 csv,我调用即可,然后我需要你定义好的 fun 函数,函数自定义域,包含你需要的两个参数,这样我可以避免去改写你的代码其他部分。

其中,如你的数据集地址,都是 hard 实际上是不能执行的。 @Cobbybaby

@Captain-Pam
Copy link
Collaborator Author

Captain-Pam commented Jun 9, 2020 via email

@JiaxiangBU
Copy link
Owner

JiaxiangBU commented Jun 10, 2020 via email

@Captain-Pam
Copy link
Collaborator Author

Captain-Pam commented Jun 10, 2020 via email

@JiaxiangBU JiaxiangBU assigned slsongge and unassigned JiaxiangBU Jun 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants