# 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/domacnosti.csv", header = TRUE, skip = 5) str (tabulka) summary (tabulka) C <- tabulka$C P <- tabulka$P V <- tabulka$V X <- as.matrix (tabulka) # Ciselne charakterisitky apply (X, 2, mean) cov (X) cor (X) # Scatter-plot pairs (X) # MNOHONASOBNA LINEARNI REGRESE # Linearni regresni model model <- lm (V ~ P + C, data = tabulka) # Koeficienty mnohonasobne linearni regrese summary (model) # Predikce vynosu (V) v zavislosti na poctu clenu domacnosti (C) a prijmech (P) c <- seq (1, 5, by = 0.1) p <- seq (0, 150, by = 0.1) grid.2d <- expand.grid (C = c, P = p) prediction <- predict (model, grid.2d, interval = "prediction") V.predicted <- matrix (prediction[,1], length (c), length (p)) # Okno 3D grafiky open3d () # col <- rainbow (length (V.predicted))[rank(V.predicted)] # paleta barvy duhy col <- terrain.colors (length (V.predicted))[rank(V.predicted)] # paleta topograficka mapa # 3D plocha persp3d (c, p, V.predicted, col = col, lit = FALSE, front = "fill", back = "fill", smooth = FALSE, alpha = 0.9, xlab = "C", ylab = "P", zlab = "V") # Body ve 3D spheres3d (tabulka$C, tabulka$P, tabulka$V, 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 # V . C P # 1. jako korelace s nejlepsim linearnim odhadem m <- lm (V ~ C + P, data = tabulka) v <- summary (m) cor (V, m$fitted.values) # 2. jako r.squared v linearnim regresnim modelu sqrt (v$r.squared) # Dopocitejte dalsi mnohonasobne korelace # P . C V # C . P V # PARCIALNI KORELACE # P V . C R.parc$estimate["P","V"] R.parc$estimate["V","P"] # 1. jako korelace mezi dvema rezidui m1 <- lm (P ~ C, data = tabulka) m2 <- lm (V ~ C, data = tabulka) v1 <- summary (m1) v2 <- summary (m2) cor (m1$residuals, m2$residuals) # 2. pomoci r.squared v linearnich regresnich modelech m01 <- lm (P ~ C + V, data = tabulka) v01 <- summary (m01) sqrt ( (v01$r.squared - v1$r.squared) / (1 - v1$r.squared) ) m02 <- lm (V ~ C + P, data = tabulka) v02 <- summary (m02) sqrt ( (v02$r.squared - v2$r.squared) / (1 - v2$r.squared) ) # Dopocitejte dalsi parcialni korelace # C P . V # C V . P # SEMIPARCIALNI KORELACE # P (V . C) R.semiparc$estimate["P","V"] # 1. jako korelace mezi jednou velicinou a reziduem druhe veliciny m2 <- lm (V ~ C, data = tabulka) cor (P, m2$residuals) # 2. pomoci r.squared v linearnich regresnich modelech m01 <- lm (P ~ V + C, data = tabulka) v01 <- summary (m01) m1 <- lm (P ~ C, data = tabulka) v1 <- summary (m1) sqrt (v01$r.squared - v1$r.squared) # V (P . C) R.semiparc$estimate["V","P"] # 1. jako korelace mezi jednou velicinou a reziduem druhe veliciny m1 <- lm (P ~ C, data = tabulka) cor (V, m1$residuals) # 2. pomoci r.squared v linearnich regresnich modelech m02 <- lm (V ~ P + C, data = tabulka) v02 <- summary (m02) m2 <- lm (V ~ C, data = tabulka) v2 <- summary (m2) sqrt (v02$r.squared - v2$r.squared) # Dopocitejte dalsi semiparcialni korelace # C( P . V) # P (C . V) # C (V . P) # V (C . P)