# Potrebne knihovny library (Hmisc) # rcorr library (ppcor) # pcor, spcor, pcor.test, spcor.test library (corrplot) # corrplot library (rgl) # 3D grafika # Nacteni dat tabulka <- read.csv2 ("../data/deti.csv") str (tabulka) summary (tabulka) hmotnost <- tabulka$hmotnost body <- tabulka$body vek <- tabulka$vek X <- as.matrix (tabulka[,2:4]) # Ciselne charakterisitky apply (X, 2, mean) cov (X) cor (X) # Scatter-plot pairs (X) # MNOHONASOBNA LINEARNI REGRESE # Linearni regresni model model <- lm (body ~ hmotnost + vek, data = tabulka) # Koeficienty mnohonasobne linearni regrese summary (model) # Predikce vynosu (V) v zavislosti na poctu clenu domacnosti (C) a prijmech (P) h <- seq (20, 60, by = 0.1) v <- seq (5, 15, by = 0.1) grid.2d <- expand.grid (hmotnost = h, vek = v) prediction <- predict (model, grid.2d, interval = "prediction") body.predicted <- matrix (prediction[,1], length (h), length (v)) # Okno 3D grafiky open3d () # col <- rainbow (length (body.predicted))[rank(body.predicted)] # paleta barvy duhy col <- terrain.colors (length (body.predicted))[rank(body.predicted)] # paleta topograficka mapa # 3D plocha persp3d (h, v, body.predicted, col = col, lit = FALSE, front = "fill", back = "fill", smooth = FALSE, alpha = 0.9, xlab = "H", ylab = "V", zlab = "B") # Body ve 3D spheres3d (tabulka$hmotnost, tabulka$vek, tabulka$body, col = "black", radius = 1, lit = FALSE) # Uzavreni okna s 3D grafikou rgl.close () # KORELACNI ANALYZA # Korelacni matice a testy vyznamnosti R <- rcorr (X) R$r R$P # Matice parcialnich korelaci a testy vyznamnosti R.parc <- pcor (X) R.parc$estimate R.parc$p.value # Matice semiparcialnich korelaci a testy vyznamnosti R.semiparc <- spcor (X) R.semiparc$estimate R.semiparc$p.value # Korelogramy corrplot (R$r, p.mat = R$P, method = "circle") corrplot (R.parc$estimate, p.mat = R.parc$p.value, method = "circle") corrplot (R.semiparc$estimate, p.mat = R.semiparc$p.value, method = "circle") # MNOHONASOBNA KORELACE # body . hmotnost vek # 1. jako korelace s nejlepsim linearnim odhadem m <- lm (body ~ hmotnost + vek, data = tabulka) v <- summary (m) cor (body, m$fitted.values) # 2. jako r.squared v linearnim regresnim modelu sqrt (v$r.squared) # Dopocitejte dalsi mnohonasobne korelace # PARCIALNI KORELACE # body hmotnost . vek R.parc$estimate["body","hmotnost"] R.parc$estimate["hmotnost","body"] # 1. jako korelace mezi dvema rezidui m1 <- lm (body ~ vek, data = tabulka) m2 <- lm (hmotnost ~ vek, data = tabulka) v1 <- summary (m1) v2 <- summary (m2) cor (m1$residuals, m2$residuals) # 2. pomoci r.squared v linearnich regresnich modelech m01 <- lm (body ~ hmotnost + vek, data = tabulka) v01 <- summary (m01) sqrt ( (v01$r.squared - v1$r.squared) / (1 - v1$r.squared) ) m02 <- lm (hmotnost ~ body + vek, data = tabulka) v02 <- summary (m02) sqrt ( (v02$r.squared - v2$r.squared) / (1 - v2$r.squared) ) # Dopocitejte dalsi parcialni korelace # SEMIPARCIALNI KORELACE # hmotnost (body . vek) R.semiparc$estimate["body","hmotnost"] # 1. jako korelace mezi jednou velicinou a reziduem druhe veliciny m2 <- lm (hmotnost ~ vek, data = tabulka) cor (body, m2$residuals) # 2. pomoci r.squared v linearnich regresnich modelech m01 <- lm (body ~ hmotnost + vek, data = tabulka) v01 <- summary (m01) m1 <- lm (body ~ vek, data = tabulka) v1 <- summary (m1) sqrt (v01$r.squared - v1$r.squared) # V (P . C) R.semiparc$estimate["hmotnost","body"] # 1. jako korelace mezi jednou velicinou a reziduem druhe veliciny m1 <- lm (body ~ vek, data = tabulka) cor (hmotnost, m1$residuals) # 2. pomoci r.squared v linearnich regresnich modelech m02 <- lm (hmotnost ~ body + vek, data = tabulka) v02 <- summary (m02) m2 <- lm (hmotnost ~ vek, data = tabulka) v2 <- summary (m2) sqrt (v02$r.squared - v2$r.squared) # Dopocitejte dalsi semiparcialni korelace