R语言:利用相关性分析对复杂数据进行数据探索

cor(1:5,1:5)

## [1] 1

cor(1:5,5:1)

## [1] -1

cor(1:5,c(1,2,3,4,4))

## [1] 0.9701

cor(1:5,c(1,2,3,1,4))

## [1] 0.6063

library(RCurl)

## Loading required package: bitops

urlfile<-"http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"

x<-getURL(urlfile,ssl.verifypeer=FALSE)

adults<-read.csv(textConnection(x),header=F)

head(adults,2)

##   V1                V2    V3         V4 V5                  V6

## 1 39         State-gov 77516  Bachelors 13       Never-married

## 2 50  Self-emp-not-inc 83311  Bachelors 13  Married-civ-spouse

##                 V7             V8     V9   V10  V11 V12 V13            V14

## 1     Adm-clerical  Not-in-family  White  Male 2174   0  40  United-States

## 2  Exec-managerial        Husband  White  Male    0   0  13  United-States

##      V15

## 1  <=50K

## 2  <=50K

# 更改列名

names(adults)<-c("Age","Workclass","FinalWeight","Education","EducationNumer","MaritalStatus","Occupation","Relationship","Race","Sex","CapitalGain","CapitalLoss","HoursWeek","NativeCountry","Income")

head(adults,2)

##   Age         Workclass FinalWeight  Education EducationNumer

## 1  39         State-gov       77516  Bachelors             13

## 2  50  Self-emp-not-inc       83311  Bachelors             13

##         MaritalStatus       Occupation   Relationship   Race   Sex

## 1       Never-married     Adm-clerical  Not-in-family  White  Male

## 2  Married-civ-spouse  Exec-managerial        Husband  White  Male

##   CapitalGain CapitalLoss HoursWeek  NativeCountry Income

## 1        2174           0        40  United-States  <=50K

## 2           0           0        13  United-States  <=50K

adults$Income<-ifelse(adults$Income==" <=50K",0,1)

str(adults)

## ‘data.frame‘:    32561 obs. of  15 variables:

##  $ Age           : int  39 50 38 53 28 37 49 52 31 42 ...

##  $ Workclass     : Factor w/ 9 levels " ?"," Federal-gov",..: 8 7 5 5 5 5 5 7 5 5 ...

##  $ FinalWeight   : int  77516 83311 215646 234721 338409 284582 160187 209642 45781 159449 ...

##  $ Education     : Factor w/ 16 levels " 10th"," 11th",..: 10 10 12 2 10 13 7 12 13 10 ...

##  $ EducationNumer: int  13 13 9 7 13 14 5 9 14 13 ...

##  $ MaritalStatus : Factor w/ 7 levels " Divorced"," Married-AF-spouse",..: 5 3 1 3 3 3 4 3 5 3 ...

##  $ Occupation    : Factor w/ 15 levels " ?"," Adm-clerical",..: 2 5 7 7 11 5 9 5 11 5 ...

##  $ Relationship  : Factor w/ 6 levels " Husband"," Not-in-family",..: 2 1 2 1 6 6 2 1 2 1 ...

##  $ Race          : Factor w/ 5 levels " Amer-Indian-Eskimo",..: 5 5 5 3 3 5 3 5 5 5 ...

##  $ Sex           : Factor w/ 2 levels " Female"," Male": 2 2 2 2 1 1 1 2 1 2 ...

##  $ CapitalGain   : int  2174 0 0 0 0 0 0 0 14084 5178 ...

##  $ CapitalLoss   : int  0 0 0 0 0 0 0 0 0 0 ...

##  $ HoursWeek     : int  40 13 40 40 40 40 16 45 50 40 ...

##  $ NativeCountry : Factor w/ 42 levels " ?"," Cambodia",..: 40 40 40 40 6 40 24 40 40 40 ...

##  $ Income        : num  0 0 0 0 0 0 0 1 1 1 ...

# 对因子变量进行哑变量处理

library(caret)

## Loading required package: lattice

## Loading required package: ggplot2

dmy<-dummyVars("~.",data=adults)

adultsTrsf<-data.frame(predict(dmy,newdata=adults))

dim(adults)

## [1] 32561    15

dim(adultsTrsf)

## [1] 32561   109

head(adultsTrsf)

##   Age Workclass... Workclass..Federal.gov Workclass..Local.gov

## 1  39            0                      0                    0

## 2  50            0                      0                    0

## 3  38            0                      0                    0

## 4  53            0                      0                    0

## 5  28            0                      0                    0

