www.crosshyou.info

政府統計の総合窓口のデータや、OECDやUCIやのデータを使って、Rの練習をしています。ときどき、読書記録も載せています。

World Bank Population living in slums data analysis 3 - Which country has decreased living in slums most?

Generated by Bing Image Creator: Long view from the sky, high mountains and green grass fields, photo

www.crosshyou.info

This post is following of the above post.

In the previous post, I find data start from 2000 to 2020. So, let's see how each countries changed living in slums.

First, I make data frame which contains only 2000 data.

I also make a data frame which only contains 2020 data.

I use inner_join() function to merge the both data frames.

Let's see a histogram of "change".

Some countries have increased living in slums, but almost countries has decreased.

Let's see which country has increased\decreased living in slums?

Angola, Mynmar, Iraq, Cuba, Suriname, Guinea and  Ireland has inclrased living in slums and Burkina Faso has the most decreased.

Let's make a scatter plot living in slums vs. sulums_2000.

I see there is negative correlation between living in slums and change.

Then, let's see summary statistics for "change" by Region.

East Asia and Pacific has the lowest avarage change, Latin America and Carribbean has the high est one.

In South Asia, there is no country which ahs increased living in slums.

Let's see summary "change" statistics by IncomeGroup.

High income group has the highest change average while Low income group has the lowest average change. It is no surprise.

That's it. Thank you!

To read from the 1st pot,

www.crosshyou.info

World Bank Population living in slums data analysis 2 - making histograms using R

Generated by Bing Image Creator: Beautiful flower bucket, photo

www.crosshyou.info

This post is following of the above post. In the previous post, I load Population living in slums data into R. In this post, I will make histograms using R to get sense of how those data are distributed.

First, I make overall histogram.

There seems 2 peaks in the overall distribution.

Next, let's see by Region histogram.

There are 6 regions. Europe and Central Asisa region has low population living in sulums.

Sub_Saharan Africa has high population living in sulums.

Next, let's see by IncomeGroup.

There are 4 income groups. High income groupe has low population, Low income group has high population.

Next, let's see by year

I see data start from 2000 to 2020, every two years. I don't see particular pattern. It seems every years have similar distribution.

Let's see time trend line chart by each country.

It seems over all trend is decreasing. Let's confirm it.

Overall mean population living is slums is degreasing.

To summarize this post,

1. Over all population living in slums has two peak distribution.

2. Sub-Saharan Africa regiion has high population while Europe and Central Asia has low population.

3. Low income group has high population while High income group has low population.

4. Population living is slums is decreasing in the world.

That's it. Thank you!

Next post is

www.crosshyou.info

 



To read from the 1st post,

www.crosshyou.info

 









 

World Bank Population living in slums data analysis 1 - import CSV file data into R and tidying data.

Generated by Bing Image Creator: Local area photograph, apple trees, blue sky and white clouds

In this post, I will analyze World Bank Population living in slums data using R.

Population living in slums (% of urban population) | Data (worldbank.org)

I got below CSV files.

First, I load tidyverse package.

Then, I use read_csv() function to load CSV file

Since df_raw data frmae is wide format or not tidy data, I need to convert it to tidy data frame. I use pivot_longer() function.

Next, I merge df and country_meta_data using inner_join() function

All right. Now I have a tidy data frame named "df".

That's it. Thank you!

Next post is

www.crosshyou.info

 

UCI の Adult データの分析6 - R の rpart パッケージで決定木での分類 (Classification)

Generated by Bing Image Creator: 遠くの空からの風景写真。牧場に牛が数頭いる。可愛い青い花がたくさん咲いている。

www.crosshyou.info

の続きです。

今回は R の rpart パッケージで決定木の方法で分類に挑戦してみたいと思います。
まず、rpart, rpart.plot パッケージを読み込みします。

rpart では、glmnet や FNN と違いデータフレームを使うので、トレーニング用のデータフレームとテスト用のデータフレームを作成します。

これで準備完了です。まず、raprt 関数で最初の決定木をつくります。

rpart のパラメータは、cp(complexity parameter) というもので、決定木をどれだけ複雑にするかのパラメータです。

この cp と 決定木の予測の誤差、xerror をグラフにしてみます。

最適な cp を確認します。

この最適な cp の水準で作成した決定木を剪定して、最終モデルにします。

この fit_rpart をグラフにしてみます。

けっこう多く枝分かれしていますね。

predict() 関数で予測します。

あら、エラーメッセージが出てしまいました。native_country には新しい水準があります、と出ました。しかたないので、native_country を削除してやり直します。

