2016年9月20日火曜日

RStudio : Boxplot

RStudioでBoxplotを書きます。

1.データのインポート
> awareness_mean1=c(11,11.15384615,11.56521739,10.63636364,11.13157895,10.55555556,9.833333333,9.571428571,9.838709677,7.678571429,9.176470588,7.333333333,6.909090909)
> decision_mean1=c(10.38461538,10.69230769,11.7826087,10.72727273,11.15789474,11,9.666666667,9.214285714,9.612903226,7.607142857,8.352941176,7.333333333,7)
> comunication_mean1=c(11.38461538,11.92307692,11.7826087,10.61363636,10.57894737,10.55555556,9,9.571428571,9.387096774,7.464285714,8.352941176,8.111111111,6.545454545)
> leadership_mean1=c(10.76923077,10.61538462,11.73913043,10.40909091,10.28947368,10,9.166666667,8.285714286,9.096774194,7.035714286,7.823529412,6.555555556,6.545454545)

2.グラフの描画
> boxplot(awareness_mean1,decision_mean1,comunication_mean1,leadership_mean1, main='BoxPlot for mean of each category',names = c('Awareness', 'Decision', 'Communica','Leadership'))


もう一つ例
> awareness_variance1=c(1.976331361,3.146683673,3.167533819,3.244897959,2,2.851211073,0.805555556,2.082644628,1.135802469,1.538461538,1.685950413,1.114265928,0.767485822)
> decision_variance1=c(1.905325444,5.095663265,4.172736733,3.168367347,1.777777778,2.46366782,1.222222222,2.727272727,1.111111111,2.236686391,1.652892562,0.922437673,0.691871456)
> comunication_variance1=c(0.071005917,5.677295918,4.172736733,3.673469388,2.987654321,3.404844291,1.333333333,2.975206612,1.135802469,1.621301775,1.646177686,0.822714681,0.517958412)
> leadership_variance1=c(10.23668639,6.605867347,5.635796046,4.918367347,3.135802469,2.851211073,2.472222222,1.884297521,1.777777778,1.562130178,1.559917355,1.100415512,0.801512287)
> boxplot(awareness_variance1,decision_variance1,comunication_variance1,leadership_variance1, main='BoxPlot for variance of each category',names = c('Awareness', 'Decision', 'Communica','Leadership'))


RStudio : Bartlett Test

RStudioを使って多重比較のための等分散性の検定を実施します。

帰無仮説:全てのカテゴリの母分散が等しい
対立仮説:少なくとも一つの母分散が他の母分散と異なる

1.データを入力
> score=c(11,10.38461538,11.38461538,10.76923077,11.15384615,10.69230769,11.92307692,10.61538462,11.56521739,11.7826087,11.7826087,11.73913043,10.63636364,10.72727273,10.61363636,10.40909091,11.13157895,11.15789474,10.57894737,10.28947368,10.55555556,11,10.55555556,10,9.833333333,9.666666667,9,9.166666667,9.571428571,9.214285714,9.571428571,8.285714286,9.838709677,9.612903226,9.387096774,9.096774194,7.678571429,7.607142857,7.464285714,7.035714286,9.176470588,8.352941176,8.352941176,7.823529412,7.333333333,7.333333333,8.111111111,6.555555556,6.909090909,7,6.545454545,6.545454545)
 
2. ラベルと標本数
> group=factor(rep(c("Awareness", "Decision", "Comunication", "Leadership"), c(13, 13, 13, 13)))
 
3.テスト実施 
> bartlett.test(score ~ group)

 Bartlett test of homogeneity of variances

data:  score by group
Bartlett's K-squared = 5.9198, df = 3, p-value = 0.1156
 
4.結果
p-value = 0.1156なので各群の母分散は等しくないと言えない(等しいとして次の検定を実施)。この場合は Bonferroniを実施