## 6  37            0                      0                    0

##   Workclass..Never.worked Workclass..Private Workclass..Self.emp.inc

## 1                       0                  0                       0

## 2                       0                  0                       0

## 3                       0                  1                       0

## 4                       0                  1                       0

## 5                       0                  1                       0

## 6                       0                  1                       0

##   Workclass..Self.emp.not.inc Workclass..State.gov Workclass..Without.pay

## 1                           0                    1                      0

## 2                           1                    0                      0

## 3                           0                    0                      0

## 4                           0                    0                      0

## 5                           0                    0                      0

## 6                           0                    0                      0

##   FinalWeight Education..10th Education..11th Education..12th

## 1       77516               0               0               0

## 2       83311               0               0               0

## 3      215646               0               0               0

## 4      234721               0               1               0

## 5      338409               0               0               0

## 6      284582               0               0               0

##   Education..1st.4th Education..5th.6th Education..7th.8th Education..9th

## 1                  0                  0                  0              0

## 2                  0                  0                  0              0

## 3                  0                  0                  0              0

## 4                  0                  0                  0              0

## 5                  0                  0                  0              0

## 6                  0                  0                  0              0

##   Education..Assoc.acdm Education..Assoc.voc Education..Bachelors

## 1                     0                    0                    1

## 2                     0                    0                    1

## 3                     0                    0                    0

## 4                     0                    0                    0

## 5                     0                    0                    1

## 6                     0                    0                    0

##   Education..Doctorate Education..HS.grad Education..Masters

## 1                    0                  0                  0

## 2                    0                  0                  0

## 3                    0                  1                  0

## 4                    0                  0                  0

## 5                    0                  0                  0

## 6                    0                  0                  1

##   Education..Preschool Education..Prof.school Education..Some.college

## 1                    0                      0                       0

## 2                    0                      0                       0

## 3                    0                      0                       0

## 4                    0                      0                       0

## 5                    0                      0                       0

## 6                    0                      0                       0

##   Education.Numer MaritalStatus..Divorced MaritalStatus..Married.AF.spouse

## 1              13                       0                                0

## 2              13                       0                                0

## 3               9                       1                                0

## 4               7                       0                                0

## 5              13                       0                                0

## 6              14                       0                                0

##   MaritalStatus..Married.civ.spouse MaritalStatus..Married.spouse.absent

## 1                                 0                                    0

## 2                                 1                                    0

## 3                                 0                                    0

## 4                                 1                                    0

## 5                                 1                                    0

## 6                                 1                                    0

##   MaritalStatus..Never.married MaritalStatus..Separated

## 1                            1                        0

## 2                            0                        0

## 3                            0                        0

## 4                            0                        0

## 5                            0                        0

## 6                            0                        0

##   MaritalStatus..Widowed Occupation... Occupation..Adm.clerical

## 1                      0             0                        1

## 2                      0             0                        0

## 3                      0             0                        0

## 4                      0             0                        0

## 5                      0             0                        0

## 6                      0             0                        0

##   Occupation..Armed.Forces Occupation..Craft.repair

## 1                        0                        0

## 2                        0                        0

## 3                        0                        0

## 4                        0                        0

## 5                        0                        0

## 6                        0                        0

##   Occupation..Exec.managerial Occupation..Farming.fishing

## 1                           0                           0

## 2                           1                           0

## 3                           0                           0

## 4                           0                           0

## 5                           0                           0

## 6                           1                           0

##   Occupation..Handlers.cleaners Occupation..Machine.op.inspct

## 1                             0                             0

## 2                             0                             0

## 3                             1                             0

## 4                             1                             0

## 5                             0                             0

## 6                             0                             0

##   Occupation..Other.service Occupation..Priv.house.serv

## 1                         0                           0

## 2                         0                           0

## 3                         0                           0

## 4                         0                           0

## 5                         0                           0

## 6                         0                           0

##   Occupation..Prof.specialty Occupation..Protective.serv Occupation..Sales

## 1                          0                           0                 0

## 2                          0                           0                 0

## 3                          0                           0                 0

## 4                          0                           0                 0

## 5                          1                           0                 0

## 6                          0                           0                 0

##   Occupation..Tech.support Occupation..Transport.moving

## 1                        0                            0

## 2                        0                            0

## 3                        0                            0

## 4                        0                            0

## 5                        0                            0

## 6                        0                            0

##   Relationship..Husband Relationship..Not.in.family

## 1                     0                           1

## 2                     1                           0

## 3                     0                           1

