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做到了這一點。對於每個國家我都必須運行此代碼。我確信必須有更簡單的方法來自動執行此代碼。請建議! 謝謝!
謝謝!!還有一件事我需要問一下,如果我想要讀取不同文件的文件夾,然後在這些文件的國家/地區運行PCA,該怎麼辦? – Kavya
@Kavya看看'list.files()'函數。這個問題在很多時候都被問及過。 –
這就是正確的,但我不能使用上述功能(myPCA)的相同。 – Kavya