V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Or2
V2EX  ›  问与答

求助 R 的大神,求助一个关于改进 R 速度的问题

  •  
  •   Or2 · 2016-04-15 14:29:51 +08:00 · 1430 次点击
    这是一个创建于 3129 天前的主题,其中的信息可能已经有所发展或是发生改变。
    自己写的 bootstrap 太慢了。。。
    第 1 条附言  ·  2016-04-15 23:17:36 +08:00
    代码贴在了 3 楼,希望大家帮下忙。

    老师要求不要用 boot package 。 所以我自己写了,请大家使劲的喷,我心理素质极好^.^
    第 2 条附言  ·  2016-04-16 13:37:27 +08:00
    5 楼放了新改的,速度快了许多
    5 条回复    2016-04-16 13:36:45 +08:00
    theoractice
        1
    theoractice  
       2016-04-15 14:36:50 +08:00 via Android
    换 openblas
    其他不会了
    srlp
        2
    srlp  
       2016-04-15 16:29:41 +08:00
    无语了,你代码都没有,怎么帮你?
    尽量写成向量化的,容易并行。
    Or2
        3
    Or2  
    OP
       2016-04-15 23:15:37 +08:00
    data = read.csv("C:/Users/Bamb0o0o0/Desktop/dataset.csv",header = TRUE) # import the data as Data Frame

    # dummies

    data$logearnings = log(data$earnings)

    data$occ1 = ifelse(data$occ == 1,1,0)
    data$occ2 = ifelse(data$occ == 2,1,0)
    data$occ3 = ifelse(data$occ == 3,1,0)
    data$occ4 = ifelse(data$occ == 4,1,0)

    data$northeast = ifelse(data$region == 1,1,0)
    data$south = ifelse(data$region == 2,1,0)
    data$midwest = ifelse(data$region == 3,1,0)
    data$west = ifelse(data$region == 4,1,0)
    data$central = ifelse(data$region == 5,1,0)

    data$exp = data$age - data$edu - 6 # 6 the age people gets in to primary school

    data$exp_2 = (data$exp)**2

    data$exp_3 = (data$exp)**3

    data$edu_ability = data$edu*data$ability

    Data=subset(data,select=-c(earnings,occ,region)) # create a new Data Frame with out column earnings, occ, region

    female_earnings = subset(Data,Data$female == 1)
    male_earnings = subset(Data,Data$female == 0)





    ############################################################################################
    ################ Bootstrap ################
    ############################################################################################

    ### empty Matrix for coefficients of OLS
    matrix_Female = matrix(, nrow = 0, ncol = 11)
    matrix_Male = matrix(, nrow = 0, ncol = 11)

    for (s in 1:500){
    ### empty data frame for new sample
    df = data.frame(id = integer(), ability = integer(), age = integer(),
    female = integer(), edu = integer(), logearnings = double(), occ1 = integer(),
    occ2 = integer(), occ3 = integer(), occ4 = integer(), northeast = integer(),
    south = integer(), midwest = integer(), west = integer(), central = integer(),
    exp = integer(), exp_2 = integer(), exp_3 = integer(), edu_ability = integer())
    ### create new data frame/ sample)

    A = sample(Data$id, 10000, replace = TRUE)

    for (i in A) {
    df = rbind(df,Data[i,])}

    df_female_earnings= subset(df,df$female == 1)
    df_male_earnings = subset(df,df$female == 0)

    ### OLS for new Sample
    Model_11_F = lm(df_female_earnings$logearnings ~ df_female_earnings$edu + df_female_earnings$northeast
    + df_female_earnings$south + df_female_earnings$midwest + df_female_earnings$age
    + df_female_earnings$ability + df_female_earnings$exp_2 + df_female_earnings$exp_3
    + df_female_earnings$edu_ability + df_female_earnings$west)
    #summary(Model_11_F)
    coef(Model_11_F)
    matrix_Female=rbind(matrix_Female,coef(Model_11_F))

    Model_11_M = lm(df_male_earnings$logearnings ~ df_male_earnings$edu + df_male_earnings$northeast
    + df_male_earnings$south + df_male_earnings$midwest + df_male_earnings$age
    + df_male_earnings$ability + df_male_earnings$exp_2 + df_male_earnings$exp_3
    + df_male_earnings$edu_ability + df_male_earnings$west)
    #summary(Model_11_M)
    coef(Model_11_M)
    matrix_Male=rbind(matrix_Male,coef(Model_11_M))
    }


    Female_Mean = colMeans(matrix_Female)
    Male_Mean = colMeans(matrix_Male)

    Matrix_Female_Mean = matrix(colMeans(matrix_Female), nrow=500, ncol=11, byrow=TRUE)
    Matrix_Male_Mean = matrix(colMeans(matrix_Male), nrow=500, ncol=11, byrow=TRUE)

    #deviation matrix
    F = (matrix_Female - Matrix_Female_Mean)^2
    M = (matrix_Male - Matrix_Male_Mean)^2

    F_sum = colSums(F)
    M_sum = colSums(M)

    哦,现在贴,第一次发这样的帖子不知道该怎么办。

    老师的要就是不让用 boot package ,所以我就索性自己弄了。结果速度慢的一塌糊涂
    Or2
        4
    Or2  
    OP
       2016-04-15 23:16:26 +08:00
    @srlp 贴了,请尽量指出我的问题
    Or2
        5
    Or2  
    OP
       2016-04-16 13:36:45 +08:00
    data_DF = read.csv("C:/Users/Bamb0o0o0/Desktop/dataset.csv",header = TRUE) # import the data_DF as data_DF Frame

    data_DF$logearnings = log(data_DF$earnings)

    data_DF$occ1 = ifelse(data_DF$occ == 1,1,0)
    data_DF$occ2 = ifelse(data_DF$occ == 2,1,0)
    data_DF$occ3 = ifelse(data_DF$occ == 3,1,0)
    data_DF$occ4 = ifelse(data_DF$occ == 4,1,0)

    data_DF$northeast = ifelse(data_DF$region == 1,1,0)
    data_DF$south = ifelse(data_DF$region == 2,1,0)
    data_DF$midwest = ifelse(data_DF$region == 3,1,0)
    data_DF$west = ifelse(data_DF$region == 4,1,0)
    data_DF$central = ifelse(data_DF$region == 5,1,0)

    data_DF$exp = data_DF$age - data_DF$edu - 6 # 6 the age people gets in to primary school

    data_DF$exp_2 = (data_DF$exp)**2

    data_DF$exp_3 = (data_DF$exp)**3

    data_DF$edu_ability = data_DF$edu*data_DF$ability

    # create a new data_DF Frame with out column earnings, occ, region
    data_DF = subset(data_DF,select = -c(earnings,occ,region))

    data = data.matrix(data_DF)

    BootStrapping = function(){
    id = sample(data[,1], 10000, replace = T)
    # Data Frame: data_DF$id or data_DF[,1] class(id) now is integer, need less memory
    # Matrix: data[,which(col.names(data)== 'id')], class(id) now is numeric, need more memory


    New_Sample = data[id,]

    # Screening data again
    female_earnings = New_Sample[New_Sample[,4] == 1,]
    male_earnings = subset(New_Sample, New_Sample[,4] == 0)

    # OLS
    Model_11_F = lm(female_earnings[,6] ~ female_earnings[,5] + female_earnings[,11]
    + female_earnings[,12] + female_earnings[,13] + female_earnings[,3]
    + female_earnings[,2] + female_earnings[,17] + female_earnings[,18]
    + female_earnings[,19] + female_earnings[,14])

    coef(Model_11_F)

    Model_11_M = lm(male_earnings[,6] ~ male_earnings[,5] + male_earnings[,11]
    + male_earnings[,12] + male_earnings[,13] + male_earnings[,3]
    + male_earnings[,2] + male_earnings[,17] + male_earnings[,18]
    + male_earnings[,19] + male_earnings[,14])
    coef(Model_11_M)

    return(c(coef(Model_11_F),coef(Model_11_M)))
    }

    Numbers_of_Replication = 1000

    Coefficient_Matrix = replicate(Numbers_of_Replication,BootStrapping())

    # Set Row names
    rownames(Coefficient_Matrix) = c('Female_Intercept', 'Female_edu', 'Female_northeast',
    'Female_south', 'Female_midwest', 'Female_age', 'Female_ability',
    'Female_exp_2', 'Female_exp_3', 'Female_edu_ability', 'Female_west',
    'Male_Intercept', 'Male_edu', 'Male_northeast','Male_south', 'Male_midwest',
    'Male_age', 'Male_ability', 'Male_exp_2', 'Male_exp_3', 'Male_edu_ability', 'Male_west')

    # Create Mean Matrix Residuals and STDER
    Coefficient_Mean_Matrix = matrix(rowMeans(Coefficient_Matrix),
    nrow = 22, ncol = Numbers_of_Replication, byrow = FALSE)

    Residuals_Square = (Coefficient_Matrix - Coefficient_Mean_Matrix)^2

    Residuals_Square_Sum = rowSums(Residuals_Square)

    STDER = sqrt(1/(Numbers_of_Replication-1)*Residuals_Square_Sum)

    这是新改的代码,谁帮我看看还有有什么地方可以改进下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2905 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 02:41 · PVG 10:41 · LAX 18:41 · JFK 21:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.