#=============================================================================== # # Section 1 基礎 # #=============================================================================== ###[解説]英語の文が、名詞と動詞からなるように、人工言語も名詞(オブジェクト)と ### 述語(関数)がある。また、修飾語に相当するものをオプションという。 #------------------------------------------------------------------------------ ## 1.1 オブジェクト a = c(1,2,4,8,12) ## 1.2 関数 ### 基本的な統計量 mean(a) ?mean sd(a) max(a) max(a) - min(a) 1+1 "+"(1,1) ## 1.3 オプション a = c(1,2,4,8,12, NA) mean(a) a = c(1,2,4,8,12, NA) mean(a, na.rm = TRUE) #=============================================================================== # # Section 2 ファイルの操作 # #=============================================================================== ###[解説]Rがパソコンの中にある別のファイルなどにアクセスをする場合、 ### まず、場所を指定する。この場所をディレクトリと言い、そのディレクトリへ ###   パソコンのデフォルトの位置からどのような経路で到達するかを示したものをパス(path)という。 ###[関数]  ### setwd: 現在のワーキングディレクトリを指定する関数 ### read.csv: csv.fileを読み込む関数 #------------------------------------------------------------------------------ # 2.1 ディレクトリの操作 myDirectory = "C:ここは自分で設定してね" setwd(myDirectory) fileName = "results.csv" results = read.csv(fileName, head = T, encoding = "Shift-8") #------------------------------------------------------------------------------ # 2.2 中身(データフレーム)の確認 #------------------------------------------------------------------------------ ###[関数]  head: 上から6つ示す関数 head(results) ###[関数]  "$": 列を指定する関数 "$"(results, "ID") results$ID ###[関数]  "[": n番目の要素を取り出す関数 a = results$ID "["(a,1) a[1] results$ID[1] results$age[100] results[1,1] results[1,1:6] results[1:6,1:6] ###[関数]  c: 要素をまとめて(concatenate)、ベクトルを作る関数 index = c(1,2,4,100) results.temp = results[index, 1] head(results.temp) ###[関数]  which: 条件に合う出席番号を返す関数 index = which(results$age < 21) results.temp = results[index,] head(results.temp) ###[関数]  grep: 条件に合う出席番号を返す関数 index = grep("^e_", results$Label, perl = TRUE) results = results[index,] head(results) ###[関数]  xtabs: 集計表を作る関数 xtabs( ~ Label, results) xtabs( ~ Answer + Label, results) ###[関数]  intersect: 条件が合うものに絞る関数 index1 = grep("^e_", results$Label, perl = TRUE) index2 = grep("^x", results$Answer, perl = TRUE, invert = TRUE) index = intersect(index1, index2) results = results[index,] xtabs( ~ Answer + Label, results) #=============================================================================== # # Section 3 線形モデルによる分析 # #------------------------------------------------------------------------------ ###[関数]  lm: 線形回帰を行う関数 ## t-test: Gender lm1 = lm( Answer ~gender , results) summary(lm1) index = which(results$gender != "noanswer") nrow(results) results = results[index,] nrow(results) lm1 = lm( Answer ~gender , results) summary(lm1) ## anova: Gender + age summary(results$age) xtabs1 = xtabs( ~ age, results) barplot(xtabs1) results$age2 = 0 index = which(results$age <= 30) results$age2[index] = 1 table(results$age2) lm2 = lm( Answer ~gender + age2 , results) summary(lm2) lm3 = lm( Answer ~gender + age2 + gender * age2, results) summary(lm3) ###[関数]  AIC: モデルのAICを計算する関数 AIC(lm1, lm2, lm3) BIC(lm1, lm2, lm3) ###[関数]  install.packages: 追加パッケージをインストールする関数 ### [解説] 標準搭載されているもの以上のもを使うときには、パッケージをインストールする必要がある install.packages("beepr") ###[関数]  library: 追加されたパッケージを読み込む関数 beep(1) library(beepr) ###[関数]  beep: 音を鳴らす関数 beep(1) beep(5) beep(2) # 楽しくなる beep(8) # とても楽しくなる ### stepwise法 ###[関数]  stepAIC: ステップワイズ法を行う関数 library(MASS) stepAIC(lm3) ### All subsets regression ###[関数]  regsubsets: モデル選択を行う関数 install.packages("leaps") library(leaps) regsubsets1 = regsubsets( Answer ~ age2*gender, data =results ) plot(regsubsets1) ## anova: neg * zero index = grep("[cd]", results$Label, perl = TRUE) results$zero = 0 results$zero[index] = 1 index = grep("[bd]", results$Label, perl = TRUE) results$neg = 0 results$neg[index] = 1 regsubsets2 = regsubsets( Answer ~ neg*zero, data =results) plot(regsubsets2, scale = "bic")