0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

線形代数について語ろう

Posted at

Rで線形代数を体験する

実装環境
Anaconda jupyter lab with R kernel
多分Rstudio だけでも行けるはず

やってることフローチャート
1.functionをつくってデータ作成する
2. Parameterを1.のFunctionに設定してデータセットを用意
3. みんなの大好きggplotに当てはめてLinear regression を作る。

  *インストールできてないライブラリーは随時インストールする。


library(ggplot2)
# generate a list of points between (0,5)
x = seq(0,5,length.out=1000)


MyFun <- function(x,a,b,c) {
     res <- a + b*x + c*(x**2)
     return(res)
    }
# set values for a,b,c
set1 <- list(a=0, b=1, c=0.1)
set2 <- list(a=0, b=2, c=-0.1)
set3 <- list(a=-5, b=0.5, c=1)
set4 <- list(a=20, b=0, c=-1)

options(repr.plot.width = 7, repr.plot.height = 3)
plt <- ggplot(data.frame(x), aes(x=x)) + 
  stat_function(fun = MyFun, n = 1000, args = set1,aes(colour = paste(names(set1),set1,sep="=",collapse=", " ))) +
  stat_function(fun = MyFun, n = 1000, args = set2,aes(colour = paste(names(set2),set2,sep="=",collapse=", " ))) +
  stat_function(fun = MyFun, n = 1000, args = set3,aes(colour = paste(names(set3),set3,sep="=",collapse=", " ))) +
  stat_function(fun = MyFun, n = 1000, args = set4,aes(colour = paste(names(set4),set4,sep="=",collapse=", " )))+
  scale_colour_manual("Parameter Values",values = c("red", "blue", "green", "orange"))


print(plt)

いろいろRで線形モデルを作ってみる

うちの学校で出たやつ。
 手順
1.きれいなデータセットと予めその理想的なLinear modelを作って起き、
2.ノイズを加えて現実的なデータプロットにして、
3.そこからノイズ入りデータからlinear regression を作ってみる

1.きれいなデータセットであらかじめ理想的なLinear modelを作って起き、

xmin=0
xmax=10
set.seed(54321)
##############################
#      Customize the demo here
#      =======================
# the "true" function of x with range fixed at [0,10]
trueFun <- function(x) {
     res <- sin(x*2.0)*sqrt(x)/3.3
     return(res)
    }
#  when fitting goes wild, need to constrain what y's are plotted
#  must be compatible with "true" function
ydisplaymin = -1.8
ydisplaymax = 1.8

#  don't make points more than 100 as demo is O(points^3)
points = 30

#  set noise level (std.dev)
mySigma = 0.2
##############################


#  generate a x vector randomly
x = sort(runif(points,xmin,xmax))
#  build the true values matching the sampled data
yt = trueFun(x)

# xts and yts store the "true" function for the purposes of plotting
# these have to be high frequency to make the resultant plot look
# like a smooth curve
xts = seq(0,10,length.out=200)
yts = trueFun(xts)

options(repr.plot.width = 7, repr.plot.height = 4)
ggplot(data.frame(x,yt),aes(x=x,y=yt)) + 
    geom_point() +  stat_function(fun = trueFun, aes( colour = 'Truth')) +  
    labs(x = 'x',y='y') + ylim(ydisplaymin,ydisplaymax) +
    scale_colour_manual("",values = 'blue')

単純なsin関数に徐々に変動が大きくなるようにsquere(x)の影響下にしただけのしんぷるな関数。

2.ノイズ(ガウスノイズ)を加えて現実的なデータプロットにして、

#add Gaussian noise
y <- yt + rnorm(length(yt), sd = mySigma)
yNoise <- y-yt

options(repr.plot.width = 7, repr.plot.height = 4)
base <-ggplot(data.frame(x,yNoise),aes(x=x,y=yNoise)) + 
    geom_point() + labs(x = 'x',y='y', title='Noise')
print(base)


ggplot(data.frame(x,y),aes(x=x,y=y)) + geom_point() +
    stat_function(fun = trueFun, aes( colour = 'Truth')) +
    labs(x = 'x',y='y', title='Data and Truth Function') + 
    scale_colour_manual("",values = 'blue')


3.そこからノイズ入りデータからlinear regression を作ってみる

##############################
#      Customize the demo here
#      =======================
#     change the order to see different order polynomials being fit
order = 4
##############################

options(repr.plot.width = 7, repr.plot.height = 4)
# plot data and the truth
base <- ggplot(data.frame(x,y),aes(x=x,y=y)) + geom_point() +
     aes(colour = 'Truth') + 
    geom_smooth(method = "lm", formula = y ~ poly(x,order), 
                se=F, aes(colour='Fit'),size = 0.5) +
    labs(x = 'x',y='y', title='Data and Function')


print(base)

線形代数でよく使われる二重誤差(lm)を用いて回帰線と実際のデータ(ノイズ入りのデータ)との違いの総量を最小化をする。

まあ、現時点で分けワカメな人も単純にRの回帰曲線サンプルコードとして、実際に自分の環境でCopy and pasteして試してくださいまし。
 

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?