library(KernSmooth)
library(car)
library(dplyr)
library(tibble)
library(KernSmooth)
library(ggplot2)
library(splines)
data("Prestige")
plot(Prestige$income,Prestige$prestige, col="orange")

fit <- locpoly(Prestige$income, Prestige$prestige,
degree=0, bandwidth=1500) %>% as.tibble
ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
geom_line(data=fit, aes(x=x,y=y), col='green')

smr <- loess(prestige ~ income, data=Prestige)
ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
ggtitle("Old Faithful (Loess, span=0,75)") +
geom_line(aes(x=income, y=fitted(smr)),
col='purple')

smr <- smooth.spline(
Prestige$income,
Prestige$prestige,
cv=TRUE)
smr <- data.frame(x=smr$x,y=smr$y)
ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
ggtitle("Prestige by income") +
geom_line(data=smr, aes(x=x, y=y), col='blue')

fit <- lm(prestige ~ ns(income, df=6), Prestige)
ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
ggtitle("Prestige by income") +
geom_line(aes(x=income, y=fitted(fit)), col='red')

#im więcej kolejnych lokalnych regresji tym bardziej dopasowany wykres do danych