sample_data <-
data.frame
(x=1:10,
y=
c
(25, 22, 13, 10, 5,
9, 12, 16, 34, 44))
linear_model1 <-
lm
(y~x, data=sample_data)
linear_model2 <-
lm
(y~
poly
(x,2,raw=
TRUE
), data=sample_data)
linear_model3 <-
lm
(y~
poly
(x,3,raw=
TRUE
), data=sample_data)
linear_model4 <-
lm
(y~
poly
(x,4,raw=
TRUE
), data=sample_data)
linear_model5 <-
lm
(y~
poly
(x,5,raw=
TRUE
), data=sample_data)
plot
(sample_data$x, sample_data$y)
x_axis <-
seq
(1, 10, length=10)
lines
(x_axis,
predict
(linear_model1,
data.frame
(x=x_axis)), col=
'green'
)
lines
(x_axis,
predict
(linear_model2,
data.frame
(x=x_axis)), col=
'red'
)
lines
(x_axis,
predict
(linear_model3,
data.frame
(x=x_axis)), col=
'purple'
)
lines
(x_axis,
predict
(linear_model4,
data.frame
(x=x_axis)), col=
'blue'
)
lines
(x_axis,
predict
(linear_model5,
data.frame
(x=x_axis)), col=
'orange'
)