## 4                     1                           0

## 5                     0                           0

## 6                     0                           0

##   Relationship..Other.relative Relationship..Own.child

## 1                            0                       0

## 2                            0                       0

## 3                            0                       0

## 4                            0                       0

## 5                            0                       0

## 6                            0                       0

##   Relationship..Unmarried Relationship..Wife Race..Amer.Indian.Eskimo

## 1                       0                  0                        0

## 2                       0                  0                        0

## 3                       0                  0                        0

## 4                       0                  0                        0

## 5                       0                  1                        0

## 6                       0                  1                        0

##   Race..Asian.Pac.Islander Race..Black Race..Other Race..White Sex..Female

## 1                        0           0           0           1           0

## 2                        0           0           0           1           0

## 3                        0           0           0           1           0

## 4                        0           1           0           0           0

## 5                        0           1           0           0           1

## 6                        0           0           0           1           1

##   Sex..Male CapitalGain CapitalLoss HoursWeek NativeCountry...

## 1         1        2174           0        40                0

## 2         1           0           0        13                0

## 3         1           0           0        40                0

## 4         1           0           0        40                0

## 5         0           0           0        40                0

## 6         0           0           0        40                0

##   NativeCountry..Cambodia NativeCountry..Canada NativeCountry..China

## 1                       0                     0                    0

## 2                       0                     0                    0

## 3                       0                     0                    0

## 4                       0                     0                    0

## 5                       0                     0                    0

## 6                       0                     0                    0

##   NativeCountry..Columbia NativeCountry..Cuba

## 1                       0                   0

## 2                       0                   0

## 3                       0                   0

## 4                       0                   0

## 5                       0                   1

## 6                       0                   0

##   NativeCountry..Dominican.Republic NativeCountry..Ecuador

## 1                                 0                      0

## 2                                 0                      0

## 3                                 0                      0

## 4                                 0                      0

## 5                                 0                      0

## 6                                 0                      0

##   NativeCountry..El.Salvador NativeCountry..England NativeCountry..France

## 1                          0                      0                     0

## 2                          0                      0                     0

## 3                          0                      0                     0

## 4                          0                      0                     0

## 5                          0                      0                     0

## 6                          0                      0                     0

##   NativeCountry..Germany NativeCountry..Greece NativeCountry..Guatemala

## 1                      0                     0                        0

## 2                      0                     0                        0

## 3                      0                     0                        0

## 4                      0                     0                        0

## 5                      0                     0                        0

## 6                      0                     0                        0

##   NativeCountry..Haiti NativeCountry..Holand.Netherlands

## 1                    0                                 0

## 2                    0                                 0

## 3                    0                                 0

## 4                    0                                 0

## 5                    0                                 0

## 6                    0                                 0

##   NativeCountry..Honduras NativeCountry..Hong NativeCountry..Hungary

## 1                       0                   0                      0

## 2                       0                   0                      0

## 3                       0                   0                      0

## 4                       0                   0                      0

## 5                       0                   0                      0

## 6                       0                   0                      0

##   NativeCountry..India NativeCountry..Iran NativeCountry..Ireland

## 1                    0                   0                      0

## 2                    0                   0                      0

## 3                    0                   0                      0

## 4                    0                   0                      0

## 5                    0                   0                      0

## 6                    0                   0                      0

##   NativeCountry..Italy NativeCountry..Jamaica NativeCountry..Japan

## 1                    0                      0                    0

## 2                    0                      0                    0

## 3                    0                      0                    0

## 4                    0                      0                    0

## 5                    0                      0                    0

## 6                    0                      0                    0

##   NativeCountry..Laos NativeCountry..Mexico NativeCountry..Nicaragua

## 1                   0                     0                        0

## 2                   0                     0                        0

## 3                   0                     0                        0

## 4                   0                     0                        0

## 5                   0                     0                        0

## 6                   0                     0                        0

##   NativeCountry..Outlying.US.Guam.USVI.etc. NativeCountry..Peru

## 1                                         0                   0

## 2                                         0                   0

## 3                                         0                   0

## 4                                         0                   0

## 5                                         0                   0

## 6                                         0                   0

##   NativeCountry..Philippines NativeCountry..Poland NativeCountry..Portugal

## 1                          0                     0                       0

## 2                          0                     0                       0

## 3                          0                     0                       0

## 4                          0                     0                       0

## 5                          0                     0                       0

## 6                          0                     0                       0

##   NativeCountry..Puerto.Rico NativeCountry..Scotland NativeCountry..South

