****************** STATISTIKA ************************************************* x <- read.table("test3.vec", header=TRUE) dobimo dva vektorja x$v1 in x$v2 plot(x$v1, x$v2) var(x) - variance/kovariance za vse spremenljivke cov(x$v2,x$v1) - kovarianca med dvema cor(x$v2,x$v1) - korelacijski k. cor(x) - korelacijski k. med vsemi pari summary(x) - vrne opisne statistike za obe spremenljivki quantile(x$v1,0.1) - kvantili - prvi decil quantile(x$v1,seq(0.1,1,0.1)) - vsi decili ---------------------------------- table(x) ali table(x$v1,x$v2) izpise kontingencno tabelo lahko tudi y <- table(x$v1,x$v2) potem summary(y) izpise hi-kvadrat test podobno chisq.test(y) ----------------------------------------- lahko podatke o frekvencah v kontingencni tabeli ze imamo: x <- matrix(c(200,300,100,200,400,500),2) chisq.test(x) vrne Pearson's Chi-squared test data: x X-squared = 11.873, df = 2, p-value = 0.002641 ----------------------------------------- V datotekah metoda1.txt in metoda2.txt so shranjeni podatki o ucinkovitosti po dveh metodah. x<-scan('metoda1.txt') y<-scan('metoda2.txt') summary(x) vrne rezultat Min. 1st Qu. Median Mean 3rd Qu. Max. 79.97 80.02 80.03 80.02 80.04 80.05 summary(y) slika boxplot - boxplot(x,y) - narise min, max, Q1, Q3 im Mediano - vidimo, da daje prva metoda boljse rezultate ----------------------------------------- unpaired sample t-test t.test(x,y) Rezultat * Welch Two Sample t-test * * data: x and y * t = 3.2499, df = 12.027, p-value = 0.00694 * alternative hypothesis: true difference in means is not equal to 0 * 95 percent confidence interval: * 0.01385526 0.07018320 * sample estimates: * mean of x mean of y * 80.02077 79.97875 Razlika je statisticno znacilna. ----------------------------------------- preverjanje enakosti varianc var.test(x, y) * F test to compare two variances * * data: x and y * F = 0.5837, num df = 12, denom df = 7, p-value = 0.3938 * alternative hypothesis: true ratio of variances is not equal to 1 * 95 percent confidence interval: * 0.1251097 2.1052687 * sample estimates: * ratio of variances * 0.5837405 Razlika ni statisticno znacilna - varianci nista statisticno znacilno razlicni, zato lahko uporabimo t-test, ki predvideva enakost varianc ----------------------------------------- t.test(x,y,var.equal=TRUE) * Two Sample t-test * * data: x and y * t = 3.4722, df = 19, p-value = 0.002551 * * alternative hypothesis: true difference in means is not equal to 0 * 95 percent confidence interval: * 0.01669058 0.06734788 * sample estimates: * mean of x mean of y * 80.02077 79.97875 ----------------------------------------- REGRESIJA Se enkrat preberemo podatke x <- read.table("pod1.txt",header=TRUE) lm(x$Price ~ x$Area) - linearna regresija - 1 neodvisna spremenljivka ali model1 <- lm(x$Price ~ x$Area) rezultat pogledamo z model1 Coefficients: (Intercept) x$Area 50.646559 0.006844 Price = 50.65 + 0.006844 * Area ********************************************** linearna regresija - 2 neodvisni spremenljivki model2 <- lm(x$Price ~ x$Area + x$Rooms) Coefficients: (Intercept) x$Area x$Rooms 22.90751 0.01497 4.04428 coefficients(model2) formula(model2) summary(model2) - obseznejsi izpis rezultatov *********************************************** Manjkajoce vrednosti v izracunih korelacij: x <- c(1, 2, 3, NA, 5, 7, 9) y <- c(3, 2, 4, 5, 1, 3, 4) z <- c(NA, 2, 3, 5, 4, 3, 4) m <- data.frame(x, y, z) m x y z 1 1 3 NA 2 2 2 2 3 3 4 3 4 NA 5 5 5 5 1 4 6 7 3 3 7 9 4 4 cor(m) x y z x 1 NA NA y NA 1 NA z NA NA 1 complete.obs - zbrise vse primere z manjkajocimi vrednostmi preden gre racunat korelacije: cor(m, use = "complete.obs") x y z x 1.0000 0.34819 0.70957 y 0.3482 1.00000 0.04583 z 0.7096 0.04583 1.00000 pairwise.complete.obs - zbrise samo primere z manjkajocimi vrednostmi, ki nastopajo v vsakem racunanju korelacije posebej: cor(m, use = "pairwise.complete.obs") x y z x 1.0000 0.2498 0.7096 y 0.2498 1.0000 0.4534 z 0.7096 0.4534 1.0000 Korelacija med x in z je enaka po obeh metodah, medtem ko je korelacija z y razlicna po obeh metodah. ***********************************************