2016-07-07 45 views
1

我在做PCA。下面是各项─R代码自动化

### Read .csv file ##### 
    data<-read.csv(file.choose(),header=T,sep=",") 
    names(data) 
    data$qcountry 
#### for the country-ARGENTINA####### 
ar_data<-data[which(data$qcountry=="ar"),] 
ar_data$qcountry<-NULL 
names(ar_data) 
names(ar_data)<-c("01_insufficient_efficacy","02_safety_issues","03_inconvenient_dosage_regimen","04_price_issues" 
        ,"05_not_reimbursed","06_not_inculed_govt","07_insuficient_clinicaldata","08_previously_used","09_prescription_opted_for_some_patients","10_scientific_info_NA","12_involved_in_diff_clinical_trial" 
        ,"13_patient_inappropriate_for_TT","14_patient_inappropriate_Erb","16_patient_over_65","17_Erbitux_alternative","95_Others") 

     names(ar_data) 
     ar_data_wdt_zero_columns<-ar_data[, colSums(ar_data != 0) > 0] 
####Testing multicollinearity#### 
     vif(ar_data_wdt_zero_columns) 

#### Testing appropriatness of PCA #### 
      KMO(ar_data_wdt_zero_columns) 
      cortest.bartlett(ar_data_wdt_zero_columns) 

    #### Run PCA #### 
     pca<-prcomp(ar_data_wdt_zero_columns,center=F,scale=F) 
     summary(pca) 

#### Compute the loadings for deciding the top4 most correlated variables### 
     load<-pca$rotation 
     write.csv(load,"loadings_argentina_2015_Q4.csv") 

我在这里为一个国家所示的代码,我已经为9countries做到了这一点。对于每个国家我都必须运行此代码。我确信必须有更简单的方法来自动执行此代码。请建议! 谢谢!

回答

1

是的,这对每个国家都是可行的。你可以使你的自定义功能采用适当的参数,例如国家名称和数据。你在里面做魔法并返回一个适当的对象(或不)。把这个魔法传递给你导入的一个处理过的数据,并且只做一次。下面的代码没有经过测试,但应该让你开始。

有几点意见。 请勿使用file.choose(),因为它会在行后3天内破坏您的代码。你怎么知道要选择什么文件?为什么在每次运行脚本时都点击,以便可以使脚本适合您?在这个意义上说是懒惰的。

你的脚本中有很多混乱。坚持一些风格,不要随意留下任何线索,以便尝试“屎和咯咯”。至少在代码中使用空格。

在选择对象名称时更有想象力。如果可能对象已经以基本函数的形式存在,例如, load

myPCA <- function(my.country, my.data) { 

    ar_data <- data[data$qcountry %in% "ar", ] 
    ar_data$qcountry <- NULL 

    ar_data_wdt_zero_columns <- ar_data[, colSums(ar_data != 0) > 0] 

    #### Run PCA #### 
    pca <- prcomp(ar_data_wdt_zero_columns, center = FALSE, scale = FALSE) 

    #### Compute the loadings for deciding the top4 most correlated variables### 
    write.csv(pca$rotation, paste("loadings_", my.country, ".csv", sep = "")) # may need tweaking 

    return(list(pca = pca, vif = vif(ar_data_wdt_zero_columns), 
       kmo = KMO(ar_data_wdt_zero_columns), correlation = cortest.bartlett(ar_data_wdt_zero_columns)) 
} 

data <- read.csv("relative_link_to_file", header = TRUE, sep = ",") 
names(data) <- c("01_insufficient_efficacy","02_safety_issues","03_inconvenient_dosage_regimen","04_price_issues" 
        ,"05_not_reimbursed","06_not_inculed_govt","07_insuficient_clinicaldata","08_previously_used","09_prescription_opted_for_some_patients","10_scientific_info_NA","12_involved_in_diff_clinical_trial" 
        ,"13_patient_inappropriate_for_TT","14_patient_inappropriate_Erb","16_patient_over_65","17_Erbitux_alternative","95_Others") 

sapply(data$qcountry, FUN = myPCA) 
+0

谢谢!!还有一件事我需要问一下,如果我想要读取不同文件的文件夹,然后在这些文件的国家/地区运行PCA,该怎么办? – Kavya

+0

@Kavya看看'list.files()'函数。这个问题在很多时候都被问及过。 –

+0

这就是正确的,但我不能使用上述功能(myPCA)的相同。 – Kavya