こんどはエラーメッセージがでませんでした。

予測結果をみてみましょう。

正解率は、86% でした。

glmnet パッケージでの Elastic-Net Regression や FNN パッケージでの k-nearest neighbors よりも高い正解率となりました。

今回は以上です。

初めから読むには、

www.crosshyou.info

です。

読書記録 - 「食の歴史」 ジャック アタリ 著 プレジデント社

食の歴史

食の歴史

Amazon

知人が、面白いと言っていたので読んでみました。

面白かったです。

北京原人やアウストラロピテクスの頃から現在までの食の歴史、そしてこれからがコンパクト(といっても 360 ページくらい) にまとめられています。

食事の時間は他者と関わる大切な時間だけれども、現代人は一人で、短時間で食べることが多くなっているということが繰り返し書かれていします。

コカ・コーラ社やペプシコ社、ナビスコ社、ネスレ社、ユニリーバ社など現代の大きな食品会社の由来がたくさんあって、いろいろと知ることができてよかったです。

毎日の食事にもっと気をつけようと思いました。

UCI の Adult データの分析5 - R の FNN パッケージで、k-nearest neighbors での Classification



Generated by Bing Image Creator: 広い野原に小川と小道があって、菜の花がいっぱい咲いていて、空は青空の写真

www.crosshyou.info

の続きです。

今回は、R の FNN パッケージで、k-nearest neighbors での分類をします。

まずは、FNN パッケージの読み込みをします。

k-nearest neighbors のパラメータは k ですが、最適な k を求めるために、mtx_train をさらにトレーニング用、テスト用のデータにわけます。

for loop で前回にように最適な k を求めます。

for loop が私の PC(Thinkbook 15-IIL) だと 10分ぐらいかかりました。

correct_boxのグラフをみます。

correct じゃなくて、collect とすべきでした。

k = 40 より大きい値がいいようですね。

best_k は 40でした。 k は奇数のほうがいいので、グラフをみると、39のほうが41よりもよさそうなので、k = 39 でやってみます。

成果率は、82.9% でした。

今回は以上です。

次回は、

www.crosshyou.info

です。

初めから読むには、

www.crosshyou.info

です。

UCI の Adult データの分析4- R の glmnet パッケージを使って、 LASSO, Ridge, Elastic-Net Regression で income の分類に挑戦

Generated by Bing Image Creator: Photo of old times, beautiful mountains scenery, blue sky and white clouds, cheerful flowers 

www.crosshyou.info

の続きです。前回は、 glmnet パッケージで income の分類をするための前準備でした。今回は、実際に分類してみます。

まずは、LASSO Regression です。alpha = 1 にします。今回は、 >50K か <=50K かという 2値の分類なので、family = "binomial" とします。parallel = TRUE で並列処理をしています。

fit_lasso を plot() 関数で処理してみます。

モデルの係数をみます。

Japanの係数が 0.63 なので、他の変数が全て同じならば、native country が日本の人のほうが、そうでない人よりも >50K の確率は高いです。

predict() 関数でテスト用データの予測をします。

実際のデータとどれだけあっているかみてみましょう。

正解率は、(6903 + 1417) / (6903 + 924 + 525 + 1417) = 85.2% でした。

けっこういい正解率なのではないでしょうか?

仮に トレーニング用の >50K の比率で、デタラメにテスト用の予想をしたらどのくらいでしょうか?

デタラメ予想だと、63% の正解率なので、LASSO Regression で予測したほうがいいですね。

同じように、alpha = 0 だとRidge Regression になります。

plot() 関数で fit_ridge のグラフを描いてみます。

係数は Japanのところだけみてみましょう。

0.55 とプラスの係数でした。

テスト用のデータで予測してみましょう。

Ridge Regression での正解率は、84.4% でした。 

Elastic-Net Regression は alpha が 0 ~ 1 のあいだの値です。どのくらいの alpha の水準がいいのかは、いろいろな alpha 水準を試してみないとわかりません。

for loop でいろいろと試してみます。

上の for loop のコードですが、計算が終わるのに 10分以上かかりました。

mse_box の中身をグラフにしてみてみます。

mse_box の値が一番小さなところが最適な alpha の水準です。

この alpha の水準で ELastic-Net Regression をします。

fit_elnet を plot() でグラフにしてみます。

係数の確認は割愛して、予測とその結果をみてみます。

正解率は、85.2% でした。LASSO よりも少しだけ良い結果でした。

3つの正解率をまとめて表示してみます。

今回は以上です。

次回は

www.crosshyou.info

です。

初めから読むには、

www.crosshyou.info

です。