lab1-3
|
@ -0,0 +1,140 @@
|
|||
# Podsumowanie
|
||||
Podsumowanie zajęć
|
||||
|
||||
---
|
||||
## LAB 1
|
||||
Zakres:
|
||||
- wstęp do języka R
|
||||
|
||||
- wykład 1 na stronie
|
||||
|
||||
|
||||
|
||||
### R
|
||||
Lista:
|
||||
```r
|
||||
# wektory
|
||||
rep(TRUE, 3)
|
||||
seq(1, 20, by=1)
|
||||
order(zad6, decreasing = TRUE)]
|
||||
|
||||
# pętle
|
||||
for(i in 1:length(zad5)){}
|
||||
while (licznik <= length(x)){}
|
||||
repeat {
|
||||
if (licznik > length(x)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# funkcja, pakiety
|
||||
minmax <- function(x){}
|
||||
install.packages("schoolmath")
|
||||
library(schoolmath)
|
||||
```
|
||||
|
||||
### Zagadnienia
|
||||
![operatory](lab1/operatory.png)
|
||||
|
||||
![logiczne](lab1/logiczne.png)
|
||||
|
||||
|
||||
---
|
||||
## LAB 2
|
||||
Zagadnienia:
|
||||
- ciąg dalszy wprowadzenie do R
|
||||
|
||||
- wykład 1 na stronie
|
||||
|
||||
|
||||
|
||||
### R
|
||||
Lista:
|
||||
```r
|
||||
# ładowanie danych
|
||||
dane <- read.table("dane1.csv", header = TRUE, sep = ";")
|
||||
ankieta <- read.table("http://ls.home.amu.edu.pl/data_sets/ankieta.txt", header = TRUE)
|
||||
computers <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/computers.csv")
|
||||
```
|
||||
|
||||
|
||||
### Zagadnienia
|
||||
Lista:
|
||||
- **Wektor** musi zawierać takie same typy, **lista** może różne.
|
||||
|
||||
- **Macierze**, ogólniej to są **tablice** reprezentowane przez wektor atomowy
|
||||
|
||||
- **Czynniki**: dla ("f", "p", "f") zwraca "f", "p"
|
||||
|
||||
- **Ramki danych** to jak w excelu arkusze
|
||||
|
||||
|
||||
---
|
||||
## LAB 3
|
||||
Zagadnienia:
|
||||
- **Statystka opisowa** - zaprezentowanie cechy X na próbce za pomocą tabeli, wykresu
|
||||
|
||||
- Wykład 2 na stronie
|
||||
|
||||
|
||||
### R
|
||||
```r
|
||||
# rozkład empiryczny
|
||||
ankieta <- read.table("http://ls.home.amu.edu.pl/data_sets/ankieta.txt", header = TRUE)
|
||||
empiryczny <- data.frame(cbind(liczebnosc = table(ankieta$wynik),
|
||||
procent = prop.table(table(ankieta$wynik))))
|
||||
|
||||
# wykres ramkowy
|
||||
barplot(table(ankieta$wynik),
|
||||
xlab = "Odpowiedzi", ylab = "Odpowiedzi",
|
||||
main = "Rozkład empiryczny zmiennej wynik")
|
||||
|
||||
# inne
|
||||
install.packages("e1071")
|
||||
library(e1071)
|
||||
skewness(x)
|
||||
kurtosis(x)
|
||||
```
|
||||
|
||||
|
||||
### Zagadnienia
|
||||
Lista:
|
||||
- **Miara asymetrii rozkładu** - w którą stronę - prawo/lewo, zmienna się rozkłada.
|
||||
- zero to symetryczny
|
||||
- dodatnie to prawostronnie asymetryczny - lewa część jest większa
|
||||
- ujemna to lewostronnie asymetryczna - prawa część jest większa
|
||||
![asymetria](lab3/asymetria.png)
|
||||
|
||||
- **Kurtoza** - miara skupienia wartości wokół średniej. Porównuje rozkład empiryczny z rozkładem normalnym.
|
||||
- Większa niż 0, im większa wartość tym bardziej wartości skupione wokół średniej
|
||||
- Dla rozkładu normalnego = 0
|
||||
- Dla ujemnych (min -2) wykres jest bardziej spłaszczony niż rozkłąd normalny
|
||||
![kurtoza](lab3/kurtoza.png)
|
||||
|
||||
- **Rozkład empiryczny** - za pomocą szeregu rozdzielczego to np podanie liczebności i udziału procentowego danej zmiennej.
|
||||
|
||||
- **Odchylenie standardowe** - intuicyjnie rzecz ujmując, odchylenie standardowe mówi, jak szeroko wartości jakiejś wielkości (na przykład wieku, inflacji, kursu walutowego) są rozrzucone wokół jej średniej.
|
||||
Im mniejsza wartość odchylenia tym obserwacje są bardziej skupione wokół średniej.
|
||||
|
||||
- **Współczynnik zmienności** - podaje się w procentach, jest to relacja odchylenia standardowego ze średnią. Mówi nam jak bardzo wartości odbiegają od siebie. Dzięki temu ze jest w procentach mozemy porównywać rózne rozkłady.
|
||||
- [Przykład](https://pl.wikipedia.org/wiki/Współczynnik_zmienności)
|
||||
|
||||
- **Funkcja gęstości** - nieujemna funkcja rzeczywista, określona dla rozkładu prawdopodobieństwa, taka że całka z tej funkcji, obliczona w odpowiednich granicach, jest równa prawdopodobieństwu wystąpienia danego zdarzenia losowego.
|
||||
|
||||
- **Histogram** – składa się z szeregu prostokątów umieszczonych na osi współrzędnych. Prostokąty te są z jednej strony wyznaczone przez przedziały klasowe wartości cechy, natomiast ich wysokość jest określona przez liczebności (lub częstości, ewentualnie gęstość prawdopodobieństwa) elementów wpadających do określonego przedziału klasowego.
|
||||
|
||||
- **Kwantyl** rzędu p to taka zmienna dla której prawdopodobieństwo wystąpienia od 0 do tej zmiennej jest równe p.
|
||||
Kwantyl rzędu 1/2 to inaczej mediana. Kwantyle rzędu 1/4, 2/4, 3/4 są inaczej nazywane kwartylami.
|
||||
- pierwszy kwartyl (notacja: Q1) = dolny kwartyl = kwantyl rzędu 1/4 = 25% obserwacji jest położonych poniżej
|
||||
- drugi kwartyl (notacja: Q2) = mediana = kwantyl rzędu 1/2 = dzieli zbiór obserwacji na połowę
|
||||
- trzeci kwartyl (notacja: Q3) = górny kwartyl = kwantyl rzędu 3/4 = dzieli zbiór obserwacji na dwie części odpowiednio po 75% położonych poniżej tego kwartyla i 25% położonych powyżej
|
||||
![kwanty](lab3/kwantyl.png)
|
||||
|
||||
- **Wykres ramkowy**
|
||||
![ramkowy1](lab3/ramkowy1.png)
|
||||
![ramkowy2](lab3/ramkowy2.png)
|
||||
|
||||
- **Rozkład empiryczny** – uzyskany na podstawie badania statystycznego opis wartości przyjmowanych przez cechę statystyczną w próbie przy pomocy częstości ich występowania.
|
||||
|
||||
- Statystki opisowe:
|
||||
![dodatek](lab3/dodatek.png)
|
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 84 KiB |
After Width: | Height: | Size: 52 KiB |
After Width: | Height: | Size: 82 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 60 KiB |
After Width: | Height: | Size: 112 KiB |
|
@ -0,0 +1,138 @@
|
|||
computers <- read.table("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/computers.csv")
|
||||
View(computers)
|
||||
computers <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/computers.csv")
|
||||
View(computers)
|
||||
spotify <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/spotify.csv")
|
||||
View(spotify)
|
||||
weight_height <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/weight-height.csv")
|
||||
View(weight_height)
|
||||
model_1 <- lm(valence ~ acusticness + danceability + energy + instrumentalness +
|
||||
liveness, data = spotify)
|
||||
model_1 <- lm(valence ~ acousticness + danceability + energy + instrumentalness +
|
||||
liveness, data = spotify)
|
||||
model_1 <- lm(valence ~ acousticness + danceability + energy + instrumentalness +
|
||||
liveness + loudness + speechiness + tempo + song_title, data = spotify)
|
||||
model_1
|
||||
summary(model_1)
|
||||
model_1 <- lm(valence ~ acousticness + danceability + energy + instrumentalness +
|
||||
liveness + loudness + speechiness + tempo, data = spotify)
|
||||
summary(model_1)
|
||||
step(model_1)
|
||||
step(model_1)
|
||||
step(model_1)
|
||||
summary(model_1)
|
||||
model_2 <- lm(valence ~ acousticness + danceability + energy + instrumentalness +
|
||||
liveness + loudness + speechiness, data = spotify)
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.305, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789, speechiness=0.1470)
|
||||
View(new_data)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
summary(model_2)$adj.r.squared
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.305, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=1.305, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.305, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.405, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.305, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.405, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
0.3918359 - 0.3918359
|
||||
0.3918359 - 0.3229826
|
||||
male <- ifelse(weight_height$Gender == "Male")
|
||||
male <- weight_height$Gender == "Male"
|
||||
male <- ifelse(weight_height$Gender == "Male", weight_height$Height)
|
||||
male <- ifelse(weight_height$Gender == "Male", weight_height$Height, 0)
|
||||
male <- weight_height[weight_height$Gender == "Male"]
|
||||
male <- weight_height[weight_height$Gender == "Male", ]
|
||||
View(male)
|
||||
shapiro.test(male$Height)
|
||||
qqnorm(male$Height)
|
||||
shapiro.test(male$Weight)
|
||||
qqnorm(male$Weight)
|
||||
shapiro.test(male$Height)
|
||||
qqnorm(male$Height)
|
||||
mean(male$Height)
|
||||
par(mfrow = c(1, 2))
|
||||
female <- weight_height[weight_height$Gender == "Female", ]
|
||||
shapiro.test(female$Height)
|
||||
qqnorm(female$Height)
|
||||
mean(female$Height)
|
||||
par(mfrow = c(1, 2))
|
||||
male <- weight_height[weight_height$Gender == "Male", ]
|
||||
shapiro.test(male$Height)
|
||||
qqnorm(male$Height)
|
||||
mean(male$Height)
|
||||
female <- weight_height[weight_height$Gender == "Female", ]
|
||||
shapiro.test(female$Height)
|
||||
qqnorm(female$Height)
|
||||
mean(female$Height)
|
||||
par(mfrow = c(1, 2))
|
||||
male <- weight_height[weight_height$Gender == "Male", ]
|
||||
shapiro.test(male$Height)
|
||||
qqnorm(male$Height)
|
||||
mean(male$Height)
|
||||
var(male$Height)
|
||||
female <- weight_height[weight_height$Gender == "Female", ]
|
||||
shapiro.test(female$Height)
|
||||
qqnorm(female$Height)
|
||||
mean(female$Height)
|
||||
var(female$Height)
|
||||
t.test(male$Height, female$Height, var.equal = TRUE, alternative = 'greater')$p.value
|
||||
t.test(male$Height, female$Height, var.equal = TRUE, alternative = 'greater')
|
||||
t.test(male$Height, female$Height, paired = TRUE, alternative = 'less')$p.value
|
||||
t.test(male$Height, female$Height, paired = TRUE, alternative = 'greater')$p.value
|
||||
t.test(male$Height, female$Height, alternative = 'greater')$p.value
|
||||
t.test(male$Height, female$Height, alternative = 'greater')
|
||||
t.test(male$Height, female$Height, alternative = 'greater', conf.level = 0.05)$p.value
|
||||
selected <- computers[computers$screen == 14, ]
|
||||
View(selected)
|
||||
ram_procent <- data.frame(cbind(liczebnosc = table(selected$ram),
|
||||
procent = prop.table(selected$ram)))
|
||||
table(selected$ram)
|
||||
prop.table(selected$ram)
|
||||
table(selected$ram)
|
||||
liczebnosc <- table(selected$ram)
|
||||
prop.table(liczebnosc)
|
||||
prop.table(liczebnosc)*100
|
||||
# ZAD 2 - tego trochę nie rozumiem
|
||||
w_test <- function(x, istotnosc, delta_zero, alternative = c('two.sided', 'less', 'greater')) {
|
||||
# statystyka testowa
|
||||
ss <- (1 / length(x)) * (var(x) - mean(x))
|
||||
statistic <- length(x) * ss / delta_zero * delta_zero
|
||||
# parametr w obszarach krytycznych
|
||||
d <- length(x) - 1
|
||||
# poziom istotności
|
||||
alternative <- match.arg(alternative)
|
||||
p_value <- istotnosc
|
||||
p_value <- switch(alternative,
|
||||
'two.sided' = 2 * min(p_value, 1 - p_value),
|
||||
'greater' = p_value,
|
||||
'less' = 1 - p_value)
|
||||
# rezultat
|
||||
names(statistic) <- 'T'
|
||||
names(d) <- 'num df'
|
||||
result <- list(statistic = statistic,
|
||||
parameter = d,
|
||||
p.value = p_value,
|
||||
alternative = alternative,
|
||||
method = 'Test istotności dla wariancji w modelu normalnym',
|
||||
data.name = deparse(substitute(x)))
|
||||
class(result) <- 'htest'
|
||||
return(result)
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
Version: 1.0
|
||||
|
||||
RestoreWorkspace: Default
|
||||
SaveWorkspace: Default
|
||||
AlwaysSaveHistory: Default
|
||||
|
||||
EnableCodeIndexing: Yes
|
||||
UseSpacesForTab: Yes
|
||||
NumSpacesForTab: 2
|
||||
Encoding: UTF-8
|
||||
|
||||
RnwWeave: Sweave
|
||||
LaTeX: pdfLaTeX
|
|
@ -0,0 +1,89 @@
|
|||
# testowy egzamin
|
||||
|
||||
# ładowanie danych csv
|
||||
computers <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/computers.csv")
|
||||
spotify <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/spotify.csv")
|
||||
weight_height <- read.csv("http://pp98647.home.amu.edu.pl/wp-content/uploads/2021/06/weight-height.csv")
|
||||
|
||||
|
||||
# ZAD 2 - tego trochę nie rozumiem
|
||||
w_test <- function(x, istotnosc, delta_zero, alternative = c('two.sided', 'less', 'greater')) {
|
||||
|
||||
# statystyka testowa
|
||||
ss <- (1 / length(x)) * (var(x) - mean(x))
|
||||
statistic <- length(x) * ss / delta_zero * delta_zero
|
||||
|
||||
# parametr w obszarach krytycznych
|
||||
d <- length(x) - 1
|
||||
|
||||
# poziom istotności
|
||||
alternative <- match.arg(alternative)
|
||||
p_value <- istotnosc
|
||||
p_value <- switch(alternative,
|
||||
'two.sided' = 2 * min(p_value, 1 - p_value),
|
||||
'greater' = p_value,
|
||||
'less' = 1 - p_value)
|
||||
|
||||
# rezultat
|
||||
names(statistic) <- 'T'
|
||||
names(d) <- 'num df'
|
||||
result <- list(statistic = statistic,
|
||||
parameter = d,
|
||||
p.value = p_value,
|
||||
alternative = alternative,
|
||||
method = 'Test istotności dla wariancji w modelu normalnym',
|
||||
data.name = deparse(substitute(x)))
|
||||
class(result) <- 'htest'
|
||||
return(result)
|
||||
}
|
||||
|
||||
|
||||
# ZAD 5
|
||||
model_1 <- lm(valence ~ acousticness + danceability + energy + instrumentalness +
|
||||
liveness + loudness + speechiness + tempo, data = spotify)
|
||||
|
||||
summary(model_1)
|
||||
|
||||
step(model_1)
|
||||
|
||||
model_2 <- lm(valence ~ acousticness + danceability + energy + instrumentalness +
|
||||
liveness + loudness + speechiness, data = spotify)
|
||||
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.305, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
|
||||
new_data <- data.frame(acousticness=2.84e-06, danceability=0.405, energy=0.827,
|
||||
instrumentalness=2.45e-03, liveness=0.3350, loudness=-5.789,
|
||||
speechiness=0.1470, tempo=159.882)
|
||||
|
||||
stats::predict(model_2, new_data, interval = "prediction")
|
||||
|
||||
|
||||
# ZAD 6
|
||||
par(mfrow = c(1, 2))
|
||||
|
||||
male <- weight_height[weight_height$Gender == "Male", ]
|
||||
shapiro.test(male$Height)
|
||||
qqnorm(male$Height)
|
||||
mean(male$Height)
|
||||
var(male$Height)
|
||||
|
||||
female <- weight_height[weight_height$Gender == "Female", ]
|
||||
shapiro.test(female$Height)
|
||||
qqnorm(female$Height)
|
||||
mean(female$Height)
|
||||
var(female$Height)
|
||||
|
||||
t.test(male$Height, female$Height, alternative = 'greater')$p.value
|
||||
#bardzo mała wartość, czyli hipoteza że kobiety są jest większe niż mężczyźni jest mało prawdopodobne
|
||||
|
||||
|
||||
# ZAD 7
|
||||
selected <- computers[computers$screen == 14, ]
|
||||
liczebnosc <- table(selected$ram)
|
||||
prop.table(liczebnosc)*100
|
||||
ram_procent <- data.frame(cbind(liczebnosc = table(selected$ram),
|
||||
procent = prop.table(selected$ram)))
|
|
@ -10,9 +10,9 @@ W którą stronę - prawo/lewo, zmienna się rozkłada.
|
|||
## Kurtoza
|
||||
Miara skupienia wartości wokół średniej.
|
||||
Porównuje rozkład empiryczny z rozkładem normalnym.
|
||||
- Dodatnia, im większa wartość tym bardziej wartości skupione wokół średniej
|
||||
- Dla rozkłądu normalnego = 0
|
||||
- Dla ujemnych (min -2) wykres jest bardziej spłaszczony ni rozkłąd normalny
|
||||
- Większa niż 0, im większa wartość tym bardziej wartości skupione wokół średniej
|
||||
- Dla rozkładu normalnego = 0
|
||||
- Dla ujemnych (min -2) wykres jest bardziej spłaszczony niż rozkłąd normalny
|
||||
|
||||
## Rozkład empiryczny
|
||||
Za pomocą szeregu rozdzielczego to np podanie liczebności i udziału procentowego danej zmiennej
|
||||
|
|