library(car)
## Warning: pakiet 'car' został zbudowany w wersji R 4.3.3
## Ładowanie wymaganego pakietu: carData
## Warning: pakiet 'carData' został zbudowany w wersji R 4.3.2
library(ggplot2)
## Warning: pakiet 'ggplot2' został zbudowany w wersji R 4.3.2
library(splines)
library(MASS)
Raport 3- Maciej Fleks
Loess oraz geom_smooth
gopro=ggplot(Prestige, aes(x=income, y=prestige))+geom_point()+geom_smooth(span=0.75, method="loess")+ggtitle("Loess, span=0.75")
gopro
## `geom_smooth()` using formula = 'y ~ x'
gopro2=ggplot(Prestige, aes(x=income, y=prestige))+geom_point()+geom_smooth(span=0.25, method="loess")+ggtitle("Loess, span=0.25")
gopro2
## `geom_smooth()` using formula = 'y ~ x'
gopro3=ggplot(Prestige, aes(x=income, y=prestige))+geom_point()+geom_smooth(span=0.05, method="loess")+ggtitle("Loess, span=0.05")
gopro3
## `geom_smooth()` using formula = 'y ~ x'
Za zastosowaniem wartości span wynoszącej 0.75 przemawia mniejszy zakres niepewności oraz płynniejszy kształt linii, a za wartością 0.25 lepsze dopasowanie do części wartości.
Sploty interpolujące
smr<-smooth.spline(
Prestige$income,
Prestige$prestige,
cv=TRUE
)
## Warning in smooth.spline(Prestige$income, Prestige$prestige, cv = TRUE):
## krzyżowa walidacja z nieunikalnymi wartościami 'x' wydaje się wątpliwa
smr<-data.frame(x=smr$x, y=smr$y)
interpol=ggplot(Prestige)+geom_point(aes(x=income, y=prestige))+geom_line(data=smr, aes(x=x, y=y), col='blue')+ggtitle("Sploty interpolujące")
interpol
sploty regresyjne, 6 df
fit <- lm(prestige ~ ns(income, df=6), Prestige)
reggie1=ggplot(Prestige) + geom_point(aes(x=income,y=prestige)) + ggtitle("Prestige (Naturalne sploty, 6 df)") + geom_line(aes(x=income, y=fitted(fit)), col='blue')
reggie1
sploty regresyjne, 12 df
fit2 <- lm(prestige ~ ns(income, df=12), Prestige)
reggie2=ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
ggtitle("Prestige (Naturalne sploty, 12 df)") +
geom_line(aes(x=income, y=fitted(fit2)), col='blue')
reggie2
sploty regresyjne, 3 df
fit3 <- lm(prestige ~ ns(income, df=3), Prestige)
reggie3=ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
ggtitle("Prestige (Naturalne sploty, 3 df)") +
geom_line(aes(x=income, y=fitted(fit3)), col='blue')
reggie3
sploty regresyjne, 24 df
fit4 <- lm(prestige ~ ns(income, df=24), Prestige)
reggie4=ggplot(Prestige) +
geom_point(aes(x=income,y=prestige)) +
ggtitle("Prestige (Naturalne sploty, 24 df)") +
geom_line(aes(x=income, y=fitted(fit4)), col='blue')
reggie4
Kształt linii trendu przy wartościach parametru df wynoszących 6 oraz 12 wydaje się bardzo podobny, osobiście zastosowałbym 6 df aby zminimalizować liczbę odchyleń.
MASS
data("mcycle")
mcycle
## times accel
## 1 2.4 0.0
## 2 2.6 -1.3
## 3 3.2 -2.7
## 4 3.6 0.0
## 5 4.0 -2.7
## 6 6.2 -2.7
## 7 6.6 -2.7
## 8 6.8 -1.3
## 9 7.8 -2.7
## 10 8.2 -2.7
## 11 8.8 -1.3
## 12 8.8 -2.7
## 13 9.6 -2.7
## 14 10.0 -2.7
## 15 10.2 -5.4
## 16 10.6 -2.7
## 17 11.0 -5.4
## 18 11.4 0.0
## 19 13.2 -2.7
## 20 13.6 -2.7
## 21 13.8 0.0
## 22 14.6 -13.3
## 23 14.6 -5.4
## 24 14.6 -5.4
## 25 14.6 -9.3
## 26 14.6 -16.0
## 27 14.6 -22.8
## 28 14.8 -2.7
## 29 15.4 -22.8
## 30 15.4 -32.1
## 31 15.4 -53.5
## 32 15.4 -54.9
## 33 15.6 -40.2
## 34 15.6 -21.5
## 35 15.8 -21.5
## 36 15.8 -50.8
## 37 16.0 -42.9
## 38 16.0 -26.8
## 39 16.2 -21.5
## 40 16.2 -50.8
## 41 16.2 -61.7
## 42 16.4 -5.4
## 43 16.4 -80.4
## 44 16.6 -59.0
## 45 16.8 -71.0
## 46 16.8 -91.1
## 47 16.8 -77.7
## 48 17.6 -37.5
## 49 17.6 -85.6
## 50 17.6 -123.1
## 51 17.6 -101.9
## 52 17.8 -99.1
## 53 17.8 -104.4
## 54 18.6 -112.5
## 55 18.6 -50.8
## 56 19.2 -123.1
## 57 19.4 -85.6
## 58 19.4 -72.3
## 59 19.6 -127.2
## 60 20.2 -123.1
## 61 20.4 -117.9
## 62 21.2 -134.0
## 63 21.4 -101.9
## 64 21.8 -108.4
## 65 22.0 -123.1
## 66 23.2 -123.1
## 67 23.4 -128.5
## 68 24.0 -112.5
## 69 24.2 -95.1
## 70 24.2 -81.8
## 71 24.6 -53.5
## 72 25.0 -64.4
## 73 25.0 -57.6
## 74 25.4 -72.3
## 75 25.4 -44.3
## 76 25.6 -26.8
## 77 26.0 -5.4
## 78 26.2 -107.1
## 79 26.2 -21.5
## 80 26.4 -65.6
## 81 27.0 -16.0
## 82 27.2 -45.6
## 83 27.2 -24.2
## 84 27.2 9.5
## 85 27.6 4.0
## 86 28.2 12.0
## 87 28.4 -21.5
## 88 28.4 37.5
## 89 28.6 46.9
## 90 29.4 -17.4
## 91 30.2 36.2
## 92 31.0 75.0
## 93 31.2 8.1
## 94 32.0 54.9
## 95 32.0 48.2
## 96 32.8 46.9
## 97 33.4 16.0
## 98 33.8 45.6
## 99 34.4 1.3
## 100 34.8 75.0
## 101 35.2 -16.0
## 102 35.2 -54.9
## 103 35.4 69.6
## 104 35.6 34.8
## 105 35.6 32.1
## 106 36.2 -37.5
## 107 36.2 22.8
## 108 38.0 46.9
## 109 38.0 10.7
## 110 39.2 5.4
## 111 39.4 -1.3
## 112 40.0 -21.5
## 113 40.4 -13.3
## 114 41.6 30.8
## 115 41.6 -10.7
## 116 42.4 29.4
## 117 42.8 0.0
## 118 42.8 -10.7
## 119 43.0 14.7
## 120 44.0 -1.3
## 121 44.4 0.0
## 122 45.0 10.7
## 123 46.6 10.7
## 124 47.8 -26.8
## 125 47.8 -14.7
## 126 48.8 -13.3
## 127 50.6 0.0
## 128 52.0 10.7
## 129 53.2 -14.7
## 130 55.0 -2.7
## 131 55.0 10.7
## 132 55.4 -2.7
## 133 57.6 10.7
Loess oraz geom_smooth
cy1=ggplot(mcycle, aes(x=times, y=accel))+geom_point()+geom_smooth(span=0.75, method="loess")+ggtitle("Loess, span=0.75")
cy1
## `geom_smooth()` using formula = 'y ~ x'
cy2=ggplot(mcycle, aes(x=times, y=accel))+geom_point()+geom_smooth(span=0.25, method="loess")+ggtitle("Loess, span=0.25")
cy2
## `geom_smooth()` using formula = 'y ~ x'
cy3=ggplot(mcycle, aes(x=times, y=accel))+geom_point()+geom_smooth(span=0.1, method="loess")+ggtitle("Loess, span=0.1")
cy3
## `geom_smooth()` using formula = 'y ~ x'
Przy zastosowaniu metody Loess zastsowanie wartości span wynoszącej 0.75 linia trendu położona jest w znacznej odległości od skrajnych wartości. Osobiście zdecydowałbym się na zastosowanie wartości span wynoszącej 0.25 z uwagi na dużo bardziej regularny kształt linii trendu.
Sploty interpolujące
smr2<-smooth.spline(
mcycle$times,
mcycle$accel,
cv=TRUE
)
## Warning in smooth.spline(mcycle$times, mcycle$accel, cv = TRUE): krzyżowa
## walidacja z nieunikalnymi wartościami 'x' wydaje się wątpliwa
smr2<-data.frame(x=smr$x, y=smr$y)
interpol2=ggplot(mcycle)+geom_point(aes(x=times, y=accel))+geom_line(data=smr2, aes(x=x, y=y), col='blue')+ggtitle("Sploty interpolujące dla df mcycle")
interpol2
sploty regresyjne, 3 df
fit2 <- lm(accel ~ ns(times, df=3), mcycle)
rcy3=ggplot(mcycle) +
geom_point(aes(x=times,y=accel)) +
ggtitle("Mcycle (Naturalne sploty, 3 df)") +
geom_line(aes(x=times, y=fitted(fit2)), col='blue')
rcy3
sploty regresyjne, 6 df
fit2 <- lm(accel ~ ns(times, df=6), mcycle)
rcy6=ggplot(mcycle) +
geom_point(aes(x=times,y=accel)) +
ggtitle("Mcycle (Naturalne sploty, 6 df)") +
geom_line(aes(x=times, y=fitted(fit2)), col='blue')
rcy6
sploty regresyjne, 12 df
fit2 <- lm(accel ~ ns(times, df=12), mcycle)
rcy12=ggplot(mcycle) +
geom_point(aes(x=times,y=accel)) +
ggtitle("Mcycle (Naturalne sploty, 12 df)") +
geom_line(aes(x=times, y=fitted(fit2)), col='blue')
rcy12
sploty regresyjne, 24 df
fit2 <- lm(accel ~ ns(times, df=24), mcycle)
rcy24=ggplot(mcycle) +
geom_point(aes(x=times,y=accel)) +
ggtitle("Mcycle (Naturalne sploty, 24 df)") +
geom_line(aes(x=times, y=fitted(fit2)), col='blue')
rcy24
Stopień dopasowania do danych w przypadku wartości df wynoszącej 6 oraz 12 wydaje się dość podobny, choć w przypadku 12 df występuje łagodniejszy kształt krzywej dla najwyższych wartości times oraz zachodzi większe zbliżenie linii trendu do skrajnych wartości.