## 1                          0                       0                    0

## 2                          0                       0                    0

## 3                          0                       0                    0

## 4                          0                       0                    0

## 5                          0                       0                    0

## 6                          0                       0                    0

##   NativeCountry..Taiwan NativeCountry..Thailand

## 1                     0                       0

## 2                     0                       0

## 3                     0                       0

## 4                     0                       0

## 5                     0                       0

## 6                     0                       0

##   NativeCountry..Trinadad.Tobago NativeCountry..United.States

## 1                              0                            1

## 2                              0                            1

## 3                              0                            1

## 4                              0                            1

## 5                              0                            0

## 6                              0                            1

##   NativeCountry..Vietnam NativeCountry..Yugoslavia Income

## 1                      0                         0      0

## 2                      0                         0      0

## 3                      0                         0      0

## 4                      0                         0      0

## 5                      0                         0      0

## 6                      0                         0      0

str(adultsTrsf)

## ‘data.frame‘:    32561 obs. of  109 variables:

##  $ Age                                      : num  39 50 38 53 28 37 49 52 31 42 ...

##  $ Workclass...                             : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Workclass..Federal.gov                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Workclass..Local.gov                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Workclass..Never.worked                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Workclass..Private                       : num  0 0 1 1 1 1 1 0 1 1 ...

##  $ Workclass..Self.emp.inc                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Workclass..Self.emp.not.inc              : num  0 1 0 0 0 0 0 1 0 0 ...

##  $ Workclass..State.gov                     : num  1 0 0 0 0 0 0 0 0 0 ...

##  $ Workclass..Without.pay                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ FinalWeight                              : num  77516 83311 215646 234721 338409 ...

##  $ Education..10th                          : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..11th                          : num  0 0 0 1 0 0 0 0 0 0 ...

##  $ Education..12th                          : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..1st.4th                       : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..5th.6th                       : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..7th.8th                       : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..9th                           : num  0 0 0 0 0 0 1 0 0 0 ...

##  $ Education..Assoc.acdm                    : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..Assoc.voc                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..Bachelors                     : num  1 1 0 0 1 0 0 0 0 1 ...

##  $ Education..Doctorate                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..HS.grad                       : num  0 0 1 0 0 0 0 1 0 0 ...

##  $ Education..Masters                       : num  0 0 0 0 0 1 0 0 1 0 ...

##  $ Education..Preschool                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..Prof.school                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education..Some.college                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Education.Numer                          : num  13 13 9 7 13 14 5 9 14 13 ...

##  $ MaritalStatus..Divorced                  : num  0 0 1 0 0 0 0 0 0 0 ...

##  $ MaritalStatus..Married.AF.spouse         : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ MaritalStatus..Married.civ.spouse        : num  0 1 0 1 1 1 0 1 0 1 ...

##  $ MaritalStatus..Married.spouse.absent     : num  0 0 0 0 0 0 1 0 0 0 ...

##  $ MaritalStatus..Never.married             : num  1 0 0 0 0 0 0 0 1 0 ...

##  $ MaritalStatus..Separated                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ MaritalStatus..Widowed                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation...                            : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Adm.clerical                 : num  1 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Armed.Forces                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Craft.repair                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Exec.managerial              : num  0 1 0 0 0 1 0 1 0 1 ...

##  $ Occupation..Farming.fishing              : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Handlers.cleaners            : num  0 0 1 1 0 0 0 0 0 0 ...

##  $ Occupation..Machine.op.inspct            : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Other.service                : num  0 0 0 0 0 0 1 0 0 0 ...

##  $ Occupation..Priv.house.serv              : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Prof.specialty               : num  0 0 0 0 1 0 0 0 1 0 ...

##  $ Occupation..Protective.serv              : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Sales                        : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Tech.support                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Occupation..Transport.moving             : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Relationship..Husband                    : num  0 1 0 1 0 0 0 1 0 1 ...

##  $ Relationship..Not.in.family              : num  1 0 1 0 0 0 1 0 1 0 ...

##  $ Relationship..Other.relative             : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Relationship..Own.child                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Relationship..Unmarried                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Relationship..Wife                       : num  0 0 0 0 1 1 0 0 0 0 ...

##  $ Race..Amer.Indian.Eskimo                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Race..Asian.Pac.Islander                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Race..Black                              : num  0 0 0 1 1 0 1 0 0 0 ...

##  $ Race..Other                              : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ Race..White                              : num  1 1 1 0 0 1 0 1 1 1 ...

