Rで何かをしたり、読書をするブログ

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

都道府県別の自動車(バス)輸送統計調査のデータ分析2 - R のggplot() + geom_histogram() でヒストグラム

Bing Image Creator で生成: 透明感のある風景写真。大きな滝とスイセンの花

www.crosshyou.info

の続きです。

前回は、CSV ファイルを R に読み込ませてデータフレームを作成しました。

今回は読み込んだデータの視覚化、ヒストグラムを描いてみます。

まず、合計 のデータを削除しておきます。

general_num のヒストグラムを描きます。

ほとんどの観測データが左側に集中している分布形状です。こういう場合は、対数変換した値でヒストグラムを作成するとわかりやすいです。

若干ですが、2000年度よりも2022年度のほうが分布の中心が右側に移動しているように見えます。

次は、highway_num です。

これも対数変換してみます。

警告メッセージで3つデータが削除されたとあります、これは滋賀県ですね。

こちらのヒストグラムでは、よりはっきりと2022年度が右方向に移動していることがわかります。

次は、rental_num です。

これも対数変換します。

これも2022年度の分布が右方向へ移動しているようです。

次は、general_kilo です。

こちらも対数変換します。

general_kilo も2022年度のほうが右方向に分布が移動していますね。

次は、highway_kilo です。

これも対数変換します。

こちらも分布の中心が右方向に移動しているのが確認できますね。

最後の変数は、rental_kilo です。

これもやっぱり対数変換します。

いままでの変数と同じく、rental_kilo も分布が右方向に移動しています。

以上、各変数の分布のようすとヒストグラムでみてみました。

どれも、2020年度、2021年度、2022年度と年を経るにつれて、分布の位置が右方向に動いています。つまり、年々、増加しているということです。

今回は以上です。

次回は、

www.crosshyou.info

です。

 

初めから読むには、

www.crosshyou.info

です。

都道府県別の自動車(バス)輸送統計調査のデータ分析1 - Rにデータを読み込ませる。

Bing Image Creator で生成 : 風景写真、大きな入道雲と虹、ハイビスカスの花

今回は、都道府県別の自動車輸送統計調査のデータを分析してみます。

政府統計の総合窓口(e-stat)からデータを取得します。

このようなファイルです。

一般の乗合バス、高速の乗合バス、貸切バスの輸送人員と輸送人キロのデータです。

他にも一般の乗合バスと高速の乗合バスの合計や、一般の乗合バスの内訳で定期と、定期外という変数もありましたが、割愛しました。

このデータを R に読み込ませます。

まず、tidyverse パッケージを読み込んでおきます。

red_csv() 関数でCSVファイルを読み込みます。

うまく読み込まれていいるか、glimpse() 関数を使ってチェックしてみます。

うまく、読み込まれたようです。

変数を確認しておきましょう。

year: 調査年度

location: 北海道は、地域別、その他は都道府県別

general_num: 輸送人員【千人】営業用_バス(定員11人以上)_乗合_一般乗合_計

highway_num: 輸送人員【千人】営業用_バス(定員11人以上)_乗合_高速乗合

rental_num: 輸送人員【千人】営業用_バス(定員11人以上)_貸切

general_kilo: 輸送人キロ【千人キロ】営業用_バス(定員11人以上)_乗合_一般乗合_計

highway_kilo: 輸送人キロ【千人キロ】営業用_バス(定員11人以上)_乗合_高速乗合

rental_kilo: 輸送人キロ【千人キロ】営業用_バス(定員11人以上)_貸切

です。

summary() 関数で最大値などを確認しましょう。

highway_num, highway_kilo に NAが3つあります、いつでしょうか?

滋賀県がNAでした。

これらのNAは 0 にしましょう。

df_raw というオリジナルのデータフレームから、df というデータフレームをつくり、NA を 0 に置き換えました。highway_num, highway_kilo の NA がなくなって、最小値が 0 になっていることがわかります。

今回は以上です。

 

次回は、

www.crosshyou.info

です。

World Bank Population living in slums data analysis 6 - Hierarchical Clustering using R

Generated by Bing Image Creator: Unique landscape view traditional Japanese local area with rape blossoms, photo

www.crosshyou.info

This post is followong of the above post. In this post I will do hierarchical clustering using R.

First I make a matrix object for hierarchical clustering.

I will use two variables, sulums_2000 and change for clustetring.

Next, I make a function for scaling.

Then, I use maxmin() function to re-scale variables.

Let's see summary of mtx.

You'll see the both variables are re-scaled, max is 1, minimum is 0.

Now, let's do hierarchical clustering.

I use dist() function to calculate distance.

Then, I use hclust() function to make hierarchical clustering.