もう1件例を書いておく
> score_v=c(1.976331361,1.905325444,0.071005917,10.23668639,3.146683673,5.095663265,5.677295918,6.605867347,3.167533819,4.172736733,4.172736733,5.635796046,3.244897959,3.168367347,3.673469388,4.918367347,2,1.777777778,2.987654321,3.135802469,2.851211073,2.46366782,3.404844291,2.851211073,0.805555556,1.222222222,1.333333333,2.472222222,2.082644628,2.727272727,2.975206612,1.884297521,1.135802469,1.111111111,1.135802469,1.777777778,1.538461538,2.236686391,1.621301775,1.562130178,1.685950413,1.652892562,1.646177686,1.559917355,1.114265928,0.922437673,0.822714681,1.100415512,0.767485822,0.691871456,0.517958412,0.801512287)
> group=factor(rep(c("Awareness", "Decision", "Comunication", "Leadership"), c(13, 13, 13, 13)))
> bartlett.test(score_v ~ group)

 Bartlett test of homogeneity of variances

data:  score_v by group
Bartlett's K-squared = 40.782, df = 3, p-value = 7.274e-09

今回は有意差ありなので、等分散性は棄却される。この場合は  Steel–Dwass nonparametric testを実施。

参考
http://bio-info.biz/tips/r_bartlett_test.html

2016年7月6日水曜日

形態素解析の方法

YahooデベロッパーネットワークのAPIで日本語形態素解析をやってみました。
http://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html

1.アプリケーションIDの準備
https://e.developer.yahoo.co.jp/register
からアプリケーション情報の入力をして、アプリケーションIDを取得します。
取得したIDで試しに動かしてみます。
http://jlp.yahooapis.jp/MAService/V1/parse?appid=取得したID&results=uniq&sentence=解析したい文章
これをブラウザで動かしてみると
のようになります。

2.格納用テーブルの用意
今回は、下記を使います。
・claims(文章が入っているテーブル)
・morphems(解析結果が入るテーブル)
・categories(品詞が入っているテーブル)
morphemsテーブルの構造は図のようになっています。





3.コードの追加
今回はController/ClaimsController.phpのviewアクションを利用してやりました。



public function view2($id = null) {
                if (!$this->Claim->exists($id)) {
                        throw new NotFoundException(__('Invalid claim'));
                }
                $options = array('conditions' => array('Claim.' . $this->Claim->primaryKey => $id));
                $this->set('claim', $this->Claim->find('first', $options));

                $claim = $this->Claim->find('first', $options);
                if($claim['Claim']['chk'] == 0 ){

                $appid = '自分のID';
                $url = "http://jlp.yahooapis.jp/MAService/V1/parse?appid=".$appid."&results=uniq";
                $url .= "&sentence=".urlencode($claim['Claim']['naiyou']);
                $xml  = simplexml_load_file($url);

                APP::import('Model', 'Morphem');
                $this->Morphem = new Morphem;
                APP::import('Model', 'Category');
                $this->Category = new Category;

                foreach ($xml->uniq_result->word_list->word as $cur){

                        $category_id = $this->Category->find('first',
                                array(
                                        'fields' => array('id'),
                                        'conditions' => array('Category.name' => (string)$cur->pos),
                                )
                        );

                        $this->Morphem->create();
                        $data = array(
                                'claim_id' => $id,
                                'word' =>(string)$cur->surface,
                                'category_id' => (int)$category_id['Category']['id'],
                                'count' =>(int)$cur->count,
                        );

                        $this->Morphem->save($data);

                 
                }

                        $data1 = array(
                                'id' => $id,
                                'chk' => 1
                        );
                        $this->Claim->save($data1);
                }
        }

図のように作成され、テーブルに保存します。






2016年7月5日火曜日

MySQLの移行

サーバーを移行するにあたって、MySQLの移行をしました。

バックアップ取得
SSHでログイン後、対象ユーザに一応なっておいてやります。
mysqldump -uuser_name -puser_password -r backup.sql --single-transaction DB_name
プロンプトが返ってくるとbackup.sqlができています。
これを(ローカルにダウンロードしてから)対象サーバにアップロードします。

サーバ間でファイルをFTPで送る場合は、クライアントのインストールが必要でした。
 yum -y install ftp

アップロード後、移行先サーバでデータベースをインポートします(ユーザは発行済で)
mysql -uuser_name -puser_password DB_name < backup.sql

参考
http://qiita.com/rato303/items/2e614f23e5feee150ffc
http://d.hatena.ne.jp/pospome/20130604/1370333526