-
Notifications
You must be signed in to change notification settings - Fork 0
/
coefs.sql
76 lines (68 loc) · 826 Bytes
/
coefs.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
.mode csv
.import mtcars.csv mtcars
.output coefs.csv
.headers ON
with x_tbl as (
select
rowid,
wt as x
from
mtcars
),
avg_x_tbl as (
select
avg(x) as avg_x
from
x_tbl
),
y_tbl as (
select
rowid,
mpg as y
from
mtcars
),
avg_y_tbl as (
select
avg(mpg) as avg_y
from
mtcars
),
full_data as (
select
x,
avg_x,
y,
avg_y
from
y_tbl, x_tbl,
avg_y_tbl, avg_x_tbl
where
y_tbl.rowid = x_tbl.rowid
),
beta_tbl as (
select
sum(
(x - avg_x) * (y - avg_y)
) /
sum(
(x - avg_x) * (x - avg_x)
) as beta
from
full_data
),
alpha_tbl as (
select
avg_y - beta * avg_x as alpha
from
beta_tbl,
avg_y_tbl,
avg_x_tbl
)
select
alpha,
beta
from
alpha_tbl,
beta_tbl
;