##  $ Sex..Female                              : num  0 0 0 0 1 1 1 0 1 0 ...

##  $ Sex..Male                                : num  1 1 1 1 0 0 0 1 0 1 ...

##  $ CapitalGain                              : num  2174 0 0 0 0 ...

##  $ CapitalLoss                              : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ HoursWeek                                : num  40 13 40 40 40 40 16 45 50 40 ...

##  $ NativeCountry...                         : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Cambodia                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Canada                    : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..China                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Columbia                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Cuba                      : num  0 0 0 0 1 0 0 0 0 0 ...

##  $ NativeCountry..Dominican.Republic        : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Ecuador                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..El.Salvador               : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..England                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..France                    : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Germany                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Greece                    : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Guatemala                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Haiti                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Holand.Netherlands        : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Honduras                  : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Hong                      : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Hungary                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..India                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Iran                      : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Ireland                   : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Italy                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Jamaica                   : num  0 0 0 0 0 0 1 0 0 0 ...

##  $ NativeCountry..Japan                     : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Laos                      : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Mexico                    : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Nicaragua                 : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Outlying.US.Guam.USVI.etc.: num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Peru                      : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Philippines               : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Poland                    : num  0 0 0 0 0 0 0 0 0 0 ...

##  $ NativeCountry..Portugal                  : num  0 0 0 0 0 0 0 0 0 0 ...

##   [list output truncated]

# 计算变量间相关系数的p值

cor.prob<-function(X,dfr=nrow(X)-2){

R<-cor(X,use="pairwise.complete.obs")

above<-row(R)<col(R)

r2<-R[above]^2

Fstat<-r2*dfr/(1-r2)

R[above]<-1-pf(Fstat,1,dfr)

R[row(R)==col(R)]<-NA

R

}

# 将数据整理成data.frame形式

flattenSquareMatrix<-function(m){

if((class(m) !="matrix") | (nrow(m) !=ncol(m))) stop("Must be asquare matrix.")

if(!identical(rownames(m),colnames(m))) stop("Row and column names must be equal.")

ut<-upper.tri(m)

data.frame(i=rownames(m)[row(m)[ut]],

j=rownames(m)[col(m)[ut]],

cor=t(m)[ut],

p=m[ut])

}

corMasterList<-flattenSquareMatrix(cor.prob(adultsTrsf))

dim(corMasterList)

## [1] 5886    4

head(corMasterList)

##                        i                      j      cor         p

## 1                    Age           Workclass...  0.04263 1.410e-14

## 2                    Age Workclass..Federal.gov  0.05123 0.000e+00

## 3           Workclass... Workclass..Federal.gov -0.04261 1.454e-14

## 4                    Age   Workclass..Local.gov  0.06090 0.000e+00

## 5           Workclass...   Workclass..Local.gov -0.06407 0.000e+00

## 6 Workclass..Federal.gov   Workclass..Local.gov -0.04568 1.110e-16

corList<-corMasterList[order(-abs(corMasterList$cor)),]

head(corList)

##                                      i                            j

## 1953                       Sex..Female                    Sex..Male

## 597                       Workclass...                Occupation...

## 1256 MaritalStatus..Married.civ.spouse        Relationship..Husband

## 1829                       Race..Black                  Race..White

## 527  MaritalStatus..Married.civ.spouse MaritalStatus..Never.married

## 1881             Relationship..Husband                  Sex..Female

##          cor p

## 1953 -1.0000 0

## 597   0.9980 0

## 1256  0.8932 0

## 1829 -0.7887 0

## 527  -0.6449 0

## 1881 -0.5801 0

#选择与Income相关系数绝对值大于0.2的数据集

selectedSub<-subset(corList,(abs(cor)>0.2 & j =="Income"))

bestSub<-as.character(selectedSub$i[c(1,3,5,6,8,9)])

library(psych)

##

## Attaching package: ‘psych‘

##

## 下列对象被屏蔽了from ‘package:ggplot2‘:

##

##     %+%

pairs.panels(adultsTrsf[c(bestSub,"Income")])

时间: 2024-08-29 12:08:34

R语言:利用相关性分析对复杂数据进行数据探索的相关文章

R语言之相关性分析

两个变量或两组变量之间的联系,对于连续变量称为相关性,对于分类变量称为关联性. 一.连续变量间的相关性常用命令及选项如下 使用方法如下:1.计算相关系数及相关系数矩阵 > cor(count,speed)[1] 0.7237206 > cor(count,speed,method = "spearman")[1] 0.5269556 > cor(mf)           Length       Speed      Algae         NO3       

