正規分布

x=(-10:10)
y=dnorm(x)
plot(x,y)

中心極限定理

x=runif(1000000)
hist(x, freq=FALSE)

x=x+runif(1000000)+runif(1000000)+runif(1000000)
hist(x,freq = FALSE)

平均が18、標準偏差10の正規分布

curve(dnorm(x,18,10),0,40)

X=c(28,13,16,28,29,12,14,12,10)
hist(X)

mean<-mean(X)
low<-mean-1.96*(10/3)
upper<-mean+1.96*(10/3)
mean
## [1] 18
low
## [1] 11.46667
upper
## [1] 24.53333

COVID19の感染者数と死者数の関係を表す散布図

library(readxl)
covid <- read_excel("covid.xlsx")
plot(covid$total_cases,covid$total_deaths)

COVID19の感染者数と死者数の関係を表す散布図&回帰直線

library(readxl)
covid <- read_excel("covid.xlsx")
result<-lm(total_deaths~total_cases,data = covid)
summary(result)
## 
## Call:
## lm(formula = total_deaths ~ total_cases, data = covid)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -179516  -45731     150   41216  328532 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -4.670e+04  5.107e+04  -0.914    0.379    
## total_cases  1.202e-02  1.623e-03   7.406  8.2e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 133600 on 12 degrees of freedom
## Multiple R-squared:  0.8205, Adjusted R-squared:  0.8056 
## F-statistic: 54.86 on 1 and 12 DF,  p-value: 8.203e-06
plot(covid$total_cases,covid$total_deaths)
abline(result)

ggplot GUI ローカル

ggplot GUI オンライン

ggplot

ggplot

ggplot

データ名称をdfに変更しておく。

library(readxl)
covid <- read_excel("covid.xlsx")
df<-covid

ggplot GUIで出力されるコードをコピー&ペーストして、グラフを再現。

  • library(“ggplot2”)が必要。
  • library(“plotly”)も必要。
## You can use the below code to generate the graph.
## Don't forget to replace the 'df' with the name
## of your dataframe

# You need the following package(s):
library("ggplot2")

# The code below will generate the graph:
graph <- ggplot(df, aes(x = total_cases, y = total_deaths, colour = iso_code)) +
  geom_point() +
  theme_bw()
graph

# If you want the plot to be interactive,
# you need the following package(s):
library("plotly")
ggplotly(graph)
# If you would like to save your graph, you can use:
ggsave('my_graph.pdf', graph, width = 14, height = 14, units = 'cm')

致死率(=死者数/感染者数)と一人あたりGDPの関係を調べる

library(readxl)
covid <- read_excel("covid.xlsx")
covid$cfr<-covid$total_deaths/covid$total_cases
# las = 2によりx軸のラベルを90度回転。
# cex.names = 0.80によりx軸のラベルを小さく。
barplot(covid$cfr,names.arg = covid$iso_code,cex.names = 0.80,las=2)

result<-lm(cfr~gdp_per_capita,data=covid)
summary(result)
## 
## Call:
## lm(formula = cfr ~ gdp_per_capita, data = covid)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.007992 -0.004165 -0.001411  0.003943  0.010690 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     1.823e-02  4.014e-03   4.542 0.000675 ***
## gdp_per_capita -2.848e-07  1.095e-07  -2.602 0.023148 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.006014 on 12 degrees of freedom
## Multiple R-squared:  0.3607, Adjusted R-squared:  0.3074 
## F-statistic: 6.769 on 1 and 12 DF,  p-value: 0.02315
plot(covid$gdp_per_capita,covid$cfr)
abline(result)