1
theoractice 2016-04-15 14:36:50 +08:00 via Android
换 openblas
其他不会了 |
2
srlp 2016-04-15 16:29:41 +08:00
无语了,你代码都没有,怎么帮你?
尽量写成向量化的,容易并行。 |
3
Or2 OP 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 ,所以我就索性自己弄了。结果速度慢的一塌糊涂 |
5
Or2 OP 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) 这是新改的代码,谁帮我看看还有有什么地方可以改进下 |