統計與計量‎ > ‎

敘述性統計與相關度分析

程式碼

rm(list=ls())

sink("RE-2.log")

RE.Summary <- NULL
RE.NumFields <- NULL

LoadData <- function(csvFile='RE.csv') {
  RE.Data <- read.csv(csvFile)
}
  
SummaryData <- function(RE=RE.Data) {
  options(digits=1)
  
  fieldNames <- colnames(RE)
  i = 0
  for (fieldName in fieldNames) {
    i = i + 1
    if (class(RE[, fieldName]) == 'numeric') {
      assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)      
      Summary.Field <- c(summary(RE[, fieldName]), sd(RE[, fieldName]))
      
      if (is.null(RE.Summary)) {
        assign('RE.Summary', Summary.Field, envir = .GlobalEnv)
      }
      else {
        assign('RE.Summary', rbind(RE.Summary, Summary.Field), envir = .GlobalEnv)
      }
    }
  }
  
  i = 1
  for (fieldName in fieldNames) {
    if (class(RE[, fieldName]) == 'numeric') {
      rownames(RE.Summary)[i] <- fieldName 
      i = i + 1
    }
  }
  
  colnames(RE.Summary)[7] <- 'StdDev.'
  RE.Summary
}

CoVarianceData <- function(RE=RE.Data) {
  options(digits=2)
  
  if (is.null(RE.NumFields)) {
    fieldNames <- colnames(RE)
    i = 0

    for (fieldName in fieldNames) {
      i = i + 1
      if (class(RE[, fieldName]) == 'numeric') {
        assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)      
      }
    }
  }
  
  cov(RE[, RE.NumFields])
  
}

CoRelationData <- function(RE=RE.Data) {
  options(digits=2)
  
  if (is.null(RE.NumFields)) {
    fieldNames <- colnames(RE)
    i = 0
    
    for (fieldName in fieldNames) {
      i = i + 1
      if (class(RE[, fieldName]) == 'numeric') {
        assign('RE.NumFields', c(RE.NumFields, i), envir = .GlobalEnv)      
      }
    }
  }
  
  cor(RE[, RE.NumFields])
  
}

RE.Data <- LoadData()
SummaryData(RE.Data)
CoRelationData(RE.Data)
CoVarianceData(RE.Data)

sink()

輸出

         Min. 1st Qu. Median Mean 3rd Qu. Max. StdDev.
FactorX1  4.3     5.1      6    6       6    8     0.8
FactorY1  2.0     2.8      3    3       3    4     0.4
FactorX2  1.0     1.6      4    4       5    7     1.8
FactorY2  0.1     0.3      1    1       2    2     0.8

         FactorX1 FactorY1 FactorX2 FactorY2
FactorX1     1.00    -0.12     0.87     0.82
FactorY1    -0.12     1.00    -0.43    -0.37
FactorX2     0.87    -0.43     1.00     0.96
FactorY2     0.82    -0.37     0.96     1.00

         FactorX1 FactorY1 FactorX2 FactorY2
FactorX1    0.686   -0.042     1.27     0.52
FactorY1   -0.042    0.190    -0.33    -0.12
FactorX2    1.274   -0.330     3.12     1.30
FactorY2    0.516   -0.122     1.30     0.58
ċ
RE-2.R
(2k)
李智,
2015年5月14日 下午6:56
ċ
RE.csv
(3k)
李智,
2015年5月14日 下午6:57