R语言重要数据集分析研究——需要整理分析阐明理念

1.R语言重要数据集分析研究需要整理分析阐明理念? 上一节讲了R语言作图,本节来讲讲当你拿到一个数据集的时候如何下手分析,数据分析的第一步,探索性数据分析. 统计量,即统计学里面关注的数据集的几个指标,常用的如下:最小值,最大值,四分位数,均值,中位数,众数,方差,标准差,极差,偏度,峰度 先来解释一下各个量得含义,浅显就不说了,这里主要说一下不常见的 众数:出现次数最多的 方差:每个样本值与均值的差得平方和的平均数 标准差:又称均方差,是方差的二次方根,用来衡量一个数据集的集中性 极差:最大值

【数据分析 R语言实战】学习笔记 第五章 数据的描述性分析(上)

5.1R内置的分布 分布是描述一个样本数据最核心.最重要的方式.R内嵌了很多常用的统计分布,提供了四类函数:概率密度函数(density),累积分布函数(probability).分位数(quantile)和伪随机数(random).在R中分别用d,p,q,r表示这4个项目,后面接分布的英文名称或缩写. 5.2集中趋势的分析 5.2.1集中趋势的测度 描述统计分布集中趋势的指标主要是平均数.中位数.众数,也称为“平均指标”.这些指标的主要作用包括: 反映总体各单位变量分布的集中趋势和一般水平;

【数据分析 R语言实战】学习笔记 第三章 数据预处理 (下)

3.3缺失值处理 R中缺失值以NA表示,判断数据是否存在缺失值的函数有两个,最基本的函数是is.na()它可以应用于向量.数据框等多种对象,返回逻辑值. > attach(data) The following objects are masked fromdata (pos = 3): city, price, salary > data$salary=replace(salary,salary>5,NA) > is.na(salary) [1] FALSEFALSE TRUE

R语言学习-词频分析

概念 1.语料库-Corpus 语料库是我们要分析的所有文档的集合,就是需要为哪些文档来做词频 2.中文分词-Chinese Word Segmentation 指的是将一个汉字序列切分成一个一个单独的词语. 3.停用词-Stop Words 数据处理的时候,自动过滤掉某些字或词,包括泛滥的词如Web.网站等,又如语气助词如的.地.得等. 需要加载的包 1.tm包 安装方式:install.packages("tm") 语料库: Corpus(x,readerControl) x-语料

【数据分析 R语言实战】学习笔记 第四章 数据的图形描述

4.1 R绘图概述 以下两个函数,可以分别展示二维,三维图形的示例: >demo(graphics) >demo(persp) R提供了多种绘图相关的命令,可分成三类: 高级绘图命令:在图形设备上产生一个新的图区,它可能包括坐标轴.标签.标题等. 低级绘图命令:在一个己经存在的图形上加上更多的图形元素,如额外的点.线和标签. 交互式图形命令:允许交互式地用鼠标在一个已经存在的图形.上添加图形信息或者提取图形信息. 使用R语言作图,主要按照以下步骤进行: ①取原始数据,准备好绘图需要的变量. ②

R语言重要数据集分析研究——搞清数据的由来

搞清数据的由来 作者:李雪丽 资料来源:百度百科

基于R语言的用户分析

1. 基本分析理论 C5.0是决策树模型中的算法,79年由J R Quinlan发展,并提出了ID3算法,主要针对离散型属性数据,其后又不断的改进,形成C4.5,它在ID3基础上增加了队连续属性的离散化.C5.0是C4.5应用于大数据集上的分类算法,主要在执行效率和内存使用方面进行了改进.C4.5算法是ID3算法的修订版,采用GainRatio来加以改进方法,选取有最大GainRatio的分割变量作为准则,避免ID3算法过度配适的问题.C5.0算法则是C4.5算法的修订版,适用于处理大数据集,采

R语言重要数据集分析研究——&#160; 数据集本身的分析技巧

数据集本身的分析技巧           作者:王立敏           文章来源:网络 1.数据集 数据集,又称为资料集.数据集合或资料集合,是一种由数据所组成的集合. Data set(或dataset)是一个数据的集合,通常以表格形式出现.每一列代表一个特定变量.每一行都对应于某一成员的数据集的问题.它列出的价值观为每一个变量,如身高和体重的一个物体或价值的随机数.每个数值被称为数据资料.对应于行数,该数据集的数据可能包括一个或多个成员. 2.数据分析 数据结构 创建向量和矩阵 函数c(