x <- 1:30
n <- length(x)

b0 <- 20
b1 <- 1.2

set.seed(2)
e <- rnorm(n, mean = 0, sd = 5)

y <- b0 + b1 * x + e

ybar <- mean(y)

# Test data
#x <- c(1, 2, 3, 4, 5)
#y <- c(2, 2, 4, 3, 5)

d <- data.frame(x, y)
d
##     x        y
## 1   1 16.71543
## 2   2 23.32425
## 3   3 31.53923
## 4   4 19.14812
## 5   5 25.59874
## 6   6 27.86210
## 7   7 31.93977
## 8   8 28.40151
## 9   9 40.72237
## 10 10 31.30606
## 11 11 35.28825
## 12 12 39.30876
## 13 13 33.63652
## 14 14 31.60166
## 15 15 46.91114
## 16 16 27.64465
## 17 17 44.79302
## 18 18 41.77903
## 19 19 47.86414
## 20 20 46.16133
## 21 21 55.65410
## 22 22 40.40037
## 23 23 55.54819
## 24 24 58.57326
## 25 25 50.02469
## 26 26 38.94147
## 27 27 54.78619
## 28 28 50.61721
## 29 29 58.76102
## 30 30 57.44818
# カラーパレット
# [RGB_Color] https://www.rapidtables.com/web/color/RGB_Color.html
COL <- c(rgb(255,   0,   0,  255, max = 255), # 赤
         rgb(  0,   0, 255,  255, max = 255), # 青
         rgb(  0, 155,   0,  255, max = 255)) # 緑

matplot(x, y, pch = 1, col = COL[1])
grid()

fit <- lm(y ~ x, data = d)
summary(fit)
## 
## Call:
## lm(formula = y ~ x, data = d)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5989  -2.8452   0.0335   3.6787   9.2076 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  20.8526     2.2356   9.327 4.38e-10 ***
## x             1.2188     0.1259   9.678 1.97e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.97 on 28 degrees of freedom
## Multiple R-squared:  0.7699, Adjusted R-squared:  0.7616 
## F-statistic: 93.66 on 1 and 28 DF,  p-value: 1.972e-10
matplot(x, y, pch = 1, col = COL[1], main = '主タイトル')
grid()
matlines(x, fit$fitted, col = COL[2])

library(latex2exp)
legend('topleft', lty = c(NA, 1), pch = c(1, NA), col = COL, 
       legend = c('Data', TeX('$\\hat{y}_i = b_0 + b_1 x_i $')))

library(plotly)
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:latex2exp' からマスクされています:
## 
##     TeX
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout

plot_ly() |>
  add_trace(x = x, y = y,          mode = 'markers', name = 'Data') |>
  add_trace(x = x, y = fit$fitted, mode = 'lines',   name = '$\\hat{y}_i = b_0 + b_1 x_i $') |>
  layout(font  = list(size = 11, color = 'blue', family = 'UD Digi Kyokasho NK-R'),
         title = '主タイトル',
         xaxis = list(title = 'x軸ラベル[単位]'),
         yaxis = list(title = 'y軸ラベル[単位]')) |>
  config(mathjax = 'cdn')
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
#データ
import numpy as np
import pandas as pd

b0 = 20
b1 = 1.2 

np.random.seed(3)
e = np.random.normal(loc = 0, scale = 5, size = 30).reshape(-1,1)

x = np.arange(1, 31, 1).reshape(-1,1)
y = b0 + b1*x + e

#回帰分析
from sklearn.linear_model import LinearRegression

model_lr = LinearRegression()
model_lr.fit(x, y)
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
import matplotlib.pyplot as plt

# ラベル
plt.title('主タイトル')
plt.xlabel('x軸ラベル[単位]')
plt.ylabel('y軸ラベル[単位]')

# 格子線(grid lines)
plt.grid(linestyle = '--', color = (0.9, 0.9, 0.9, 0.25))
plt.plot(x, y, 'o', label = 'Data points')
plt.plot(x, model_lr.predict(x), linestyle = 'solid', 
         label = '$\hat{y}_i = b_0 + b_1 x_i$')

# 凡例(はんれい)
plt.legend(loc = 'lower right')

plt.show()
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 36600 (\N{CJK UNIFIED IDEOGRAPH-8EF8}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12521 (\N{KATAKANA LETTER RA}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12505 (\N{KATAKANA LETTER BE}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12523 (\N{KATAKANA LETTER RU}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 65339 (\N{FULLWIDTH LEFT SQUARE BRACKET}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 21336 (\N{CJK UNIFIED IDEOGRAPH-5358}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 20301 (\N{CJK UNIFIED IDEOGRAPH-4F4D}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 65341 (\N{FULLWIDTH RIGHT SQUARE BRACKET}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 20027 (\N{CJK UNIFIED IDEOGRAPH-4E3B}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12479 (\N{KATAKANA LETTER TA}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12452 (\N{KATAKANA LETTER I}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12488 (\N{KATAKANA LETTER TO}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)