Štatistika - cviko v R
Created: 2012-03-31 - 19:18
R version 2.13.1 Patched (2011-07-22 r56481)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i386-pc-mingw32/i386 (32-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
Loading required package: rcom
Loading required package: rscproxy
> utils:::menuInstallPkgs()
--- Please select a CRAN mirror for use in this session ---
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
> x = rnorm(100)
> min(x)
[1] -2.214016
> max(x)
[1] 3.21365
> x
[1] -0.94722393 0.26114322 1.21136948 1.24876620 1.62121428 -1.65436597 1.09504437 0.55328724 0.26781693 0.88449261 -0.52192671 0.75450917 -0.01096783 -0.15491704
[15] -0.45119001 -0.02777787 0.86946711 0.78728977 -0.27691471 -0.50251016 3.21364982 0.99616221 -0.75544204 0.72113035 0.06771297 -1.21470654 -0.14194687 -0.21776183
[29] 0.80296853 -0.78796429 0.44638858 0.25615215 -0.48397229 0.82682828 -0.98061042 0.09826682 -0.33951689 -0.78669167 -0.07223029 -0.64335980 -0.10680862 1.88865068
[43] -2.21401604 -0.46350949 0.43889553 -1.07273778 -0.97999941 -1.63440144 0.59110821 0.85800290 0.92513254 1.88726570 -1.56524564 -0.32039809 -2.19154929 0.91349004
[57] 0.52479162 0.57548224 0.29499456 1.59831611 -0.12642759 1.29541787 2.04131531 1.37062340 1.05269502 -0.29097545 1.34737414 0.80685092 0.65192730 0.99055427
[71] -0.60621055 -1.58646413 0.25580931 0.80146002 0.04579534 0.99375433 -0.11053758 -0.21005887 0.13120021 0.35940287 0.17311264 1.09093131 0.44041842 -0.30593590
[85] -0.91882202 -0.32876722 -1.10254861 -1.26079882 -0.14850136 0.90597316 -1.53368851 -0.69193030 0.19492055 -0.99924244 0.78108644 -0.57499383 1.86240090 0.64761823
[99] 1.12685042 -0.52939044
> sum(x)
[1] 14.00133
> plot(x)
> mean(x)
[1] 0.1400133
> var(x)
[1] 0.9658413
> sd(x)
[1] 0.9827722
> help sd
Error: unexpected symbol in "help sd"
> help(sd)
starting httpd help server ... done
> sd(x)*sd(x)
[1] 0.9658413
> cummax(x)
[1] -0.9472239 0.2611432 1.2113695 1.2487662 1.6212143 1.6212143 1.6212143
[8] 1.6212143 1.6212143 1.6212143 1.6212143 1.6212143 1.6212143 1.6212143
[15] 1.6212143 1.6212143 1.6212143 1.6212143 1.6212143 1.6212143 3.2136498
[22] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[29] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[36] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[43] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[50] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[57] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[64] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[71] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[78] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[85] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[92] 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498 3.2136498
[99] 3.2136498 3.2136498
> diff(x)
[1] 1.20836715 0.95022626 0.03739672 0.37244808 -3.27558025 2.74941034
[7] -0.54175713 -0.28547031 0.61667568 -1.40641932 1.27643588 -0.76547701
[13] -0.14394921 -0.29627298 0.42341214 0.89724499 -0.08217734 -1.06420448
[19] -0.22559545 3.71615999 -2.21748761 -1.75160426 1.47657239 -0.65341738
[25] -1.28241951 1.07275967 -0.07581496 1.02073035 -1.59093282 1.23435287
[31] -0.19023643 -0.74012444 1.31080057 -1.80743871 1.07887724 -0.43778371
[37] -0.44717478 0.71446138 -0.57112951 0.53655118 1.99545930 -4.10266672
[43] 1.75050655 0.90240503 -1.51163331 0.09273836 -0.65440203 2.22550965
[49] 0.26689469 0.06712964 0.96213316 -3.45251134 1.24484755 -1.87115120
[55] 3.10503932 -0.38869842 0.05069062 -0.28048768 1.30332155 -1.72474370
[61] 1.42184546 0.74589744 -0.67069191 -0.31792838 -1.34367047 1.63834959
[67] -0.54052322 -0.15492362 0.33862696 -1.59676481 -0.98025358 1.84227344
[73] 0.54565072 -0.75566468 0.94795899 -1.10429191 -0.09952130 0.34125908
[79] 0.22820266 -0.18629022 0.91781866 -0.65051289 -0.74635432 -0.61288612
[85] 0.59005480 -0.77378139 -0.15825020 1.11229746 1.05447451 -2.43966166
[91] 0.84175820 0.88685086 -1.19416299 1.78032887 -1.35608027 2.43739473
[97] -1.21478267 0.47923219 -1.65624086
> x=c(1,2,4,7)
> diff(x)
[1] 1 2 3
> order(x)
[1] 1 2 3 4
> x=c(1,2,4,7,0)
> order(x)
[1] 5 1 2 3 4
> x[order(x)]
[1] 0 1 2 4 7
> x=1:10;\
Error: unexpected input in "\"
> x=1:10;
> cbind(x, cumsum(x))
x
[1,] 1 1
[2,] 2 3
[3,] 3 6
[4,] 4 10
[5,] 5 15
[6,] 6 21
[7,] 7 28
[8,] 8 36
[9,] 9 45
[10,] 10 55
> cumsum(x)
[1] 1 3 6 10 15 21 28 36 45 55
> a = matrix(a,2,sum)
Error in matrix(a, 2, sum) : object 'a' not found
> a = matrix(1:8,2,4)
> a
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> apply(a,2,sum)
[1] 3 7 11 15
> length(x)
[1] 10
> sum((x-mean(x)) ^ 2) / (length(x) -1)
[1] 9.166667
> var(x)
[1] 9.166667
> x = c(0,1)
> sum((x-mean(x)) ^ 2) / (length(x) -1)
[1] 0.5
> var(x)
[1] 0.5
> var(x)*(length(x)-1)*length(x))
Error: unexpected ')' in "var(x)*(length(x)-1)*length(x))"
> var(x)*(length(x)-1)*length(x)
[1] 1
> x
[1] 0 1
> var(x)*(length(x)-1)*length(x)
[1] 1
> var(x)*(length(x)-1)/length(x)
[1] 0.25
> var(x)*((length(x)-1)/length(x))
[1] 0.25
> x = rnorm(100)
> x
[1] 0.78821160 -0.34141601 -0.07118281 -0.81201389 0.26874703 0.40554476
[7] -1.11694181 -0.19933451 -1.29289183 -0.55480136 -1.03333939 1.46018686
[13] 0.67552120 -0.42380821 -0.05398014 0.22887773 0.09455121 0.76003883
[19] 1.41872096 0.67029670 0.56177064 -0.32080959 -0.88749787 -0.40123754
[25] 1.96633308 -1.43774795 -1.85778523 0.94505757 -0.69617967 0.06200773
[31] -0.62744976 1.07270896 0.23929145 -0.13430310 -1.78934393 1.35698066
[37] -0.07239580 0.80122640 -1.47458190 -0.02868427 0.27760466 -1.05483148
[43] 0.24081589 -0.06147058 0.48091098 -1.01116158 0.54015487 -0.53456427
[49] -1.61545444 -1.92256937 0.32554970 -0.43367975 -0.42654656 0.66419550
[55] 1.15840604 0.76196264 -0.34744312 0.74916561 0.29587967 -1.01951437
[61] -1.17184571 0.18792459 -1.89407322 -0.32492129 0.97818688 -0.01554742
[67] 0.80625017 -0.16599103 1.05229013 0.27521208 0.20096005 -0.46024458
[73] -0.02534390 -1.43035488 0.88041532 -0.30755242 0.95139204 0.42419925
[79] 0.23959115 2.57858413 -0.91977888 0.02105786 -0.52366533 -0.01550240
[85] -0.22794635 0.07509667 0.68688054 1.04639559 0.44950427 -0.67035977
[91] 1.55770837 2.17103451 0.55390713 1.64566988 0.97634989 0.02244066
[97] -0.28195868 0.84729316 1.61858689 -0.75336858
> corr(x)
Error: could not find function "corr"
> cor(x)
Error in cor(x) : supply both 'x' and 'y' or a matrix-like 'x'
> cor(x,2*x)
[1] 1
> cor(x,-2*x)
[1] -1
> cor(x,2/x)
[1] 0.08945048
> plot(cor(x,2/x))
> plot(x,2/x)
> plot(x,2*x)
> x = rnorm(100); y = rnorm(100);
> plot(x,y)
> cor(x,y)
[1] -0.05883529
> hist(x)
> hist(y)
> hist(rnorm(1000))
> hist(rnorm(100000000))
Error: cannot allocate vector of size 381.5 Mb
In addition: Warning messages:
1: In hist.default(rnorm(1e+08)) :
Reached total allocation of 1535Mb: see help(memory.size)
2: In hist.default(rnorm(1e+08)) :
Reached total allocation of 1535Mb: see help(memory.size)
3: In hist.default(rnorm(1e+08)) :
Reached total allocation of 1535Mb: see help(memory.size)
4: In hist.default(rnorm(1e+08)) :
Reached total allocation of 1535Mb: see help(memory.size)
> hist(rnorm(1000000))
> set.seed(123)
> #upravime generator nah cisel
> x = rnorm(100); y = x+rnorm(100);
> corr(x,y)
Error: could not find function "corr"
> cor(x,y)
[1] 0.66713
> x = rnorm(100); y = x+rnorm(100); cor(x,y);
[1] 0.656549
> x = rnorm(100); y = x+rnorm(100); cor(x,y);
[1] 0.7899585
> set.seed(123); x = rnorm(100); y = x+rnorm(100); cor(x,y);
[1] 0.66713
> set.seed(123); x = rnorm(100); y = x+rnorm(100); cor(x,y);
[1] 0.66713
> set.seed(123); x = rnorm(100); y = x+rnorm(100); cor(x,y);
[1] 0.66713
> set.seed(123); x = rnorm(100); y = x+rnorm(100); cor(x,y);
[1] 0.66713
> y
[1] -1.270882210 0.026706220 1.312016436 -0.277034208 -0.822330832 1.670037262
[7] -0.323988263 -2.933003171 -1.067079372 0.473334639 0.648734835 0.967778149
[13] -1.217111258 0.055120750 -0.036433931 2.088066499 0.603526672 -2.607323165
[19] -0.148348444 -1.496920198 -0.950177109 -1.165449529 -1.516561892 -0.984983421
[25] 1.218822737 -2.338643212 1.073173617 0.231333967 -2.099993571 1.182506835
[31] 1.871015080 0.156432570 0.936358583 0.455636655 -1.231666140 1.819977468
[37] -0.906722417 0.678035800 1.603140905 -1.824364162 0.007077356 -0.470114767
[43] -2.837540511 0.654288312 -0.393574175 -1.654015105 -1.864640420 0.221261419
[49] 2.880074059 -1.370399543 1.041057361 0.740495486 0.289332122 0.360225676
[55] -0.345223592 1.236075269 -0.985763271 0.212174994 1.100827631 -0.158639289
[61] 1.432350948 -1.551500460 -1.593362628 2.222464552 -1.488648815 0.601756233
[67] 1.084779453 -0.430776399 1.439129512 2.419049213 -0.706411674 -2.243875842
[73] 0.971671271 1.419251136 -1.429344713 -0.070424897 -0.246984608 -0.910236963
[79] 0.617826959 -0.597256695 -1.057561948 1.648465577 -0.720310420 -0.221136314
[85] -0.456766131 0.134606070 2.206759303 0.519918783 0.428122200 0.649515601
[91] 1.207949166 0.223711048 0.333315263 -1.523269434 0.049850915 1.396953798
[97] 2.788041817 0.281339265 -0.846866276 -2.211900985
> plot(y~x)
> abline(lm(y~x));
> cor(x,y);
[1] 0.66713
> #lm je linearny model
> #d -density/p,, p-CDF/F, q - quantile, r -randorm
> rnorm(1,5)
[1] 7.19881
> pnorm(1,5)
[1] 3.167124e-05
> rnorm(1,5,0.01)
[1] 5.013124
> seq(-3,5,length.out=100)
[1] -3.00000000 -2.91919192 -2.83838384 -2.75757576 -2.67676768 -2.59595960
[7] -2.51515152 -2.43434343 -2.35353535 -2.27272727 -2.19191919 -2.11111111
[13] -2.03030303 -1.94949495 -1.86868687 -1.78787879 -1.70707071 -1.62626263
[19] -1.54545455 -1.46464646 -1.38383838 -1.30303030 -1.22222222 -1.14141414
[25] -1.06060606 -0.97979798 -0.89898990 -0.81818182 -0.73737374 -0.65656566
[31] -0.57575758 -0.49494949 -0.41414141 -0.33333333 -0.25252525 -0.17171717
[37] -0.09090909 -0.01010101 0.07070707 0.15151515 0.23232323 0.31313131
[43] 0.39393939 0.47474747 0.55555556 0.63636364 0.71717172 0.79797980
[49] 0.87878788 0.95959596 1.04040404 1.12121212 1.20202020 1.28282828
[55] 1.36363636 1.44444444 1.52525253 1.60606061 1.68686869 1.76767677
[61] 1.84848485 1.92929293 2.01010101 2.09090909 2.17171717 2.25252525
[67] 2.33333333 2.41414141 2.49494949 2.57575758 2.65656566 2.73737374
[73] 2.81818182 2.89898990 2.97979798 3.06060606 3.14141414 3.22222222
[79] 3.30303030 3.38383838 3.46464646 3.54545455 3.62626263 3.70707071
[85] 3.78787879 3.86868687 3.94949495 4.03030303 4.11111111 4.19191919
[91] 4.27272727 4.35353535 4.43434343 4.51515152 4.59595960 4.67676768
[97] 4.75757576 4.83838384 4.91919192 5.00000000
> x = seq(-3,5,length.out=100)
> y = dnorm(x,1,1);
> y
[1] 0.0001338302 0.0001842953 0.0002521381 0.0003427099 0.0004627846 0.0006208623
[7] 0.0008275148 0.0010957722 0.0014415473 0.0018840898 0.0024464615 0.0031560163
[13] 0.0040448664 0.0051503080 0.0065151783 0.0081881065 0.0102236211 0.0126820683
[19] 0.0156292995 0.0191360817 0.0232771927 0.0281301641 0.0337736510 0.0402854146
[25] 0.0477399263 0.0562056185 0.0657418315 0.0763955298 0.0881978860 0.1011608535
[31] 0.1152738702 0.1305008512 0.1467776382 0.1640100747 0.1820728700 0.2008093962
[37] 0.2200325354 0.2395266587 0.2590507715 0.2783428081 0.2971250031 0.3151102096
[43] 0.3320089800 0.3475371752 0.3614238299 0.3734189738 0.3833010942 0.3908839312
[49] 0.3960223134 0.3986167793 0.3986167793 0.3960223134 0.3908839312 0.3833010942
[55] 0.3734189738 0.3614238299 0.3475371752 0.3320089800 0.3151102096 0.2971250031
[61] 0.2783428081 0.2590507715 0.2395266587 0.2200325354 0.2008093962 0.1820728700
[67] 0.1640100747 0.1467776382 0.1305008512 0.1152738702 0.1011608535 0.0881978860
[73] 0.0763955298 0.0657418315 0.0562056185 0.0477399263 0.0402854146 0.0337736510
[79] 0.0281301641 0.0232771927 0.0191360817 0.0156292995 0.0126820683 0.0102236211
[85] 0.0081881065 0.0065151783 0.0051503080 0.0040448664 0.0031560163 0.0024464615
[91] 0.0018840898 0.0014415473 0.0010957722 0.0008275148 0.0006208623 0.0004627846
[97] 0.0003427099 0.0002521381 0.0001842953 0.0001338302
> plot(x,y)
> y1 = dnorm(x,1/100,1);
> plot(x,y1);
> screen(2);
[1] FALSE
> help(plot)
> help(screen)
> split.screen(2)
Error in seq.int(0, 1, length.out = nc + 1) :
'length.out' must be a non-negative number
> split.screen(0,2)
Error in split.screen(0, 2) : invalid screen number
> split.screen(1,2)
Error in split.screen(1, 2) : invalid screen number
> split.screen(c(1,2))
[1] 1 2
> screen(1);
> plot(x,y);
> screen(2);
> plot(x,y1);
> y1 = dnorm(x,1,0.5);
> plot(x,y1);
> split.screen(c(2,1));
[1] 3 4
> screen(1);
> plot(x,y);
> f = pnorm(x,mean=15,sd=1); plot(rnorm(100),f);
> x = seq(-3,5, length.out = 100);
> f = pnorm(x,mean=1,sd=1);
> plot(x,f);
> split.screen(1);
Error in seq.int(0, 1, length.out = nc + 1) :
'length.out' must be a non-negative number
> split.screen(c(1));
Error in seq.int(0, 1, length.out = nc + 1) :
'length.out' must be a non-negative number
> split.screen(c(1,1));
[1] 5
> plot(x,f);
> #quantil urovne 0.5 je 0: overenie
> qnorm(0.5,0,1)
[1] 0
> qt(0.95,df=25)
[1] 1.708141
> qchisq(0.95,df=25)
[1] 37.65248
> qf(0.07,10,5)
[1] 0.3420626
> qnorm(0.95,0,1)
[1] 1.644854
> qf(0.975,10,5)
[1] 6.619154
>
EXCEL:
HODNOTA | VZOREC EXCEL | PRIKAZ R | ||
1,644853627 | =NORMINV(0,95;0;1) | qnorm(0.95,0,1) | ||
1,708140761 | =TINV(2*(1-0,95);25) | qt(0.95,df=25) | ||
37,65248413 | =CHIINV(1-0,95;25) | qchisq(0.95,df=25) | ||
6,619154331 | =FINV(1-0,975;10;5) | qf(0.975,10,5) |