Let's plot dendrogram plot.

From the above plot, I think it is better divde three clusters like below plot.

I use cutree() function to assign those clusters to each country.

Finally, I make a scatter plot with the three clusters.

I see three clusters are very clearly divided.

That's it. Thank you!

 

To read from the first post,

 

www.crosshyou.info

World Bank Population living in slums data analysis 5 - Simulation based multiple regression analysis with R infer package

Generated by Bing Image Creator: Long view from far, small creek, a few trees,  one side road along with beautiful flowers, blue sky and white clouds, photo

 

www.crosshyou.info

This post is followng of the above post.

In this post, I will do simulation based multiple regression analysis with R infer package.

To begin with, I get formula based 95% confidence intervals.

I find sulums_2000 and Region South Asia confidence interval don't include 0.
So, those two variables are statistically significant.

When Regin and IncomeGroup are controled, sulums_2000 is statistically significant variable.

When sulums_2000 and IncomeGroup are controled, Region South Asia is statistically significant different from Region East Asia and Pacific.

Next, let's do simulation based multiple regression.

I start with getting obserbed fit.

This is the same as formula based multiple regression.

 

Next, I generate bootstrap distribution.

I get confidence interval.

sulums_2000 and Region South Asia don't include 0. The results is the same as formula based confidence interval.

Let's visualize confidence interval.

You:ll see confidence intervals of Region South Asia and sulums_2000 don't include 0.

I make histogram for sulums_2000 bootstrap distribution with confidence interval.

The red vertical line is at 0 and blue vertical lines are confidence interval. You:ll see confidence interval does not include 0.

In the previous post, I did simple ANOVA analysis to see change and Region relationship and I get there is not statistically significant relationship, but in this post, when I control other variables, change and IncomeGroup, I found Region and change have statistically significant relationship.
That's it! Thank you!

Next post is

www.crosshyou.info

 

To read from the first post,

www.crosshyou.info

World Bank Population living in slums data analysis 4 - Simulation based statistical inference with R infer package

Generated by Bing Image Creatpr: Wide large view of rice fields in the summer, blue sky and sunflower

www.crosshyou.info

This post is following of the above post. In this post I will do simulation based statistical inference with R infer package.

First, I load infer package.

I refere website of Tidy Statistical Inference • infer

First, I will check "whether change are related to Region or not."

I calculate obserbed F statistic.

Then, I generate null distribution using randomization simulation.

Let's visualize null distribution and observed F statistic.

It seems there is not statistically significant relationship between change and Region.

Let's calculate p-value.

p-value is 0.291, it is grater than 0.05. So, it is not statistically significant relationship between change and Region.

To confirm that, I also do formula based ANOVA with aov() function and summary() function.

With formula based, p-value is 0.293. So, the conclusion is not changed.

 

Next, let's check whether change and IncomeGroup has relationship or not.

I calculate obserbed F statistic.

Next, I generate null distribution.

I visualize null distribution and obserbed F statistic.

It seems there is statistically significant relationship.

Let's calculate p-value.

p-value is 0.006, which is smaller than 0.05. So, there is statistically relationship between change and IncomeGroup.

How about formula based ANOVA?

p-vale by formula based ANOVA is 0.00381, so it indicates significantly relationship.

 

Then, let's check change and sulums_2000.

First, I calculate obserbed slope.

Then, I calculate null distribution.

I visualize null distribution and observed slope.

It seems there is statistically relationship between change and sulums_2000.

Let's calculate p-value.

p-value is 0, so there is  surely statistically significant relationship.

Let's do formula based regression inference with lm() function and summary() function.

With formula based regression inference, p-value is 1.91e-08. So it is statistically significant. 

In this post I clonclude there is statistically relationship between change and IncomeGroup, change and sulums_2000, there is not statisticall significant relationship between change and Region.

That's it! Thank you!

Next post is

www.crosshyou.info

 

To reard from the first post,

www.crosshyou.info

 

 


 

読書記録 - 「教養としての建築入門 - 見方、作り方、活かし方」 坂牛 卓 著 (中公新書)

非常に読みやすい本でした。なんでだろうと考えると、章や節にこの章/節では~~のことを話します、と導入があって、終わりには、~~とはこういうことでした、それでは次の章/節では~~について話します、とあるからだとわかりました。

建築の三大原理は「用・強・美」ということだそうです。

この本は建築についてですが、哲学者のカントの話が出てきたり、ソビエトの建築はスターリンの政治思想と結びついていたりなど、建築を知るには建築物だけでなく、これを取り巻く社会情勢や思想なども理解していることが必要なのだとわかりました。

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!

Next post is

www.crosshyou.info

 

 

To read from the 1st pot,

www.crosshyou.info