Un vector es una colección ordenada de números.
x <- c(1,2,3,4,5)
x
## [1] 1 2 3 4 5
y <- c(1,1,1,1,1)
y
## [1] 1 1 1 1 1
y <- rep(1,100)
y
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
z <- seq(0,1,length=100)
z
## [1] 0.00000000 0.01010101 0.02020202 0.03030303 0.04040404 0.05050505
## [7] 0.06060606 0.07070707 0.08080808 0.09090909 0.10101010 0.11111111
## [13] 0.12121212 0.13131313 0.14141414 0.15151515 0.16161616 0.17171717
## [19] 0.18181818 0.19191919 0.20202020 0.21212121 0.22222222 0.23232323
## [25] 0.24242424 0.25252525 0.26262626 0.27272727 0.28282828 0.29292929
## [31] 0.30303030 0.31313131 0.32323232 0.33333333 0.34343434 0.35353535
## [37] 0.36363636 0.37373737 0.38383838 0.39393939 0.40404040 0.41414141
## [43] 0.42424242 0.43434343 0.44444444 0.45454545 0.46464646 0.47474747
## [49] 0.48484848 0.49494949 0.50505051 0.51515152 0.52525253 0.53535354
## [55] 0.54545455 0.55555556 0.56565657 0.57575758 0.58585859 0.59595960
## [61] 0.60606061 0.61616162 0.62626263 0.63636364 0.64646465 0.65656566
## [67] 0.66666667 0.67676768 0.68686869 0.69696970 0.70707071 0.71717172
## [73] 0.72727273 0.73737374 0.74747475 0.75757576 0.76767677 0.77777778
## [79] 0.78787879 0.79797980 0.80808081 0.81818182 0.82828283 0.83838384
## [85] 0.84848485 0.85858586 0.86868687 0.87878788 0.88888889 0.89898990
## [91] 0.90909091 0.91919192 0.92929293 0.93939394 0.94949495 0.95959596
## [97] 0.96969697 0.97979798 0.98989899 1.00000000
w <- seq(0,1,by=0.01)
w
## [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14
## [16] 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29
## [31] 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43 0.44
## [46] 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59
## [61] 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.70 0.71 0.72 0.73 0.74
## [76] 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89
## [91] 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.00
Una matriz es una estructura bidimensional de datos.
Si \(A\) es una matriz de dimensión \(m \times n\):
\[ A = \begin{pmatrix} a_{11} & a_{12} & \dots & a_{1n}\\ a_{21} & a_{22} & \dots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & \dots & a_{mn} \end{pmatrix} \]
x <- matrix(c(1,2,4,5),2,2)
x
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
x <- matrix(c(1,2,4,5),2,2,byrow=TRUE)
x
## [,1] [,2]
## [1,] 1 2
## [2,] 4 5
y <- matrix(c(2,2,4,4),2,2,byrow=TRUE)
y
## [,1] [,2]
## [1,] 2 2
## [2,] 4 4
Si \(A\) y \(B\) tienen la misma dimensión:
\[ C = A + B \]
x + y
## [,1] [,2]
## [1,] 3 4
## [2,] 8 9
Multiplicación elemento a elemento:
\[ C_{ij} = A_{ij}B_{ij} \]
x * y
## [,1] [,2]
## [1,] 2 4
## [2,] 16 20
\[ C = AB \]
x %*% y
## [,1] [,2]
## [1,] 10 10
## [2,] 28 28
En R:
r ā generación aleatoriad ā densidadp ā probabilidad acumuladaq ā cuantilesEjemplo con distribución normal:
\[ X \sim N(\mu, \sigma^2) \]
x <- rnorm(1000,5,4)
hist(x,col="green", main="Normal")
mean(x)
## [1] 5.108045
var(x)
## [1] 15.16915
sd(x)
## [1] 3.894759
sqrt(var(x))
## [1] 3.894759
sd(x) == sqrt(var(x))
## [1] TRUE
plot(density(x),col=4)
Modelo de regresión:
\[ Y = \beta_0 + \beta_1 X + \varepsilon \]
donde:
x <- rnorm(1000)
y <- 3 + 4*x + rnorm(1000)
plot(x,y,col="pink")
fit <- lm(y~x)
fit
##
## Call:
## lm(formula = y ~ x)
##
## Coefficients:
## (Intercept) x
## 3.025 4.009
abline(a=fit$coefficients[1],
b=fit$coefficients[2],
col="red",lwd=2)
summary(fit)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.3554 -0.7019 -0.0198 0.6977 2.9384
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.02504 0.03194 94.71 <2e-16 ***
## x 4.00858 0.03291 121.82 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.01 on 998 degrees of freedom
## Multiple R-squared: 0.937, Adjusted R-squared: 0.9369
## F-statistic: 1.484e+04 on 1 and 998 DF, p-value: < 2.2e-16
\[ H_0 : \beta_1 = 0 \]
Se rechaza \(H_0\) si:
\[ p\text{-valor} < \alpha \]
donde:
\[ \alpha = 0.05, 0.01, 0.10 \]
Generación de múltiples muestras:
n <- 100
M <- 1000
x <- matrix(0,M,n)
for(i in 1:M){
x[i,] <- rnorm(n,10,5)
}
media <- NULL
for(i in 1:M){
media[i] <- mean(x[i,])
}
sd(media)
## [1] 0.5152265
Error estƔndar de la media:
\[ SE = \frac{\sigma}{\sqrt{n}} \]
Si \(X_1, X_2, ..., X_n\) son i.i.d.:
\[ \bar{X}_n \to \mu \]
cuando \(n \to \infty\).
x <- list()
for(i in 1:1000){
x[[i]] <- rgamma(i,4)
}
med <- NULL
for(i in 1:1000){
med[i] <- mean(x[[i]])
}
plot(med,type="l",col="green")
abline(h=4,col="red")
Si \(X_1,...,X_n\) son i.i.d. con media \(\mu\) y varianza \(\sigma^2\):
\[ \frac{\bar{X}-\mu}{\sigma/\sqrt{n}} \rightarrow N(0,1) \]
M <- 1000
n <- 100
data <- matrix(0,M,n)
for(i in 1:M){
data[i,] <- rgamma(n,4)
}
med <- NULL
for(i in 1:M){
med[i] <- mean(data[i,])
}
hist(med,col="green")
data <- matrix(0,M,n)
for(i in 1:M){
data[i,] <- rcauchy(n,4)
}
med <- NULL
for(i in 1:M){
med[i] <- mean(data[i,])
}
hist(med,col="green")
plot(density(med))
La distribución de Cauchy no tiene momentos finitos, por lo que el TLC no aplica.
data <- matrix(0,M,n)
for(i in 1:M){
data[i,] <- rpois(n,4)
}
med <- NULL
for(i in 1:M){
med[i] <- mean(data[i,])
}
hist(med,col="green")
plot(density(med))
x<-c(1,2,3,4,5)
x
## [1] 1 2 3 4 5
y<-c(1,1,1,1,1)
y
## [1] 1 1 1 1 1
y<-rep(1,100)
y
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
z<-seq(0,1,length=100)
z
## [1] 0.00000000 0.01010101 0.02020202 0.03030303 0.04040404 0.05050505
## [7] 0.06060606 0.07070707 0.08080808 0.09090909 0.10101010 0.11111111
## [13] 0.12121212 0.13131313 0.14141414 0.15151515 0.16161616 0.17171717
## [19] 0.18181818 0.19191919 0.20202020 0.21212121 0.22222222 0.23232323
## [25] 0.24242424 0.25252525 0.26262626 0.27272727 0.28282828 0.29292929
## [31] 0.30303030 0.31313131 0.32323232 0.33333333 0.34343434 0.35353535
## [37] 0.36363636 0.37373737 0.38383838 0.39393939 0.40404040 0.41414141
## [43] 0.42424242 0.43434343 0.44444444 0.45454545 0.46464646 0.47474747
## [49] 0.48484848 0.49494949 0.50505051 0.51515152 0.52525253 0.53535354
## [55] 0.54545455 0.55555556 0.56565657 0.57575758 0.58585859 0.59595960
## [61] 0.60606061 0.61616162 0.62626263 0.63636364 0.64646465 0.65656566
## [67] 0.66666667 0.67676768 0.68686869 0.69696970 0.70707071 0.71717172
## [73] 0.72727273 0.73737374 0.74747475 0.75757576 0.76767677 0.77777778
## [79] 0.78787879 0.79797980 0.80808081 0.81818182 0.82828283 0.83838384
## [85] 0.84848485 0.85858586 0.86868687 0.87878788 0.88888889 0.89898990
## [91] 0.90909091 0.91919192 0.92929293 0.93939394 0.94949495 0.95959596
## [97] 0.96969697 0.97979798 0.98989899 1.00000000
w<-seq(0,1,by=0.01)
w
## [1] 0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14
## [16] 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29
## [31] 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43 0.44
## [46] 0.45 0.46 0.47 0.48 0.49 0.50 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59
## [61] 0.60 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.70 0.71 0.72 0.73 0.74
## [76] 0.75 0.76 0.77 0.78 0.79 0.80 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89
## [91] 0.90 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.00
# Se llenan las matrices por columna
x<-matrix(c(1,2,4,5),2,2)
x
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
x<-matrix(c(1,2,4,5),2,2,byrow=TRUE)
x
## [,1] [,2]
## [1,] 1 2
## [2,] 4 5
y<-matrix(c(2,2,4,4),2,2,byrow=TRUE)
y
## [,1] [,2]
## [1,] 2 2
## [2,] 4 4
# Suma de matrices
x+y
## [,1] [,2]
## [1,] 3 4
## [2,] 8 9
# Multilicación de matrices
x
## [,1] [,2]
## [1,] 1 2
## [2,] 4 5
y
## [,1] [,2]
## [1,] 2 2
## [2,] 4 4
# Producto de Hadamark que es componente a componente
x*y
## [,1] [,2]
## [1,] 2 4
## [2,] 16 20
# Producto usual
x
## [,1] [,2]
## [1,] 1 2
## [2,] 4 5
y
## [,1] [,2]
## [1,] 2 2
## [2,] 4 4
x%*%y
## [,1] [,2]
## [1,] 10 10
## [2,] 28 28
# r: random,d: density,p: probability,q: quantile
# X dis n(5,4)
x<-rnorm(1000,5,4)
x
## [1] 3.474684607 7.420324679 1.497065391 7.250591268 5.505430831
## [6] -0.869904754 4.924761998 1.310670333 3.534421789 4.060125175
## [11] 6.399336838 2.780856408 6.921641748 9.246184146 6.320648930
## [16] 5.492882088 8.407311475 -0.153929662 2.305490180 9.897853485
## [21] 3.369758891 0.343401147 0.660497015 12.373540926 11.825486167
## [26] 0.862447193 12.917949385 5.021511795 -3.066501955 8.176970392
## [31] 2.484454984 5.525914344 5.107595334 5.672018536 3.977086886
## [36] 6.081721848 4.389369623 11.815967387 12.036008244 9.612018170
## [41] 5.657510763 11.122579926 -3.923438133 8.334116261 1.509181621
## [46] 9.954526892 8.267050516 -6.052568321 5.311514173 6.653746860
## [51] -6.481099056 4.157102117 2.840117878 7.665616795 0.754807329
## [56] 8.778683892 7.987423323 5.740055775 2.438016618 2.558481112
## [61] 0.815066238 0.009869510 8.580916228 2.726622912 14.122032220
## [66] 5.746572507 9.998924120 5.071396091 2.723953276 12.678309922
## [71] 11.429862139 8.415198506 5.564892518 5.022329627 4.273147057
## [76] 10.625917593 0.494069412 9.389025885 2.760596292 2.501797009
## [81] 3.519148228 8.129694755 10.548147308 8.289587345 6.782978913
## [86] 4.561714535 4.101181437 7.012882611 5.242034491 14.994730170
## [91] 11.156509604 10.139818161 6.028077721 4.332565966 4.125505267
## [96] 3.670164889 4.155391182 3.382312213 13.880870290 1.503887789
## [101] 6.746575764 6.211597363 -1.655514382 6.323724641 0.519895984
## [106] 5.603494852 2.439794240 -2.112740676 1.979799669 3.059792769
## [111] -3.116250478 3.912580021 0.800780424 2.923449179 14.529240559
## [116] 6.664848263 11.770772376 0.391956734 8.154956534 3.947421029
## [121] 10.166112097 9.372115789 14.928545180 14.573015969 10.840510801
## [126] 8.447358151 10.094736282 1.805432560 6.193240088 6.433440756
## [131] 7.098424263 4.451237197 3.662369169 6.728357107 5.210854343
## [136] 2.481048032 2.356505158 3.758334162 3.914710910 8.615638113
## [141] 9.208379645 3.357286796 0.841186496 7.657544923 11.618247988
## [146] 6.773689764 3.805805984 1.782478310 5.375400588 -1.186153638
## [151] 0.100002431 10.912913938 2.317429655 2.428805703 8.195846139
## [156] 11.712462576 2.601481491 1.219431285 9.074008847 8.070501410
## [161] 5.271895307 -5.143247176 -1.243744539 0.501552022 5.072428145
## [166] 6.645936883 -1.459088700 6.504035815 6.024886219 0.419939338
## [171] 2.206398487 3.783880929 7.254838476 9.038004605 2.953503632
## [176] 0.001508494 2.734152478 0.154880983 10.216859831 0.964735545
## [181] 2.833954423 6.822452207 2.119496524 6.127677372 7.487115259
## [186] 7.256948114 4.149542198 0.592511203 8.020448253 8.883434985
## [191] 7.692855199 4.352450962 5.432819648 7.999474763 1.776813767
## [196] -0.480799556 2.583620500 9.137150632 0.523807937 6.808107200
## [201] 10.513268308 -0.956290143 1.765208093 1.661385210 1.969397921
## [206] 3.517201454 3.959808981 3.137083624 8.156857763 6.716057159
## [211] 10.195654426 6.074401668 10.433946895 5.785500211 3.759708364
## [216] 8.110674269 1.934440320 2.622930417 4.887601744 7.146307316
## [221] 11.741147430 -3.695832824 8.982758986 9.065500018 10.266216548
## [226] 3.953031038 7.431990568 3.736921509 6.735031467 5.084436785
## [231] 5.734487881 11.475987310 1.662853252 7.585317256 -0.892456188
## [236] 5.686093841 1.530705966 1.823912288 13.204580026 8.362904834
## [241] 7.567341434 4.004384064 2.396380614 10.055334931 7.438607506
## [246] 8.525369130 4.094050278 6.921369291 2.484918041 13.899449923
## [251] 3.667600203 4.553185525 3.356375038 -5.898681305 14.201556458
## [256] 7.400146448 6.851324837 4.218654150 3.774300302 7.126327559
## [261] 4.343926208 5.099927019 5.217943137 5.387280880 7.102158530
## [266] 1.932642329 1.160512752 14.231837573 -0.712154876 5.838095502
## [271] 7.323898326 9.151806298 4.599542682 4.906072846 -0.164947968
## [276] 3.578035692 4.961401317 6.520516760 5.634072443 -0.847030541
## [281] 5.517459371 5.846357056 4.998742519 2.297417431 5.314570632
## [286] 9.062258884 -0.752688349 6.660508497 -2.420185782 4.235866571
## [291] 4.222010759 7.395041331 9.159081676 0.443597555 7.022868252
## [296] 1.983800643 9.218371712 3.145094678 1.967727165 8.269209402
## [301] -2.843536057 8.427618378 8.286263736 4.653670954 7.238362781
## [306] 8.610306036 2.823853957 3.369508223 -2.408756486 4.359174314
## [311] 5.463209264 5.586474926 4.627487195 -5.960382347 4.802349550
## [316] 2.814545695 2.957929437 5.762580321 0.704319023 5.045323576
## [321] 5.820351401 3.097356623 10.009627811 4.618118082 6.282734853
## [326] 0.731450129 2.884001578 -0.618229631 4.193095838 9.147077707
## [331] 7.905173145 8.863480148 6.330937562 9.561563611 1.966041699
## [336] -0.166057251 9.807896757 3.512440391 3.332957700 1.917076192
## [341] 5.127833553 11.587346492 4.626799294 2.219799911 8.010459753
## [346] 9.858631856 10.989459696 12.812834229 7.130296645 4.725396695
## [351] 8.957885254 11.305414235 7.433889717 8.713556344 5.475605768
## [356] 8.840853256 -0.444654285 5.494318713 2.732750312 7.902191816
## [361] 7.022360196 1.024888216 5.810934768 2.867132140 3.764079576
## [366] -0.727749861 6.673871341 0.088575841 5.606422946 17.287677867
## [371] 8.836772068 3.892282593 3.839883739 5.220662960 7.728351168
## [376] 10.243688578 0.706239368 15.712084394 5.504886537 4.209484494
## [381] 6.537559028 -0.133673468 1.960174984 11.640157389 10.647025002
## [386] 11.382353271 4.951187261 7.523171542 5.227986195 0.381118959
## [391] 3.489998998 12.443042371 2.388884999 -2.410416191 0.675521486
## [396] 7.239130137 0.805632524 1.725984577 3.192737039 4.675745425
## [401] -2.345148321 -0.690784724 2.685188096 2.320429152 0.758375117
## [406] 1.843513851 8.307963656 8.720040807 -3.009462134 -0.479116753
## [411] 10.083487003 5.910129404 5.230821204 8.967874907 8.700506165
## [416] 0.842917972 -2.606668195 4.915078412 6.160260106 6.774399089
## [421] 5.726156365 2.011996460 9.124478468 7.496799679 5.005363542
## [426] 10.314044480 5.680551876 9.239251714 4.617354732 5.353141212
## [431] 4.451604138 7.584221442 3.452390288 6.896654912 0.618945432
## [436] 6.885587443 7.231196651 7.233470961 8.234094528 4.873752460
## [441] -5.098529051 6.683488010 2.979570788 4.906770939 10.024115712
## [446] 7.842031478 5.903590302 6.649856959 0.997047956 4.122194991
## [451] 2.422129630 5.367626581 1.164070882 2.008527500 6.542069677
## [456] 8.917622002 0.696295141 3.818390970 7.553193306 7.433751703
## [461] 9.428435973 8.543780273 10.256464945 4.793992375 6.764957074
## [466] -0.370977632 -4.329309678 7.755119629 8.743555890 2.945628899
## [471] 9.568276241 6.216154959 11.587614226 4.303784757 3.909473208
## [476] -4.420603149 3.980534497 -2.245261586 3.945892889 11.555989115
## [481] 8.717515708 6.443818083 3.607840970 6.840440308 8.018215450
## [486] -0.180141264 5.444214956 7.565679267 4.672794726 15.406444427
## [491] 4.497075211 4.858157769 -1.678381000 5.507229087 5.658567020
## [496] 7.430478304 6.841558794 5.115615555 4.176652688 9.603054704
## [501] 8.020854917 8.186285321 12.798698314 6.918310528 4.210247758
## [506] 2.403915852 8.230447854 -2.709790634 1.674512893 2.950784325
## [511] 11.816994186 3.405982521 11.192403062 -0.146870876 6.217149989
## [516] 6.090694239 5.170615338 2.976716266 2.951355052 10.031035046
## [521] 9.919007382 5.759660705 4.734321004 7.883428130 5.913672183
## [526] 6.082320738 5.769711466 2.989829830 8.806197497 9.078666174
## [531] 5.745042527 3.132755470 7.762807368 10.027936894 3.192759945
## [536] -5.916990112 2.185752370 0.529030879 2.955331724 7.602555535
## [541] 4.138300611 5.521052060 6.688745908 1.901278875 2.773768613
## [546] 3.829644701 4.206834108 -1.593613197 4.618540545 3.113942712
## [551] 10.818196078 0.974173814 -1.014562586 4.954760637 3.899933994
## [556] 6.868549495 4.618265759 2.184050490 4.708785107 8.404759348
## [561] 3.940141051 10.433709200 5.154754131 8.512160204 2.214268008
## [566] 4.261659349 1.653100252 5.702980357 12.938623928 6.619547371
## [571] 5.134526236 10.081113473 4.404191685 4.653959889 6.048944999
## [576] 8.057723006 6.576099846 8.324826856 3.655384150 3.081625458
## [581] 9.562542680 -0.319619698 11.465537580 1.085959886 0.755594105
## [586] 3.559216799 8.354000094 -0.119672060 9.770242985 6.535790192
## [591] -7.389972735 1.829239193 -5.389231363 7.049675973 5.891100084
## [596] 13.274963980 3.373535016 6.798734333 3.298325596 10.326967093
## [601] -0.590279924 5.186184543 7.845412786 3.439168935 6.067870067
## [606] 7.671929309 7.602586450 5.372539695 2.728278353 4.576858540
## [611] 1.398358347 0.928762157 6.452381610 6.654496583 3.716555107
## [616] 3.532775366 5.527044125 6.218741656 11.432415684 2.055109229
## [621] 5.296212428 8.367318543 6.434536169 7.698399145 -1.650668988
## [626] 3.454684759 -2.743837255 2.614079536 8.591819824 3.687501227
## [631] 8.667224128 2.684197112 4.710921085 2.731757027 2.150516700
## [636] 2.890599102 3.715345845 2.759072854 5.430537395 5.098936081
## [641] 9.019628887 3.601499633 8.307239729 3.244257724 4.373001570
## [646] 3.018793801 10.090721271 3.783136902 3.681011113 5.649504234
## [651] 7.147510826 6.973192631 1.263714240 1.681162538 8.110163495
## [656] 8.234624392 4.388560075 -3.261852662 1.738727482 6.814841655
## [661] 6.954317219 -3.613015017 3.542129806 -0.072089604 -3.162180873
## [666] 4.113368100 0.712525929 2.436815496 13.263809387 -0.871371750
## [671] 12.245804489 13.518846376 -0.851818147 8.216186887 8.449397767
## [676] 5.415257269 17.349612477 0.356392152 15.472960307 7.101077891
## [681] 3.815936951 3.094980546 2.439994315 7.478276393 3.096243892
## [686] 5.053231304 14.879353582 1.802682751 3.071746741 1.868082927
## [691] 5.885017396 4.283000487 7.135403159 5.651580662 2.101150304
## [696] 12.872907457 1.215896888 5.110016304 0.203470043 9.156608868
## [701] -2.221126394 5.471211202 9.116589747 0.323290685 8.184850296
## [706] 5.761433845 8.860027809 5.183217738 7.805600709 3.009759619
## [711] 2.722219292 0.565026727 9.005868927 10.101233075 5.489099563
## [716] 9.848628853 3.395089177 -1.575366984 4.121825934 5.060681014
## [721] 4.771359234 4.214638632 3.554705748 6.118828778 5.486861591
## [726] 7.790611910 7.882193408 5.959535638 -5.012127092 1.256878234
## [731] 0.970614757 5.008125872 1.766548313 1.645074028 2.952096383
## [736] -0.655718092 12.951219662 4.065900216 2.452670233 10.953595859
## [741] 9.641336493 2.894473185 3.226197547 1.155357462 0.649989788
## [746] 8.811084400 3.334824277 0.356117715 -1.138516998 4.394900969
## [751] 3.343288841 4.852498956 9.550363093 2.291658959 9.129457655
## [756] -1.317702658 6.765891552 9.650197451 -1.101282567 10.991773769
## [761] 7.826754875 5.087042826 1.050662963 5.822671947 6.292012195
## [766] -2.518951028 4.164256940 -1.224053305 6.955994967 2.298789237
## [771] 5.718643755 7.243494729 8.859188619 0.122431024 9.883996294
## [776] 0.886674497 4.889454779 4.999487915 3.948810785 2.409690830
## [781] 1.057953272 5.495498262 8.017224668 3.861031547 11.240687909
## [786] 6.100356514 2.368753976 0.279802212 5.051307674 7.041432532
## [791] 2.166001745 4.822420064 3.840579714 4.113078733 13.449768232
## [796] 2.934041032 13.162428628 1.645426888 5.230886876 -0.871205289
## [801] 4.778108489 -3.026376338 6.217275528 3.157579704 8.682008520
## [806] 4.411757225 -2.039229657 0.391802938 0.449919380 10.300903650
## [811] 1.819532991 5.286792530 11.335357528 14.089813053 -4.845476727
## [816] 2.557085189 8.459297342 5.939535804 7.527137072 11.242477040
## [821] 0.989890205 9.369594645 7.345734223 8.047307080 17.059172908
## [826] 9.878837811 7.201253419 5.811901736 7.207769182 8.239191552
## [831] 1.074925242 7.294188953 9.006603748 1.024722074 3.220611389
## [836] 10.119492601 -2.439713666 4.655454984 5.920093383 1.563714952
## [841] 4.275179944 6.904698174 4.311741639 4.652600674 2.582039465
## [846] 0.723752404 7.203417334 4.494263313 8.634397198 9.279822425
## [851] 5.160577525 4.878713448 5.230026710 1.149569578 3.695891295
## [856] 5.231218643 4.938796563 8.238646735 3.265597426 -1.853584163
## [861] 9.282827364 5.343791691 4.680209020 9.976851211 9.494380188
## [866] 3.818348649 0.154355261 4.004300465 2.446360872 3.019868205
## [871] 5.109214796 7.723345661 7.471977426 -1.183607753 6.782769000
## [876] 1.655877716 8.041444431 6.387424058 8.499541823 14.675856942
## [881] 10.395210700 4.447490988 7.158008412 8.835359061 1.853395880
## [886] 9.031709482 -0.606756821 2.617722886 10.144704905 2.161739876
## [891] 8.270177352 5.479660524 1.563030202 13.289218757 1.110683475
## [896] 2.563566120 1.882011534 2.933380333 2.850559799 2.396213929
## [901] 4.303748870 13.690721601 -0.683054729 4.738278764 19.408022827
## [906] 3.055395868 4.404999692 3.706874427 4.021972911 5.438293355
## [911] 8.063105208 1.950343969 9.388070884 2.144806265 6.168456173
## [916] 12.661866274 1.725200406 3.330222521 4.184312649 8.573376908
## [921] 4.039502950 4.953202425 13.120163138 2.470905637 7.690100833
## [926] 1.981812962 3.693149332 2.367644287 2.165955266 4.932925980
## [931] 8.098090376 2.965308970 5.392025881 7.106269657 -3.692152502
## [936] -5.243902923 3.291836904 6.565840272 5.413951554 4.158325457
## [941] 12.069404622 7.858483347 3.490915179 7.836663572 6.211309242
## [946] 4.330783729 6.565789619 -3.989350722 6.448832082 4.766521464
## [951] 6.667190664 5.958198133 6.658178292 12.160930460 0.943289927
## [956] 3.443517330 9.718226820 -2.901116862 3.749262028 10.587377072
## [961] 8.812941225 -3.396794109 3.108451398 -2.820347413 8.162148021
## [966] 8.906330122 1.816033888 5.529857506 7.453444380 1.075977052
## [971] 1.677373125 13.219803036 1.550584865 7.118097171 1.825535790
## [976] 1.865518616 2.215736078 10.488214397 4.265683081 12.172528617
## [981] 7.104590277 6.063179835 5.434980037 6.019365398 -1.162694594
## [986] 3.825358549 9.087823473 0.346732202 11.901322130 3.746388918
## [991] 8.194163827 5.664653301 6.273865689 5.859097223 2.428277331
## [996] 10.823179666 10.146051669 -6.143899844 2.103211628 15.679884218
hist(x,col="green", main = "Normal", xlab = "x", ylab = "f(x)")
mean(x)
## [1] 5.112178
var(x)
## [1] 16.07808
sd(x)
## [1] 4.009748
sqrt(var(x))
## [1] 4.009748
# Booleano
sd(x) == sqrt(var(x))
## [1] TRUE
plot(density(x),col = 4,main = "Normal", xlab = "x", ylab = "f(x)")
#Regresión
#MĆnimos cuadrados
# Modelo con ruido
x<-rnorm(1000)
y<-3+4*x+rnorm(1000)
plot(x,y,col = "pink",main = "MĆnimos cuadrados", xlab = "x", ylab = "y")
# Modelo sin ruido
x<-rnorm(1000)
y<-3+4*x
plot(x,y,col = "pink",main = "MĆnimos cuadrados", xlab = "x", ylab = "y")
# Ajustar el modelo
# Función lm que significa linear model
x<-rnorm(1000)
y<-3+4*x+rnorm(1000)
plot(x,y,col = "pink",main = "MĆnimos cuadrados", xlab = "x", ylab = "y")
fit <- lm(y~x)
fit
##
## Call:
## lm(formula = y ~ x)
##
## Coefficients:
## (Intercept) x
## 2.997 4.005
abline(a=fit$coefficients[1],b=fit$coefficients[2],col="red")
# Linea de tendencia
abline(a=fit$coefficients[1],b=fit$coefficients[2],col="red", lwd=4)
abline(a=fit$coefficients[1],b=fit$coefficients[2],col="blue",lwd=2)
abline(h=mean(y), col="green",lwd=3)
abline(v=mean(x), col="green",lwd=3)
# Hipótesis nula es el supuesto
# Se rechaza la hipótesis nula cuando el p valor es menor a la significancia
# 0.05,0.1,0.01
summary(fit)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.5040 -0.6733 0.0087 0.7009 2.9315
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.99747 0.03105 96.55 <2e-16 ***
## x 4.00522 0.03181 125.89 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9817 on 998 degrees of freedom
## Multiple R-squared: 0.9408, Adjusted R-squared: 0.9407
## F-statistic: 1.585e+04 on 1 and 998 DF, p-value: < 2.2e-16
# Cuando no es significativa la pendiente
x<-rnorm(100)
y<-rnorm(100)
plot(x,y,col = "pink",main = "MĆnimos cuadrados", xlab = "x", ylab = "y")
fit <- lm(y~x)
fit
##
## Call:
## lm(formula = y ~ x)
##
## Coefficients:
## (Intercept) x
## 0.06002 0.11476
abline(a=fit$coefficients[1],b=fit$coefficients[2],col="red")
# Linea de tendencia
abline(a=fit$coefficients[1],b=fit$coefficients[2],col="red", lwd=4)
abline(a=fit$coefficients[1],b=fit$coefficients[2],col="blue",lwd=2)
abline(h=mean(y), col="green",lwd=3)
abline(v=mean(x), col="green",lwd=3)
summary(fit)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.19186 -0.57793 -0.04307 0.75648 1.83191
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.06002 0.09159 0.655 0.514
## x 0.11476 0.09417 1.219 0.226
##
## Residual standard error: 0.9159 on 98 degrees of freedom
## Multiple R-squared: 0.01493, Adjusted R-squared: 0.004878
## F-statistic: 1.485 on 1 and 98 DF, p-value: 0.2259
# Muestras
n<-100
M<-1000
# 1000 MUESTRAS DE TAMANO 50
x<-matrix(0,M,n)
for(i in 1:M){
x[i,]<-rnorm(n,10,5)
}
x[3,]
## [1] 3.9547369 2.7875097 2.1830356 15.6413358 7.5002046 6.0780192
## [7] 8.4639051 15.4223452 16.0870471 9.8378319 -1.9946417 11.5757415
## [13] 12.9842590 4.8046507 21.3222847 11.4266954 14.1729545 10.9165296
## [19] 4.9992239 8.7133368 8.4376911 4.0426209 9.7037017 7.7464041
## [25] 12.2799406 10.9881136 10.8803684 18.0400112 10.0007561 1.8813501
## [31] 8.9013298 4.5244028 17.3161877 10.9757577 18.7486787 11.4219377
## [37] 10.9008949 14.4175634 8.5205152 8.7210158 16.1161525 6.8679379
## [43] 15.4017994 14.6376113 14.4220641 9.6195275 12.0377898 12.5670214
## [49] 7.1416386 4.8125833 1.4749882 17.3714727 4.3004531 14.4412115
## [55] 15.2746374 7.8205159 11.4009882 18.9217117 -5.5693181 -2.8597923
## [61] 3.6090296 7.7841812 10.1047158 6.9464918 7.1623729 20.2520082
## [67] 8.0596571 4.1900791 12.4405334 5.5716484 10.6971190 9.9043427
## [73] 18.3577226 6.9665901 8.9145065 8.8728488 9.6254917 10.8248334
## [79] 11.1455817 9.1658912 6.6468923 9.8737295 15.6345347 0.4627328
## [85] 9.1913951 13.9857265 0.9009201 3.9876103 12.0842630 11.5273570
## [91] 12.8901213 13.3229064 12.2397129 9.6374052 14.6790291 1.5060640
## [97] 7.7931549 11.3262602 6.1182429 16.5083841
media <- NULL
for(i in 1:M){
media[i]<-mean(x[i,])
}
5/sqrt(100)
## [1] 0.5
sd(media)
## [1] 0.4911002
# Ley de los grandes nĆŗmeros con la Gamma
x<-list()
for(i in 1:1000){
x[[i]]<-rgamma(i,4)
}
media <- 4
x[[40]]
## [1] 3.3241414 4.8549465 3.2620400 9.1495405 6.7140924 6.1835605 3.5663004
## [8] 1.2892236 7.2314978 7.5830485 2.0570160 5.1286404 5.2307057 2.5999791
## [15] 7.8384555 3.8205046 4.1050864 4.6130689 5.1438422 2.4811335 2.2901102
## [22] 6.3324445 5.3681996 6.3564208 1.1738929 3.7395107 7.1112280 7.3480222
## [29] 3.1954881 2.9860373 2.4475868 0.9959772 3.3571860 5.2694622 6.0395473
## [36] 8.5358811 5.2555421 5.6356117 4.4953366 5.4866053
med<-NULL
for(i in 1:1000){
med[i]<-mean(x[[i]])
}
plot(med,type = "l", main = "Ley de los grandes nĆŗmeros", col = "green")
abline(h=4,col ="red")
x<-list()
for(i in 1:10000){
x[[i]]<-rgamma(i,4)
}
media <- 4
x[[40]]
## [1] 4.4311637 0.9811048 1.3617644 1.8264549 6.7915183 1.4258221 5.0335751
## [8] 5.7618521 5.0464608 6.1702201 2.6983879 2.7529414 4.2730520 2.0103390
## [15] 6.9440839 1.9727104 4.0929725 6.1933153 2.2557611 4.9158585 3.4038416
## [22] 5.2139902 5.0012427 3.2899398 4.4521857 3.5161474 4.9602348 4.5923395
## [29] 1.5342736 1.7384536 4.6023518 4.1206112 4.2402248 2.5245972 2.6043424
## [36] 4.5966140 3.5571406 4.1669671 2.9074034 2.4074323
med<-NULL
for(i in 1:10000){
med[i]<-mean(x[[i]])
}
plot(med,type = "l", main = "Ley de los grandes nĆŗmeros", col = "green")
abline(h=4,col ="red")
x<-list()
for(i in 1:10000){
x[[i]]<-rgamma(i,4)
}
media <- 4
x[[40]]
## [1] 2.1067737 2.8001803 3.3536619 8.0004113 1.3262565 2.5954911
## [7] 6.4855941 5.5069171 2.1043177 4.9054853 3.2731280 6.0263047
## [13] 0.9630069 1.9073788 2.6548557 2.6736820 3.1470006 1.1005844
## [19] 6.0722955 7.7407331 2.4557978 6.5747815 3.2426123 3.6893152
## [25] 3.1935128 1.7240427 2.7627988 10.0432123 4.7276073 2.6964527
## [31] 1.8387272 8.2956991 4.8459083 6.5991379 7.2047243 2.4513162
## [37] 2.8550662 1.9260916 3.2204721 1.4080509
med<-NULL
for(i in 1:10000){
med[i]<-mean(x[[i]])
}
plot(med,type = "l", main = "Ley de los grandes nĆŗmeros", col = "green")
abline(h=4,col ="red")
# Teorema de lĆmite central
# Gamma
M<-1000
n<-100
data<-matrix(0,M,n)
for(i in 1:M){
data[i,]<-rgamma(n,4)
}
med<-NULL
for(i in 1:M){
med[i]<-mean(data[i,])
}
hist(med, col = "green")
# Cauchy
M<-1000
n<-100
data<-matrix(0,M,n)
for(i in 1:M){
data[i,]<-rcauchy(n,4)
}
med<-NULL
for(i in 1:M){
med[i]<-mean(data[i,])
}
hist(med, col = "green")
density(med)
##
## Call:
## density.default(x = med)
##
## Data: med (1000 obs.); Bandwidth 'bw' = 0.3373
##
## x y
## Min. :-187.1 Min. :0.000000
## 1st Qu.: 744.8 1st Qu.:0.000000
## Median :1676.7 Median :0.000000
## Mean :1676.7 Mean :0.002311
## 3rd Qu.:2608.6 3rd Qu.:0.000000
## Max. :3540.5 Max. :0.625499
plot(density(med))
# No tiene momentos pero no contradice el TLC
# POIS
M<-1000
n<-100
data<-matrix(0,M,n)
for(i in 1:M){
data[i,]<-rpois(n,4)
}
med<-NULL
for(i in 1:M){
med[i]<-mean(data[i,])
}
hist(med, col = "green")
density(med)
##
## Call:
## density.default(x = med)
##
## Data: med (1000 obs.); Bandwidth 'bw' = 0.04552
##
## x y
## Min. :3.203 Min. :0.0001096
## 1st Qu.:3.594 1st Qu.:0.0394735
## Median :3.985 Median :0.3251355
## Mean :3.985 Mean :0.6384972
## 3rd Qu.:4.376 3rd Qu.:1.1988249
## Max. :4.767 Max. :1.9357555
plot(density(med))