tag:blogger.com,1999:blog-68410222385567856122024-03-06T01:26:29.601-08:00Estadística ComputacionalRosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.comBlogger48125tag:blogger.com,1999:blog-6841022238556785612.post-26518236933260447402013-04-30T13:57:00.003-07:002014-06-07T02:51:20.043-07:00Knitr en R<!-- saved from url=(0014)about:internet -->
<html>
<head>
<title>A Minimal Example for Markdown</title>
<style type="text/css">
body, td {
font-family: sans-serif;
background-color: white;
font-size: 12px;
margin: 8px;
}
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}
h1 {
font-size:2.2em;
}
h2 {
font-size:1.8em;
}
h3 {
font-size:1.4em;
}
h4 {
font-size:1.0em;
}
h5 {
font-size:0.9em;
}
h6 {
font-size:0.8em;
}
a:visited {
color: rgb(50%, 0%, 50%);
}
pre {
margin-top: 0;
max-width: 95%;
border: 1px solid #ccc;
white-space: pre-wrap;
}
pre code {
display: block; padding: 0.5em;
}
code.r, code.cpp {
background-color: #F8F8F8;
}
table, td, th {
border: none;
}
blockquote {
color:#666666;
margin:0;
padding-left: 1em;
border-left: 0.5em #EEE solid;
}
hr {
height: 0px;
border-bottom: none;
border-top-width: thin;
border-top-style: dotted;
border-top-color: #999999;
}
@media print {
* {
background: transparent !important;
color: black !important;
filter:none !important;
-ms-filter: none !important;
}
body {
font-size:12pt;
max-width:100%;
}
a, a:visited {
text-decoration: underline;
}
hr {
visibility: hidden;
page-break-before: always;
}
pre, blockquote {
padding-right: 1em;
page-break-inside: avoid;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
@page :left {
margin: 15mm 20mm 15mm 10mm;
}
@page :right {
margin: 15mm 10mm 15mm 20mm;
}
p, h2, h3 {
orphans: 3; widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
}
</style>
<!-- Styles for R syntax highlighter </head></html>
<style type="text/css">
pre .operator,
pre .paren {
color: rgb(104, 118, 135)
}
pre .literal {
color: rgb(88, 72, 246)
}
pre .number {
color: rgb(0, 0, 205);
}
pre .comment {
color: rgb(76, 136, 107);
}
pre .keyword {
color: rgb(0, 0, 255);
}
pre .identifier {
color: rgb(0, 0, 0);
}
pre .string {
color: rgb(3, 106, 7);
}
</style>
<!-- R syntax highlighter -->
<script type="text/javascript">
var hljs=new function(){function m(p){return p.replace(/&/gm,"&").replace(/</gm,"<")}function f(r,q,p){return RegExp(q,"m"+(r.cI?"i":"")+(p?"g":""))}function b(r){for(var p=0;p<r.childNodes.length;p++){var q=r.childNodes[p];if(q.nodeName=="CODE"){return q}if(!(q.nodeType==3&&q.nodeValue.match(/\s+/))){break}}}function h(t,s){var p="";for(var r=0;r<t.childNodes.length;r++){if(t.childNodes[r].nodeType==3){var q=t.childNodes[r].nodeValue;if(s){q=q.replace(/\n/g,"")}p+=q}else{if(t.childNodes[r].nodeName=="BR"){p+="\n"}else{p+=h(t.childNodes[r])}}}if(/MSIE [678]/.test(navigator.userAgent)){p=p.replace(/\r/g,"\n")}return p}function a(s){var r=s.className.split(/\s+/);r=r.concat(s.parentNode.className.split(/\s+/));for(var q=0;q<r.length;q++){var p=r[q].replace(/^language-/,"");if(e[p]){return p}}}function c(q){var p=[];(function(s,t){for(var r=0;r<s.childNodes.length;r++){if(s.childNodes[r].nodeType==3){t+=s.childNodes[r].nodeValue.length}else{if(s.childNodes[r].nodeName=="BR"){t+=1}else{if(s.childNodes[r].nodeType==1){p.push({event:"start",offset:t,node:s.childNodes[r]});t=arguments.callee(s.childNodes[r],t);p.push({event:"stop",offset:t,node:s.childNodes[r]})}}}}return t})(q,0);return p}function k(y,w,x){var q=0;var z="";var s=[];function u(){if(y.length&&w.length){if(y[0].offset!=w[0].offset){return(y[0].offset<w[0].offset)?y:w}else{return w[0].event=="start"?y:w}}else{return y.length?y:w}}function t(D){var A="<"+D.nodeName.toLowerCase();for(var B=0;B<D.attributes.length;B++){var C=D.attributes[B];A+=" "+C.nodeName.toLowerCase();if(C.value!==undefined&&C.value!==false&&C.value!==null){A+='="'+m(C.value)+'"'}}return A+">"}while(y.length||w.length){var v=u().splice(0,1)[0];z+=m(x.substr(q,v.offset-q));q=v.offset;if(v.event=="start"){z+=t(v.node);s.push(v.node)}else{if(v.event=="stop"){var p,r=s.length;do{r--;p=s[r];z+=("</"+p.nodeName.toLowerCase()+">")}while(p!=v.node);s.splice(r,1);while(r<s.length){z+=t(s[r]);r++}}}}return z+m(x.substr(q))}function j(){function q(x,y,v){if(x.compiled){return}var u;var s=[];if(x.k){x.lR=f(y,x.l||hljs.IR,true);for(var w in x.k){if(!x.k.hasOwnProperty(w)){continue}if(x.k[w] instanceof Object){u=x.k[w]}else{u=x.k;w="keyword"}for(var r in u){if(!u.hasOwnProperty(r)){continue}x.k[r]=[w,u[r]];s.push(r)}}}if(!v){if(x.bWK){x.b="\\b("+s.join("|")+")\\s"}x.bR=f(y,x.b?x.b:"\\B|\\b");if(!x.e&&!x.eW){x.e="\\B|\\b"}if(x.e){x.eR=f(y,x.e)}}if(x.i){x.iR=f(y,x.i)}if(x.r===undefined){x.r=1}if(!x.c){x.c=[]}x.compiled=true;for(var t=0;t<x.c.length;t++){if(x.c[t]=="self"){x.c[t]=x}q(x.c[t],y,false)}if(x.starts){q(x.starts,y,false)}}for(var p in e){if(!e.hasOwnProperty(p)){continue}q(e[p].dM,e[p],true)}}function d(B,C){if(!j.called){j();j.called=true}function q(r,M){for(var L=0;L<M.c.length;L++){if((M.c[L].bR.exec(r)||[null])[0]==r){return M.c[L]}}}function v(L,r){if(D[L].e&&D[L].eR.test(r)){return 1}if(D[L].eW){var M=v(L-1,r);return M?M+1:0}return 0}function w(r,L){return L.i&&L.iR.test(r)}function K(N,O){var M=[];for(var L=0;L<N.c.length;L++){M.push(N.c[L].b)}var r=D.length-1;do{if(D[r].e){M.push(D[r].e)}r--}while(D[r+1].eW);if(N.i){M.push(N.i)}return f(O,M.join("|"),true)}function p(M,L){var N=D[D.length-1];if(!N.t){N.t=K(N,E)}N.t.lastIndex=L;var r=N.t.exec(M);return r?[M.substr(L,r.index-L),r[0],false]:[M.substr(L),"",true]}function z(N,r){var L=E.cI?r[0].toLowerCase():r[0];var M=N.k[L];if(M&&M instanceof Array){return M}return false}function F(L,P){L=m(L);if(!P.k){return L}var r="";var O=0;P.lR.lastIndex=0;var M=P.lR.exec(L);while(M){r+=L.substr(O,M.index-O);var N=z(P,M);if(N){x+=N[1];r+='<span class="'+N[0]+'">'+M[0]+"</span>"}else{r+=M[0]}O=P.lR.lastIndex;M=P.lR.exec(L)}return r+L.substr(O,L.length-O)}function J(L,M){if(M.sL&&e[M.sL]){var r=d(M.sL,L);x+=r.keyword_count;return r.value}else{return F(L,M)}}function I(M,r){var L=M.cN?'<span class="'+M.cN+'">':"";if(M.rB){y+=L;M.buffer=""}else{if(M.eB){y+=m(r)+L;M.buffer=""}else{y+=L;M.buffer=r}}D.push(M);A+=M.r}function G(N,M,Q){var R=D[D.length-1];if(Q){y+=J(R.buffer+N,R);return false}var P=q(M,R);if(P){y+=J(R.buffer+N,R);I(P,M);return P.rB}var L=v(D.length-1,M);if(L){var O=R.cN?"</span>":"";if(R.rE){y+=J(R.buffer+N,R)+O}else{if(R.eE){y+=J(R.buffer+N,R)+O+m(M)}else{y+=J(R.buffer+N+M,R)+O}}while(L>1){O=D[D.length-2].cN?"</span>":"";y+=O;L--;D.length--}var r=D[D.length-1];D.length--;D[D.length-1].buffer="";if(r.starts){I(r.starts,"")}return R.rE}if(w(M,R)){throw"Illegal"}}var E=e[B];var D=[E.dM];var A=0;var x=0;var y="";try{var s,u=0;E.dM.buffer="";do{s=p(C,u);var t=G(s[0],s[1],s[2]);u+=s[0].length;if(!t){u+=s[1].length}}while(!s[2]);if(D.length>1){throw"Illegal"}return{r:A,keyword_count:x,value:y}}catch(H){if(H=="Illegal"){return{r:0,keyword_count:0,value:m(C)}}else{throw H}}}function g(t){var p={keyword_count:0,r:0,value:m(t)};var r=p;for(var q in e){if(!e.hasOwnProperty(q)){continue}var s=d(q,t);s.language=q;if(s.keyword_count+s.r>r.keyword_count+r.r){r=s}if(s.keyword_count+s.r>p.keyword_count+p.r){r=p;p=s}}if(r.language){p.second_best=r}return p}function i(r,q,p){if(q){r=r.replace(/^((<[^>]+>|\t)+)/gm,function(t,w,v,u){return w.replace(/\t/g,q)})}if(p){r=r.replace(/\n/g,"<br>")}return r}function n(t,w,r){var x=h(t,r);var v=a(t);var y,s;if(v){y=d(v,x)}else{return}var q=c(t);if(q.length){s=document.createElement("pre");s.innerHTML=y.value;y.value=k(q,c(s),x)}y.value=i(y.value,w,r);var u=t.className;if(!u.match("(\\s|^)(language-)?"+v+"(\\s|$)")){u=u?(u+" "+v):v}if(/MSIE [678]/.test(navigator.userAgent)&&t.tagName=="CODE"&&t.parentNode.tagName=="PRE"){s=t.parentNode;var p=document.createElement("div");p.innerHTML="<pre><code>"+y.value+"</code></pre>
";t=p.firstChild.firstChild;p.firstChild.cN=s.cN;s.parentNode.replaceChild(p.firstChild,s)}else{t.innerHTML=y.value}t.className=u;t.result={language:v,kw:y.keyword_count,re:y.r};if(y.second_best){t.second_best={language:y.second_best.language,kw:y.second_best.keyword_count,re:y.second_best.r}}}function o(){if(o.called){return}o.called=true;var r=document.getElementsByTagName("pre");for(var p=0;p<r.length;p++){var q=b(r[p]);if(q){n(q,hljs.tabReplace)}}}function l(){if(window.addEventListener){window.addEventListener("DOMContentLoaded",o,false);window.addEventListener("load",o,false)}else{if(window.attachEvent){window.attachEvent("onload",o)}else{window.onload=o}}}var e={};this.LANGUAGES=e;this.highlight=d;this.highlightAuto=g;this.fixMarkup=i;this.highlightBlock=n;this.initHighlighting=o;this.initHighlightingOnLoad=l;this.IR="[a-zA-Z][a-zA-Z0-9_]*";this.UIR="[a-zA-Z_][a-zA-Z0-9_]*";this.NR="\\b\\d+(\\.\\d+)?";this.CNR="\\b(0[xX][a-fA-F0-9]+|(\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)";this.BNR="\\b(0b[01]+)";this.RSR="!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|\\.|-|-=|/|/=|:|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~";this.ER="(?![\\s\\S])";this.BE={b:"\\\\.",r:0};this.ASM={cN:"string",b:"'",e:"'",i:"\\n",c:[this.BE],r:0};this.QSM={cN:"string",b:'"',e:'"',i:"\\n",c:[this.BE],r:0};this.CLCM={cN:"comment",b:"//",e:"$"};this.CBLCLM={cN:"comment",b:"/\\*",e:"\\*/"};this.HCM={cN:"comment",b:"#",e:"$"};this.NM={cN:"number",b:this.NR,r:0};this.CNM={cN:"number",b:this.CNR,r:0};this.BNM={cN:"number",b:this.BNR,r:0};this.inherit=function(r,s){var p={};for(var q in r){p[q]=r[q]}if(s){for(var q in s){p[q]=s[q]}}return p}}();hljs.LANGUAGES.cpp=function(){var a={keyword:{"false":1,"int":1,"float":1,"while":1,"private":1,"char":1,"catch":1,"export":1,virtual:1,operator:2,sizeof:2,dynamic_cast:2,typedef:2,const_cast:2,"const":1,struct:1,"for":1,static_cast:2,union:1,namespace:1,unsigned:1,"long":1,"throw":1,"volatile":2,"static":1,"protected":1,bool:1,template:1,mutable:1,"if":1,"public":1,friend:2,"do":1,"return":1,"goto":1,auto:1,"void":2,"enum":1,"else":1,"break":1,"new":1,extern:1,using:1,"true":1,"class":1,asm:1,"case":1,typeid:1,"short":1,reinterpret_cast:2,"default":1,"double":1,register:1,explicit:1,signed:1,typename:1,"try":1,"this":1,"switch":1,"continue":1,wchar_t:1,inline:1,"delete":1,alignof:1,char16_t:1,char32_t:1,constexpr:1,decltype:1,noexcept:1,nullptr:1,static_assert:1,thread_local:1,restrict:1,_Bool:1,complex:1},built_in:{std:1,string:1,cin:1,cout:1,cerr:1,clog:1,stringstream:1,istringstream:1,ostringstream:1,auto_ptr:1,deque:1,list:1,queue:1,stack:1,vector:1,map:1,set:1,bitset:1,multiset:1,multimap:1,unordered_set:1,unordered_map:1,unordered_multiset:1,unordered_multimap:1,array:1,shared_ptr:1}};return{dM:{k:a,i:"</",c:[hljs.CLCM,hljs.CBLCLM,hljs.QSM,{cN:"string",b:"'\\\\?.",e:"'",i:"."},{cN:"number",b:"\\b(\\d+(\\.\\d*)?|\\.\\d+)(u|U|l|L|ul|UL|f|F)"},hljs.CNM,{cN:"preprocessor",b:"#",e:"$"},{cN:"stl_container",b:"\\b(deque|list|queue|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<",e:">",k:a,r:10,c:["self"]}]}}}();hljs.LANGUAGES.r={dM:{c:[hljs.HCM,{cN:"number",b:"\\b0[xX][0-9a-fA-F]+[Li]?\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"number",b:"\\b\\d+(?:[eE][+\\-]?\\d*)?L\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"number",b:"\\b\\d+\\.(?!\\d)(?:i\\b)?",e:hljs.IMMEDIATE_RE,r:1},{cN:"number",b:"\\b\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d*)?i?\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"number",b:"\\.\\d+(?:[eE][+\\-]?\\d*)?i?\\b",e:hljs.IMMEDIATE_RE,r:1},{cN:"keyword",b:"(?:tryCatch|library|setGeneric|setGroupGeneric)\\b",e:hljs.IMMEDIATE_RE,r:10},{cN:"keyword",b:"\\.\\.\\.",e:hljs.IMMEDIATE_RE,r:10},{cN:"keyword",b:"\\.\\.\\d+(?![\\w.])",e:hljs.IMMEDIATE_RE,r:10},{cN:"keyword",b:"\\b(?:function)",e:hljs.IMMEDIATE_RE,r:2},{cN:"keyword",b:"(?:if|in|break|next|repeat|else|for|return|switch|while|try|stop|warning|require|attach|detach|source|setMethod|setClass)\\b",e:hljs.IMMEDIATE_RE,r:1},{cN:"literal",b:"(?:NA|NA_integer_|NA_real_|NA_character_|NA_complex_)\\b",e:hljs.IMMEDIATE_RE,r:10},{cN:"literal",b:"(?:NULL|TRUE|FALSE|T|F|Inf|NaN)\\b",e:hljs.IMMEDIATE_RE,r:1},{cN:"identifier",b:"[a-zA-Z.][a-zA-Z0-9._]*\\b",e:hljs.IMMEDIATE_RE,r:0},{cN:"operator",b:"<\\-(?!\\s*\\d)",e:hljs.IMMEDIATE_RE,r:2},{cN:"operator",b:"\\->|<\\-",e:hljs.IMMEDIATE_RE,r:1},{cN:"operator",b:"%%|~",e:hljs.IMMEDIATE_RE},{cN:"operator",b:">=|<=|==|!=|\\|\\||&&|=|\\+|\\-|\\*|/|\\^|>|<|!|&|\\||\\$|:",e:hljs.IMMEDIATE_RE,r:0},{cN:"operator",b:"%",e:"%",i:"\\n",r:1},{cN:"identifier",b:"`",e:"`",r:0},{cN:"string",b:'"',e:'"',c:[hljs.BE],r:0},{cN:"string",b:"'",e:"'",c:[hljs.BE],r:0},{cN:"paren",b:"[[({\\])}]",e:hljs.IMMEDIATE_RE,r:0}]}};
hljs.initHighlightingOnLoad();
</script>
<!-- MathJax scripts -->
<script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript">
</script>
<br />
<h1>
<span class="GingerNoCheckStart"></span>Un pequeño ejemplo de Knitr en R</h1>
<h1>
</h1>
<h1>
A Minimal Example for Markdown</h1>
This is a minimal example of using <strong>knitr</strong> to produce an <em>HTML</em> page from <em>Markdown</em>.<br />
<h2>
R code chunks</h2>
<pre><code class="r"># set global chunk options: images will be 7x5 inches
opts_chunk$set(fig.width = 7, fig.height = 5)
</code></pre>
Now we write some code chunks in this markdown file:<br />
<pre><code class="r">x <- 1 + 1 # a simple calculator
set.seed(123)
rnorm(5) # boring random numbers
</code></pre>
<pre><code>## [1] -0.56048 -0.23018 1.55871 0.07051 0.12929
</code></pre>
We can also produce plots:<br />
<pre><code class="r">par(mar = c(4, 4, 0.1, 0.1))
with(mtcars, {
plot(mpg ~ hp, pch = 20, col = "darkgray")
lines(lowess(hp, mpg))
})
</code></pre>
<img alt="plot of chunk graphics" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfgAAAFoCAYAAAC7Tuk8AAAEJGlDQ1BJQ0MgUHJvZmlsZQAAOBGFVd9v21QUPolvUqQWPyBYR4eKxa9VU1u5GxqtxgZJk6XtShal6dgqJOQ6N4mpGwfb6baqT3uBNwb8AUDZAw9IPCENBmJ72fbAtElThyqqSUh76MQPISbtBVXhu3ZiJ1PEXPX6yznfOec7517bRD1fabWaGVWIlquunc8klZOnFpSeTYrSs9RLA9Sr6U4tkcvNEi7BFffO6+EdigjL7ZHu/k72I796i9zRiSJPwG4VHX0Z+AxRzNRrtksUvwf7+Gm3BtzzHPDTNgQCqwKXfZwSeNHHJz1OIT8JjtAq6xWtCLwGPLzYZi+3YV8DGMiT4VVuG7oiZpGzrZJhcs/hL49xtzH/Dy6bdfTsXYNY+5yluWO4D4neK/ZUvok/17X0HPBLsF+vuUlhfwX4j/rSfAJ4H1H0qZJ9dN7nR19frRTeBt4Fe9FwpwtN+2p1MXscGLHR9SXrmMgjONd1ZxKzpBeA71b4tNhj6JGoyFNp4GHgwUp9qplfmnFW5oTdy7NamcwCI49kv6fN5IAHgD+0rbyoBc3SOjczohbyS1drbq6pQdqumllRC/0ymTtej8gpbbuVwpQfyw66dqEZyxZKxtHpJn+tZnpnEdrYBbueF9qQn93S7HQGGHnYP7w6L+YGHNtd1FJitqPAR+hERCNOFi1i1alKO6RQnjKUxL1GNjwlMsiEhcPLYTEiT9ISbN15OY/jx4SMshe9LaJRpTvHr3C/ybFYP1PZAfwfYrPsMBtnE6SwN9ib7AhLwTrBDgUKcm06FSrTfSj187xPdVQWOk5Q8vxAfSiIUc7Z7xr6zY/+hpqwSyv0I0/QMTRb7RMgBxNodTfSPqdraz/sDjzKBrv4zu2+a2t0/HHzjd2Lbcc2sG7GtsL42K+xLfxtUgI7YHqKlqHK8HbCCXgjHT1cAdMlDetv4FnQ2lLasaOl6vmB0CMmwT/IPszSueHQqv6i/qluqF+oF9TfO2qEGTumJH0qfSv9KH0nfS/9TIp0Wboi/SRdlb6RLgU5u++9nyXYe69fYRPdil1o1WufNSdTTsp75BfllPy8/LI8G7AUuV8ek6fkvfDsCfbNDP0dvRh0CrNqTbV7LfEEGDQPJQadBtfGVMWEq3QWWdufk6ZSNsjG2PQjp3ZcnOWWing6noonSInvi0/Ex+IzAreevPhe+CawpgP1/pMTMDo64G0sTCXIM+KdOnFWRfQKdJvQzV1+Bt8OokmrdtY2yhVX2a+qrykJfMq4Ml3VR4cVzTQVz+UoNne4vcKLoyS+gyKO6EHe+75Fdt0Mbe5bRIf/wjvrVmhbqBN97RD1vxrahvBOfOYzoosH9bq94uejSOQGkVM6sN/7HelL4t10t9F4gPdVzydEOx83Gv+uNxo7XyL/FtFl8z9ZAHF4bBsrEwAAPLVJREFUeAHtnQecXVWdxzOZSSMFaSahd4FQbAgWFA0qIJZlKa6N3UWTITF0xUoYRBZUDBBMA1FExQUUFFZUAiKLBQGFuAREBaQllARIIH0m+/0P78ZxeC+Z8t679937PZ/Pb86t557/99y5v3dubVq7du0AkwQkIAEJSEAC+SLQkq9wykfT1NR0BXNGo2Xll3CqBCQgAQlIIDcENqfzvn8hDJ4mG4SOJOBFuWk+A5GABCQgAQmUIUCn9vqYPLDMPCdJQAISkIAEJNDgBDT4Bm9Aqy8BCUhAAhIoR0CDL0fFaRKQgAQkIIEGJ6DBN3gDWn0JSEACEpBAOQIafDkqTpOABCQgAQk0OAENvsEb0OpLQAISkIAEyhHQ4MtRcZoEJCABCUigwQkU5Tn4TDTTBRdcMHro0KGnUpmt1qxZM2Xy5Mk+l5+JlrESEpCABPJHwB58ndp02rRpw0gL2dzJ6OiWlpb5c+bM2bhOm3czEpCABCRQMAIafJ0afPjw4XuzqSd4w9DAklZ1dHTsWafNuxkJSEACEigYAQ2+fg3+CJuKV+YmaWsM/m/JiLkEJCABCUigmgQ0+GrSXE9ZEyZMWMDs8bwP/0HyG9DekyZNilP2JglIQAISkEDVCTQV4XOxnBK/GnIT/dhM1fcfC5SABCQggYwRiI/N4HeH2YPPWMNYHQlIQAISkEA1CGjw1aBoGRKQgAQkIIGMEdDgM9YgVkcCEpCABCRQDQIafDUoWoYEJCABCUggYwQ0+Iw1iNWRgAQkIAEJVIOABl8NipYhAQlIQAISyBgBDT5jDWJ1JCABCUhAAtUgoMFXg6JlSEACEpCABDJGQIPPWINYHQlIQAISkEA1CGjw1aBoGRKQgAQkIIGMEdDgM9YgVkcCEpCABCRQDQIafDUoWoYEJCABCUggYwRaMlaf3Fenra1t8JgxYy7gYwAHEey8uXPnHnXllVe25z5wA5SABCQggboSsAdfV9wDBmDu12Dux7LZnfnaz3sOOuig0+pcBTcnAQlIQAIFIKDB17+RR7DJQbFZjH4IJv/W+lfBLUpAAhKQQN4JaPD1b+ErMPU1pc2uYvjy+lfBLUpAAhKQQN4JaPB1buGJEyfOYpMT0GzM/ajW1tbv1bkKbk4CEpCABApAoAmTyX2YnAq/miAnEuui3AdrgBKQgAQkUGgCeN71+N1h9uALvRsYvAQkIAEJ5JWABp/XljUuCUhAAhIoNAENvtDNb/ASkIAEJJBXAhp8XlvWuCQgAQlIoNAENPhCN7/BS0ACEpBAXglo8HltWeOSgAQkIIFCE9DgC938Bi8BCUhAAnkloMHntWWNSwISkIAECk1Agy908xu8BCQgAQnklYAGn9eWNS4JSEACEig0AQ2+0M1v8BKQgAQkkFcCGnxeW9a4JCABCUig0AQ0+EI3v8FLQAISkEBeCWjweW1Z45KABCQggUIT0OAL3fwGLwEJSEACeSWgwee1ZY1LAhKQgAQKTUCDL3TzG7wEJCABCeSVgAbfg5adM2fOtrNnz96lB4u6SB8JBN/p06dv3cfVXU0CEpCABLoRyJTBNzU1bYJautUx1VGMp5UK/Jp63YXRz061Mjnd+KxZs74N37lDhgx5FN4fy2mYhiUBCUigrgRSN3gO7EegO9FKIl+MVjD8MJqBRtWVRreNRc+dOnzjqaee2nrx4sUjmf2BmTNnHtBtMUf7QQCeBw0cOPBoitg2ioH3l2bMmDGmH0W6qgQkIAEJQCBVg+dg/k7qMA19AY1FzSiMdDxaha5FqaXVq1cPZeOPjRo1asBVV1014MEHHxzW3Nw8LLUK5XDD8BxCWC8koa1du3ZTFNNMEpCABCTQDwKpGjz1PhidzQH9Z2gx6kDL0d+Yfgrajh8Bm5CnkiZPnvwAdbmRU8fLDznkkEXXXXdd+8SJE3+fSmVyutEFCxbcCONFhPc8ehp9B+5/z2m4hiUBCUigbgTSNvi7iXQ8Jl6uHnszbwu0tG40ymwIQ/84BnTotttu+6H58+efyiKfK7OYk/pIYOrUqasWLly4R0dHx9FwPgLek/tYlKtJQAISkEAXAk0cVLuM1ncQYx/OFi9Fb0R3oLgGH6dnt0Rh8CdQvyvI+5XYztUUMJGyoqfY51T6ITKdAm6hrKv6XJArSkACEpCABGpEAK+6Ho86LNU71qnAi8R3NJXZh3w3tB1ajh5Hceq2x713yjibdSrF82bm9TtRnw62cyYFzSa/m/G/9LtQC5CABCQgAQnUgECqPfhK8WCem2Cez1aaX24667yO6eVO9cfiP0e7UGa/evBRUCS2dSDZx0OUuYLcJAEJSEACEsgEATwq/R58kKAi7yU7At2Cwoh/gcYxPW60OxwDnUe+wcRyd1VaiLLWVJrXl+ls6xbK3Jd1T0Tn9KUM15GABCQgAQnUkkClHm8tt7mubEzyrYzEY3J/Raegueg7KJ6DnolOR1lNF1KxPYjhHVmtoPWSgAQkIIHiEkjV4MF+GGqjR/wl8iNRPBL3NcafROcxvCsGmurLbqhD2UT9VjLjTHQ8dfTFLGUpOVECEpCABNIikLbB/5nA4275ARjmfWSTyDtiHNPclCxefNPjG+1ivXom6hpnHi5Hp1PfqKtJAhKQgAQkkAkCaRv8j6Dwdszx20EDw7wmcsbfRhbmfzHT0nuOLyqzgUT1fsgiS9B/bGBRZ0tAAhKQgATqRqDSY2V1qQDm+CxmHjerdf9S2yNM25/5caNdI6T/opLfJJY7qXO8vMckAQlIQAISSJVA2j346LVHeqArBcYfQo1i7hFDvGY1TP7zmHwm7xnoytdhCUhAAhLIP4HUDT4viDH5eEzvRvTZvMRkHBKQgAQk0LgENPjqtt03KW5TevH/Ut1iLU0CEpCABCTQOwIafO94rXdpevHtLHAG+ndMfsf1LuxMCUhAAhKQQA0JaPBVhovJL6DI6WgqJj+kysVbnAQkIAEJSKBHBDT4HmHq3UKYfLyR7370yd6t6dISkIAEJCCB6hDQ4KvDsVwp5zPxtfTi43W8JglIQAISkEBdCWjwNcJNLz4+e3sGOhGT36JGm7FYCUhAAhKQQFkCGnxZLNWZiMnH9+L/G8WrbGVdHayWIgEJSEACPSCg6fQAUn8WweTD4KM3f0x/ynFdCUhAAhKQQG8IaPC9odX3ZeMtd4fRi+/8sE7fi3FNCUhAAhKQQM8IaPA949SvpejFP0sB56AvYPIj+1WYK0tAAhKQgAR6QECD7wGkaiyCyd9BOTej06pRnmVIQAISkIAE1kdAg18fnerPm0ORW9CLf2/1i7ZECUhAAhKQwD8IaPD/YFHzIXrxHWzkTHQsJr9DzTfoBiQgAQlIoLAENPg6Nz0m/zibjFfZxqNzg+u8eTcnAQlIQAIFIaDBp9DQmPxNbPY+dFIKm3eTEpCABCRQAAIafHqNfAGbHkcvfnx6VXDLEpCABCSQVwIafEotSy9+JZs+A03B5LdKqRpuVgISkIAEckpAg0+xYTH5B9n8pSg+LduSYlXctAQkIAEJ5IyABp9yg2LyP6EK8Q351pSr4uYlIAEJSCBHBDT4bDTmV6nGW+jFvzEb1bEWEpCABCTQ6AQ8LZxCC86cOXPvgQMH7t7e3v6bSZMmPUov/gXM/Uyqchb5BMafSaFablICEpCABHJEwB58nRsTcz+gubn5N2x2TktLyyNz5sx5bVQBU59PdjWK99XbLgHFJAEJSEACfSagkfQZXd9WxNwvYs3hmPioKAFjPzUpieHvM9yOPpZMM5eABCQgAQn0hYAG3xdq/VvnHow8Xlkb5r6GrPvX5c5i2nv5AbBPLGOSgAQkIAEJ9IWABt8Xav1YZ+XKladg3lHC38nvXrp06Qe7Fofpx6dlz0Zxqn7jrvMcloAEJCABCfSUgAbfU1JVWm7KlClPU9TQNWvWHED+plNPPfXF7kVj8ncx7Ub0me7zHJeABCQgAQn0hEATZtKT5Rp6GXrCTxHA7sS6qFECoc7N1DU+SnMz9Y6b70wSkIAEJCCBDRLAP67HNw6zB79BVOksQOPEzXZt6CM01q7p1MKtSkACEpBAoxLQ4DPccpj8k1Tv6yheZTssw1W1ahKQgAQkkDECGnzGGqR7dTD5W5l2Jzql+zzHJSABCUhAApUIaPCVyGRr+jeozo704g/JVrWsjQQkIAEJZJWABp/VlulSL3rxqxg9A7Vi8tt2meWgBCQgAQlIoCwBDb4sluxNxOQfoVazUVyPH5y9GlojCUhAAhLIEgENPkutsYG6YPI/ZZGH0aQNLOpsCUhAAhIoOAENvvF2gK9R5X3pxb+18apujSUgAQlIoF4ENPh6ka7SdujFL6eoM9FJmPzoKhVrMRKQgAQkkDMCfg++ARsUk/8z5h5fnjud/HjG46U4fU58svY4Vp5CObt0dHS87rjjjpvX58JcUQISkIAEMkHAHnwmmqH3lcCMr2KtJeg/e7/2P9aYPXt2nOqfgXbnx0ILn7O9dvr06Z2fsv3HUg5JQAISkECjEdDgG63F/rm+5zD6Loz5df88uVdjm7P0c8ka/HAYPmjQoO6fsE1mm0tAAhKQQIMQ0OAbpKHKVRMzfp7p8f34z2Hym5RbZkPTWO9WlnmKslaTv8j4fa2trY9vaD3nS0ACEpBAtglo8Nlunw3WDmO+h4WuQ5/f4MJlFpgwYcIzS5YseS3lfJbZk+fOnTu+zGJOkoAEJCCBBiPg52IbrMHKVZded/xQm4Zux6jj5juTBCQgAQkUlACe4Odi89L2mHoHscSp+iNo2D3yEpdxSEACEpBA3wl4ir7v7DK1Jib/NBX6KopH50ZkqnJWRgISkIAE6k4gcwaPOQ1Fm9adRA42iMn/ljBuQ5/KQTiGIAEJSEAC/SCQqsFj5MegT0b9yQeiCxl8Aj3O8A1o95hn6hWBWSw9Fnbv69VaLiwBCUhAArkikKrBQ3Is2qZENL51vh86AMX0X6J4mYupFwToxa9h8TZ0LCa/Qy9WdVEJSEACEsgRgbQNvivK9zNyIQZ1L3oOfYXxwZhUvIjF1AsCsIvn2KejNvgN6cWqLioBCUhAAjkhkAWD3wUTiju/49rxlglXpm3GcPRGFyXTzHtOAJOfy9Lz0fE9X8slJSABCUggLwRaUg4kbgp7LboWxal6fL3pfPJ49Wp8+/xsjGot+QYT68U71Sv9YBm0wQLyuUCwnAObd4Dx5nyGaFQSkIAEJFCOQKoGj+n8ikqFwtnj0a5xTFvNcNxotz96DPU0hcE3V1i4kAYPyxWwPAMm08jvZzy4miQgAQlIoAAEUn+THcbzRTi3ohXoNEzo6uDO9DDrNYw3xXh/EmU9xfq7U1YhT/cT/weIP25inAyDuOxhkoAEJCCBnBLgmJ/+m+yoxBvhezAK8/kCOo9pE8hNVSSAqcclkPiRI9sqcrUoCUhAAlkmkOopesDEs9oXYUDzyOdh7reR30a+mPwaZKoegXMp6puwvQvet1evWEuSgAQkIIEsEqh0U1q96hpfQotefGfCeB5l4DAUj3i9o3Oif6pCALYvUNCZ6DRM3kcPq0LVQiQgAQlkl0Cq1+AxmpGgiV77MgxondEzPW6wux5txvSGvAbf1tY2cMyYMd8ilnhxz1YrV67cesqUKfG++FQT9fkwFdgXnQTbHj2hkGqF3bgEJCABCfSKAMf59K/B4y9L0T7U/MSutWfa7xjfC8XLbhoyYe5tQP4gld8BDSZ9MyOBfJ96tKOPZaQ+VkMCEpCABGpAIO1T9J0hYegvuybMtAXotBrEXJciMfdhbGhwsjHGX50Mp5nDNHrtX0bvo057p1kXty0BCUhAArUjkAmDr1146ZW8Zs2a77L1J1A8lhbXv6ehTCQ8Pm5iPAd9AZMflYlKWQkJSEACEqgqAQ2+qjj/UdikSZPuXrVqVVzrntze3n7UhAkTMmPwUUtM/g6yeJ3tZ2PcJAEJSEAC+SKQ6k129UJJL7XQL7qpxBku8TKhi9BcDP+HlZZzugQkIAEJNA4Bju3p32TXOLjyWVNMPW62a0MfZYfYJZ9RGpUEJCCBYhLwFH0x231d1Jj8Qkbi8sFUTD5uDDRJQAISkEAOCGjwOWjE/oaAyf+KMu5EJ/W3LNeXgAQkIIFsENDgs9EOWajFDCqxM734Q7NQGesgAQlIQAL9I6DB949fbtamF7+KYKaiiZj89uQmCUhAAhJoYAIafAM3XrWrjsnHtwDirvo2TH5Itcu3PAlIQAISqB8BDb5+rBtiS5j8jVT0XnR8Q1TYSkpAAhKQQFkCGnxZLIWfeCEE9qIX/87CkxCABCQggQYloME3aMPVstr04ldQflyP/yQmv00tt2XZEpCABCRQGwIafG24NnypmPxDBDEHnYHJr/toTsMHZgASkIAECkJAgy9IQ/clTEz+f1gvjH5yX9Z3HQlIQAISSI+ABp8e+0bZ8nlU9PX04g9slApbTwlIQAISGDCgpRwEDua/YfrYbvM6GH8O3YBOp3cX46acE6Cdl7M/xPX488gfYDw+gWuSgAQkIIGMEyj7NTkO5F+l3q9CF6P4Etsh6EPok+gE9FsO9GeRN0QiHr8m18OWmjNnzjTa9r0w26mjo+MnAwcO3ItVd2hrazv2iSee2J/hScyPb9ybJCABCUgggwQ4fnd+Ta6SwccLT/bkQP58UndWuI7hr6DH0GXMe2syL+u5Bt+zFpo9e/bhsIofdZuW1oizNJ2XcWjvm1pbW/+X8UUMx8twTBKQgAQkkEECicFXugYfj0ltldSbhZsYjs+JxutM44tj64yfYVNOCGDcmxHKiC7hRLt3JnaBfRiIMztvZviAl6b6VwISkIAEskqgksGfS4Vv4UD+PRS9tfvRw+hP6Bp0FTLljACn468npMEY/UrypSgMfiXjS9DX0DLGz0Ans1+MITdJQAISkEBGCZQ9RR915QD+erJ3oXgn+Y85uP+BacMZ3oLhh8kbJlFvr8H3sLVmzJgxAqM/gjZevGLFituGDRv2PlZdwOn5nydFwPNfGT4ITWE5r8cnYMwlIAEJZIAAx+jK1+CT+rHQJgyP4iD+92RaI+YafPVbDaZxk+UC9o1vVL90S5SABCQggb4SSAy+7Cl6Zu6AbqfwZ9B8hp9BR/d1Y66XSwLnENUB7BdvzmV0BiUBCUigwQmUNXhi+hb6EXolegWKU7LxHPQe5CYJDKDn/gIY2tAp7BejRSIBCUhAAtkiUMngd6CaX+EgHo9ErUa/Yvwy1DCPxmULcz5rw35xH5H9AE3F5JvzGaVRSUACEmhMApUM/m7C+U8O2i0RVqmHdiiD8YY7kwTWEcDkr2QkHpv8+LqJDkhAAhKQQOoEKhn8YGp2CVqEuf8f+QIUb7a7kvH7UbwMxSSBhMDZDIxnv9gvmWAuAQlIQALpEij7mBwH6nipSZh8pfQ8PbcHKs3M2nTi8TG5GjcKjMexiS+hiewbT9d4cxYvAQlIQAIVCHA87nxMrmwPngP0Paz3FxTXVeM0faK/Me+ORjJ36m6qAwH2iXvZTLwA6XR2rrL7VR2q4SYkIAEJSKBEoPMae3caHKDPZNpk9CRq7zL/FIZ/0WXcQQmsI4DJX8G+8xom/CeKSzwmCUhAAhJIiUBZg6cux6JdOWAvSqlebrZxCXyZql+C0d/D/nNH44ZhzSUgAQk0NoFKBh831cVHRzT4xm7fqtV+1qxZ7+YVtu/HtO+bOHHi9EoFM//50hmgNvJP+COxEimnS0ACEqgtgUo32e3PZq9FN6B4m12SLueAPS8ZaZQco/Emu340Fp+RPRiG8SGauCdjdXt7++ePO+64r66vSJb/EPNfz/5ycrLcUUcd1Tx+/Pj3M28npsflnzv5sTA/mW8uAQlIQAL9JxDHa46xh1W6GerrbCJumnoCreiirtfjmWwqCIGPEWeYe6RBzc3NH3xpsPJfdq7vM7edHS0u93QmzP1mxr/NyLnk30L3cmbgHS/N9a8EJCABCVSTQKVT9PHt9604SMf3300FJ8B+8BvM+F/AMJThNQyv7iGSs1gursfPu+iii5a1tLSMY3xkad2myDntHz38m0vTzCQgAQlIoEoEKvXgb6L8g6q0DYtpcAJPPvnkDIz9B+hB9L0FCxYc2JOQWDbecHcm+uwvf/nLZow+vjPfNbWzzKZdJzgsAQlIQALVIVDpGnxcf38/ipvsul6DP5ED8s+qs+n6lYKxeA2+frhftiX4x/X4/c8///xf8335eHd9GPsSpj+wcuXKd02ZMmXJy1ZyggQkIAEJ9IkAx9bK34NnZqU32f2VA/OzfdpiiisRjwafIv/YNG1wLlm8KGkON+1dyvi7GZ7HTXaHpFw1Ny8BCUggVwQ4vlY2+FxFSjAafPotShuMohaX0Fsfu+eee8Y35OMa/NqOjo4ftba2HpF+Da2BBCQggXwQSAy+0jX4fERpFJkhEKfkqUzbLbfcsu/SpUs7b7BjvImb7MZnppJWRAISkECOCGjwOWrMrIeCyd87bty4e6+//vrouUd116K4fGKSgAQkIIEqE9DgqwzU4tZP4PDDD38DvfZF3FW/EsO/7a677tpz/Ws4VwISkIAE+kKg7F30fSkoy+t4DT5brUN7DKdGF6NZmPyt2aqdtZGABCTQ2AS8Bt/Y7dfQtcfUXySA09HJ7IjbNnQwVl4CEpBARgl4ij6jDZP3amHyfyXGmegsTH5Y3uM1PglIQAL1JpApg+dAvwmq9PrcerNxezUmgMn/nE3cjT5T401ZvAQkIIHCEUjd4DH0I9CdKF5juhitYPhhNAPFs9OmfBO4kPBG09ZH5TtMo5OABCRQXwKpGjwH9XcS7jT0BTQWxRfL4mMk8Wx0fOgmXplryjEBevFrCO+L6Cj2h71zHKqhSUACEqgrgVQNnkgPRmdzkP8ZWow60HL0N6afgrbjoL8JuSnHBGjvpwnvS6iN9t45x6EamgQkIIG6EUjb4OP663gO6uXqEb25LdDSutFwQ6kRwOTvYePnofhW/JapVcQNS0ACEsgJgbRvaPsRHA9Dcc39DvLFaAiKA3wY/EQO/HEK11QAArT1bewHowj1PPLJjMf+YJKABCQggT4QSNXgOYDH89BHczCPr9fthrZDy9Hj6Ebm97j3Thk3ss4gVC5tXG6i07JHgDb/KW0Zl2W+Qn5CaR/JXkWtkQQkIIGME8j0m+w4wG/EAX5ZfxlSjp+L7S/EOq9Pm01hkzuhT7MPxA2XpioQmDFjxqthO3LSpEm3wTW+BWCSgARyRoD/8c7PxZa79l3XUKnIF9Hj6G9o3WdDGY476qOHbyogAbxnOmEvQrF/pL6f5qEJZs2a9ZmWlpafNTc3Xzt79uw/zpkzp9IZrzyEawwSKDyBVA+cHLjfSAvEnfSHoHhULq69TiA3SSAInI2GopNjxNR3AtOnT9+Cj/ycQQmj0aZom/b29vi/M0lAAjklkKrBw/R96CJ6a/PQFQy/BX0ek1/Xk2fcVFAC7BPthB7PyO/EPnFsQTFUM+yFXQobguF7ir4LEAclkDcCaRt8PBoVvfjOxAH9UQbirvo4PfuOzon+KTQB9okVADgNvQ2T/9dCw+hH8FOmTIl3DZxVKuIxuN4+ceLE6/pRpKtKQAIZJ5DqTXYcsOOtdbehZRxw1hk90/dn2vVoM6Y3kfcrUZ432fWLYPor04bxToRvoNnsEzelX6PGrAHX3neB34jjjjvubnJ78I3ZjNZaAuslwPGy8ya7tB+TW0ot96Ey+3WtLced3zFtL6ad2HW6w8UlwD7xNPvEqRC4gHwJ4/HehAEzZ858Faead2X89tbW1vghZ1o/gbHwG3HkkUfOY7G4BGKSgARySiDVHny9mHJAswdfL9g13g5tuQebiJvvPssjXxtzR/gvGF6JNuemsd3omf6ZYVMZAvTep8HvI8yKH/ZLXnzxxd1OOumkeO+ESQISyBEB/s+z8ZhcjpgaSh0I0FOfz2bC4L+8ePHiy8jjMs/maABm/+nITS8nwI+hMfzTf4I5weoVcBw5bNiwQ1++pFMkIIG8EEj7Jru8cDSOOhLAnH7P5mZcdtllw5Yujas8namd6X6YKKHRLef593jlc9xol6ShGL6994SGuQRySECDz2GjFiEkzHzuCy+8cNbVV189YPny5Y8Q81+ef/75Y4oQe19inDBhwjMdHR1xD0Okv2Pu13AX/U9fGvWvBCSQRwJeg89jqxYopo022uhDXHuPF+F8edWqVfH61a691AKR6FmoF1100ZaDBw8ehuH/rWdruJQEJNBoBPgB7zX4Rms06/tyAsuWLfs+xv4hFDffXcKOfSmaiOKd6/G6Y1MXApj7a/gR9Ia2trbBXSY7KAEJ5JCAPfgcNmpRQ8LQ450J8VXCeOzyDWg79Ed0O/pd0Xv33EX/XTgcCqZ4B337mjVrtuajMy8wbJKABHJEgP/x9J+DzxFPQ8kAAQw8XtxyX0nfZiePb8vvi8LwP854fF8+btAL3cPiceNZIVLp1Hy8JTL5dPKzPHUQ34G4uhAADFICBSSQ6otuCsjbkOtIAANfwubirXedb77D4F/FcPTs/wPtyHjSu7+dZZ9kWm4TZh53zD+HEoMfQczrHkHIbeAGJoECE/AUfYEbv8ihY+4jiD/MPhQ9/DC7OJUfit79avJcJT4P+3YCuhk9RHxxF/0puQrQYCQggU4CHN86T9Fr8O4QEoAA/xC7kIXRh3ZC8SGkOJUf1+4XkOciYfIb87jcEF/rm4vmNAgJlCWgwZfF4kQJdJp99O5fj6J3Hx8+ehFFzz4MPz7SsorcJAEJSCCTBDT4TDZLfirFHdv7YISb8SGY/+WZ64Y+3c0/y860THIqf1eG40MtnafzifHx/LSakUhAAnkgoMHnoRUzGsOsWbM+gbHH++IjDeRxrO3y8jgW/zgbEVP07pPT+fGhm+jZh+H/wd49FEwSkECqBBKD9y76VJshnxvH3C8ksqGl6JYxfhTDl+YhWgx8GXHcWlJcu9+R4ejdH42mMv4n8k7DZ9lHGTZJQAISSIWABp8K9txvNF6DOq4UZTuml9vnzTHxB4kz9APiHEb+OhS9+6MYbycPs/85y80nN0lAAhKoGwENvm6oi7MhzOwEzG0u+SPk87lj+3Ieyao6AF63OnDs2LGfoeB3sa2fs43/qvpGelEgdVjO4reVFL377RmO3v3nGF5Bfj26keXipj2TBCQggZoS8GtyNcVbzMIx2ptWrly5DaZ22Ny5cw/D0OINc1VPY8aMuYSiT6fgt7GtqTwC9vGqb6QfBVK3h9GV6CMU8w20N4qe/qdQvFLXJAEJSKBmBOzB1wxtsQueMmXKYxB4jDvoawYCk9yTwoeUNhD5e9AlpfFMZZh8vDXvj9Q53iR3CPoiw9Hj/wmKsx1xbd8kAQlIoGoE7MFXDaUF1ZsApngD24y72COtYvzXLw1m9y91fB79AH2YWs5Ar0FXYvbRq39VdmtuzSQggUYjYA++0VrM+q4jsHDhwjauwY/GLHfGHOPVq3EavGES9f4Dlf0DdY9e/aHodIbj+nxyrT56+CYJSEACfSLgq2r7hM2VJFAbAhh83IX/XhT5LSjeKf1ncpMEJCCBHhHgOOLnYntEyoUkUEcCmPldbO4u/kE3IY9r9fFs/VLy69BNzK9Jr54bFOMb8eei8WzjZp58OJm8JjdHsg2TBCRQBwKeoq8DZDchgd4SwFufZZ3vhzD4eHNe9OpbGb6Z/Drm/4W8aokP0PyYFxKNp8DBbGNX3kYY5cc9AiYJSKBBCWjwDdpwVrs4BDDzO4n2Tow3evVxrf5LDD9PHnfg38z8fvfqMfctKGswijSU8t9BrsF34vCPBBqTgHfRN2a7WesCEohePfoe+iDhx+OA+6H/xoxPRjv3Bwk9+GtZv/MreZS/Jnr0/SnPdSUggfQJeJNd+m1gDSTQZwIY+6asHL36w1Cc1o9r9dGrjzfn9ThRThNfADyd9fZgpR/xRMJ/93hlF5SABDJFgH/nzpvsNPhMNYuVkUDfCIRBs+a+6H1oH3QTimv18V0AkwQkUCACicF7Db5AjW6o+SWAkccd778P8c8dvfp4q9/ZDC8iT3r1yUuBmGSSgATyTsBr8HlvYeMrHAG8fjG6nMDjWv1l6M3oKsw+PgK0I8MmCUigAATswRegkQ2xmARKvfrbif52jH1z8rhWfw7DT5LfgHp9rZ51TBKQQIMQsAffIA1lNSXQHwKY/TPoO5RxNPouehOKXn3cge878IFhkkDeCNiDz1uLGo8E1kOgW68+rtUfguJtectGjBgx96STTvpTc3Pzn6dOnbpmPcU4SwISaAACGnwDNJJVlEAtCGD2iyn3e6E999zz4G222eb7V1111dCtttqqedSoUROXLl36K5Z5qBbbtkwJSKD2BDT42jN2CxLIPIHjjz/+Aiq5yapVqwY8+uija1avXv2RW2+99Z307IcwPe7OvwPdieHHe/FNEpBAAxDQ4BugkayiBOpA4BG2sevgwYMH7LTTTi1o6a9+9asPY/BbMv0N6CB0KuN/J+98HI/8fgy/g9wkAQlkkIAGn8FGsUoSqDeB9vb2M7n2Hib+DBpGD/7EqAMG/gRZvMb2Wsy9mXwvFIZ/Mnol0+Lrd9G7/z3LxromCUggIwR8k11GGsJqSCBtAnwydidMeg9O0/9xypQpj22oPph73KQXb89LFNf0k979PMpavaEynC8BCVSfAP+bvqq2+lgtUQLFJMABJV6VuyuK3n1oJzQPJb37Rxk2SUACdSCQGLyn6OsA201IIO8E6K3Hq3L/XNLlHGCGM/w6FL37oxhf9ypdxv/A4svITRKQQA0JaPA1hGvREigqAQz8RWK/taQBGPy2DEfPPj6G8znG/0Ke9O4fYNhUJwKzZs2aAf93ox3Z5HYTJkyIGyxNOSSgweewUQ1JAlkjgOGHiYSuxlgGk8cX76J3H2b/CvJOs4+cZZ8jN9WAAOb+iYEDB36IojeO4mEd70E4IIZN+SOgweevTY1IApkmgKmsooJh6KHoTW5BHr37t6D4IE7cuZ8Y/v+xfDvjpioQgO3G8BxJ3lka+R5VKNYiMkpAg89ow1itxicwffr0rXmu/N1E8szEiRN/3PgR1SYCDOdpSv6fEIYzkDxMJ3r3rWgrpt1L/qeS7mN5X6MLjL4kHn/8IfvkV1l3FRxXwHZmX8pxncYg4GNyjdFO1rLBCPDIWfSU7qbaYzmItnR0dJzb2tr6+QYLI/Xqwm4klXgNiufvQ9ujuH4fd+iH6UcP/wVyUw8JnH/++a8YOnToBzhV/zTX3+OHlSlnBPi/6XxMLnM9eCo2FNYb8U8bz9SaJNCQBNh/P0zFo/c5KALgYHokmQYfMHqR4Bivxr21pLhZL44P0cPfGwXT05m2kDzp4d/DOnFGwFSBwIknnhj3OHy7wmwn54hAqgbPP+YxsBzJP+RFDMepufPRR9Awxm8hP5l595GbJNBQBNh/w2RWoE6DJ9+loQLIaGU5HgTTP5QUhh9v19sZRe8+bhabzLS4xh+GH738/0MPsV48pmeSQKEIpGrwkB6LNikRj89W7ofin/RxNAFdhfZEJgk0FAGuuV89e/bsT+Mrr8ZwBvB2uDdmOYBp06YN22ijjS6mrgdQ53k33XTTB6688srM39xGXaOOyfP3VwdjYtiKLAw/jh1HoM2YFtfxQ2H481lvOblJArkmkLbBd4X7fkYu5B8v/gkjfYV/yo+jzZnmO65fYuLfBiGAuR9KVbdj/+38Hxs0aNDXGX9rVquPud9C3V6LWqjzKw866KCTGY6bsRoucbyIDkLoZ1F54hlJFmYf+hjalWkxP3r5nabPOnGa3ySBXBHIgsHvwj9bXFO7DW2Z0GXaZgzH3bKLkmnmEmgUAhjGNuzDmyb1ZThe45rZRP3iFHZyPBhK/ffPbGV7WTFiWcoqvy0pOa2/C+Nh+PFoXivxk3WafWL6D7Be5s9gRKVN+SHQ1tY2dPTo0cdwz0743/ncBNmvNz7Gde80U/zThYlfi2ajL/GPNgjFwSXulL2UfzKvnQHC1FgEuGv+l9Q4rg/Hy0SWozuyHAH1+w71Swwt8llZrm9/6kas7Sg+dXs1OgPFafxPoriZL07vx9mL6zkOXYAmoDehUUwzSaBmBNjHmsaOHXsP5j4t9ks29Pt4Gqc/G8zMY3LENoJAxhHY7QzHay2jF/FAT4Njne+z7KAKyx/G9K0pz7MBFQA5ufoEeGvY9vyzHk/J98+dO/ebWb+mzcHkQ9R1H36c/JxH+m6uPpHGKZHjyTBqG2cWx6Ho6cfwYhTX8O9HcQ3/he7iGOO1faCYek9g5syZr+J4cQv73phYm33pKbKPcD/Pjb0tjTI6H5PLjMH3NoDuyxNQnNJ46fVM3WcOGHAfk3bT4F8OxikSkMCGCXB8iWPL9ihu3tsBRYeknAYz/WXGX5oWlwoqzYvpSzhGxRMApgIS4Af25oR9D1p3qZr94dUYfEzrVcqdwa8veoKNX0K7a/Dro+Q8CUhgfQRKZ2TiTOGrOJZcyoH3U92X51gTl2XKGX9Pp8X6YfYvlvJKPwjKzqdeK1jP1KAELr744n1pw1uo/l85k3YmZ9J+2JdQMmHwVOI9VD5+EVdMBHtOxZk9nKHB9xCUi0lAAhUJ0MNadz8Qx6UlLBinT6+ruEIfZnCsisuM8WNgeCmv9MOg0vxYvyc/EKL+cUbh+VIeZw9iPVMOCCQGn9w1m1ZI8Q/zeRSv9JyXViXcrgQkIIENEcAAH+TAuWMsRz6UHlZcFqxqYhurKfDZknpdNvXqyRmEuMdpIxTvIImbB0eijVk37jsI408U5t/1h0AyPfJkXvwwiDqbMkggVYNnx/gpO9X74HIpOpjxOO1kkoAEJJBFAjdQqQ9znBrBcWswN0Rdk7VKUrd4AiLMN9SrREzxVFUYfiju3k6Gk3xsuXmsF9ssa/7dpnf9YfACdV13RoTlTDUgkImb7NhBohf/O9r7phrEGL+2vQZfC7CWKYGCEeA0/TH03Edi7j/hGeVHqhU+d1Dv3dzc3EKZf6hWmfUqh+NrfB8g+RHQ/YdBjMcZgpjfdV5cYohLAvFDJC4VdDX/5MdCcpYguZQQZwtWsqxpAwRok3zdRb++eDX49dFxngQkkCYBfjSci3F9lDoM51h1IyZ/RJr1qce2iTOeSkh+FHReIugy3v3HQLJc5HGWITlDkfwQ6J4nPwyS6Uvh28F6hUng1eAL09oGKgEJZJIArzSON3nOo3LRC470JDoCk483e5q6EYBVPIbY9UxAV/Mv98Mglo0bFeONcGH43c0/xsueQeBHQcO+0yAx+FSvwQPWJAEJSKDoBJ4BwNYBAVMZjrw2XWGPAE28J+Dpkios9fLJGF6YfPcfBsn4duXmsU74Y/IDoNwPg+QMwT/l1DHuSchEysQ1+FqToKG8Bl9ryJYvAQn0iQDP13+Na/qTWXkx5nA7j94d3qeCXKmqBEoGn/wI6HqmoNy0rvPjXQT/ZPpdxsv9UIhLCFW9wZy6e4q+qnuDhUlAAhLoI4EZM2a8mlUHTZo06Y4+FuFqGSGAucYNhInhx4+BcjcZdr2cEPPjEcWu5n8apt/nSwQaPDRNEpCABCQggbQJYMjx/oLkR0Hk92Hw8SG2PqXE4L0G3yd8riQBCUhAAhKoDgHMPK7bP1tSdQqllHjkwCQBCUhAAhKQQM4IaPA5a1DDkYAEJCABCQQBDd79QAISkIAEJJBDAhp8DhvVkCQgAQlIQALeZOc+IAEJSCBFAtzx3MSz8IdQhUELFy68burUqYV6rWqK6HO/aXvwuW9iA5SABLJMAHO/nvp9B5+/fOzYsfPa2trseGW5wRqobhp8AzWWVZWABPJFgA/N7IWxvxHFt+VH8rjUZqNHj35rvqI0mrQIaPBpkXe7EpBA4Qm0t7fHK0q7vrFsc8bjM6omCfSbgAbfb4QWIAEJSKBvBI477rgH6bWfztrxgZm/05M/q7W19fd9K821JPDPBPzYzD/zcEwCEpBA3QlwHf6VfHCmmc/ELqj7xt1g7gjwQ9GPzeSuVQ1IAhKQgAQKTyAxeE/RF35XEIAEJCABCeSRgAafx1Y1JglIQAISKDwBDb7wu4AAJCABCUggjwQ0+Dy2qjFJQAISkEDhCWjwhd8FBCABCUhAAnkkoMHnsVWNSQISkIAECk9Agy/8LiAACUhAAhLIIwENPo+takwSkIAEJFB4Ahp84XcBAUhAAhKQQB4JaPB5bFVjkoAEJCCBwhPQ4Au/CwhAAhKQgATySECDz2OrGpMEJCABCRSegAZf+F1AABKQgAQkkEcCGnweW9WYJCABCUig8AQ0+MLvAgKQgAQkIIE8EtDg89iqxiQBCUhAAoUnoMEXfhcQgAQkIAEJ5JGABp/HVjUmCUhAAhIoPAENvvC7gAAkIAEJSCCPBDT4PLaqMUlAAhKQQOEJaPCF3wUEIAEJSEACeSSgweexVY1JAhKQgAQKT0CDL/wuIAAJSEACEsgjAQ0+j61qTBKQgAQkUHgCGnzhdwEBSEACEpBAHglo8HlsVWOSgAQkIIHCE9DgC78LCEACEpCABPJIQIPPY6sakwQkIAEJFJ5AS5YINDU1bUJ9lq5du3ZNluplXSQggfoTmDNnzqfZ6rFoV44J4yZOnDi/J7VgvY1Z7hrW2Z1jyp8XLFhw0NSpUz2m9ASey+SKQOo9eP4Bj0B3opWQXYxWMPwwmoFG5Yq2wUhAAj0igEm/iwXPQbuWVvhRj1ZkIYz9T2QHcvwYQ77f6NGjp/R0XZeTQJ4IpGrw/AO+E5jT0BfQWNSMRqLxaBW6FpkkIIHiEdgco34xCZtjxWbJ8IZyln2KZZpKyw1lfK8NreN8CeSRQKoGD9CD0dn8I/8MLUYdaDn6G9NPQdvxzxmn7U0SkECBCCxfvvwm/vef5FiwmrDjst1vexo+y17Csk+Rd5CvRV/r6bouJ4E8EUjb4O8G5nj+kcvVY2/mbYGW5gm4sUhAAhsmcMIJJzy5Zs2aV2PSp7J068KFCz+w4bVeWoJr9bM6Ojo+xnHleKbs09Nr9z0t3+Uk0CgEmvgHSq2u/AMOZ+OXojeiO9BiNARticLgT6B+V5D3K7GdOGW3O2Ut6ldBriwBCUhAAhLIOAE873r87rBU76KnAnGN7Wgqsw/5bmg7tBw9jm5kfo9775TxUdaJa/jlUvxoMElAAhKQgAQKQyBVg08oY+T3MBzqT4pTEZVORyzoT8GuKwEJSEACEmg0Apkw+GpA40fCdyuVQ+/+vZXmOV0CEpCABCSQRwKpGjzG+x6grvcRFow7noU1SUACEpCABCTQCwKpGjz1jFPqn0dxN/08ZJKABCQgAQlIoAoEUjV4euc/pRf/PuKIO+kPZnzdiy2qEJtFSEACEpCABApLIFWDD+qY+i8x+Xgxxf7opphWgxTP2Q9iOz1+G1YN6mCREpCABCQggXoQ6HyiLNXn4OsRZWwDY4/r+ONQvO++pymexY/H9zyrUJnYUGbFZZbecK1cWj7njCCsZSjeqmYqTyA+DvN8+VlOhUC8dnckWiKNigQGMSeO1b+ruESxZqym8/xvhTD4vrQrPwpez3pHA+lTfVm/COvAaBJxPg2jq4oQb19ihNG3WK8NRg/3Zf0irAOjW+BzYBFi7UuM8NmI9a6CUdyUbCpDAEZvY/LbYXRGmdmFnVTuFbGFhWHgEpCABCQggbwQ0ODz0pLGIQEJSEACEuhCQIPvAsNBCUhAAhKQQF4IaPB5aUnjkIAEJCABCXQhoMF3geGgBCQgAQlIIC8ENPjKLRmPNa2pPNs5EGhHPv61/l0h9qF4lNBUmcDqyrOcA4HYf2S0/l3B43UZPj4mVwaKkyQgAQlIQAKNTsAefKO3oPWXgAQkIAEJlCGgwZeB4iQJSEACEpBAoxPQ4Bu9Ba2/BCQgAQlIoAwBDb4MFCdJQAISkIAEGp2ABt/oLWj9JSABCUhAAmUIaPBloDhJAhKQgAQk0OgENPhGb0HrLwEJSEACEihDQIMvA8VJEpCABCQggUYnoME3egvWof58a7kFNdVhUw27CRk1bNNZcQnkloAGT9NycH49eqSbtopWZ9qB6Db0ELoGbZLbvaFMYMS7DZP/jnZMZgcDdCX6C/oTelOXeYXjRfzlGLlPsVPAZg90BboH3YSO7sm+wnKfRfNQ/N99Nlknjznx/Ru6GQWj76LdkzgZvgN1PTZN7DKvSIw+CIc4Ds9HsT9tvCEOLFPxOJWsm/t87dq1A4ouGrkVfRNt1EXRY90cPYH2RoPQ19GlReFFrMeiv6JVaKckboavRF9AwehAtBANQ4XjRcyVGLlPcWwh/QJ9LPYd0pboSTQaVdxXmHckug3FQXwMuhsdkux/ecpL8cX/z+gSo/9g+Oel4c0YXoyGo+TY1FKaVyRG0blYgLYoxX4xw+dtiAPLlD1OxXpFkT149gLSq9Ht6JUodqJlKI5Ir0f3MTgPxccepqPDUe4Tv34HE+RR6FD0XLeAD2Z8RjAi3cLwY+gtqFC8NsCo8PsUfOL4MgNdgeKgGj+Wl6LXovXtK7F/fZfln0dhfrH+v6A8pmB0FHHGD59I96DkjFjsQ3eh+CG9C1rFcvHxokhFYvQQ8e5J7E93Rj5gQAt5c2l4fRxiXrnjVGnV/Gca/EttHP9Ip6LobTzMgenclyYP2JY8fjkmKf4JN2b+kGRCXnP+meJg8m70QNcYiT0uUQxhevQskhQH4fhxVChelRiVoBR+n4JPB7oWdX4JjX1nPGxi//ntBvaV7vtR7F/R689dgs0T6NYugU1g+H9K47EPjUN3ot+g38PwFeSRisQIRGsXEftu6Cpi3w+dFxBIZTmw3PqOUy+tWYC/GvxLjRy/ko9lJ9qVPHoXU9hBtiCPU2QvoiQtLw3E6bKipu5MgkNwGYG6zysyL/ep2DNKif+n+N+6HH2S/7PnyNe3r3Sft4zl4zR1rhOMPk6A70XR2YgUP2zOh9du5HGfR3A4CkUqIqNhxB0djqHo3ShSJQ7dp8eyyXEqhguR4lRH4RP/QJMTCAz/kX+0XzMep+KfQXH9PUkjGVjBMs8mEwqYB5NR3eKO8SdQB5IXENyngFBK/D+FQd2MzoTLFaXJFf+3WL77PpbsX6VV85cR80SiakNvh1Fc8op96HuRR2J4Mct8h8Ew+DmocIxg8EfijuNz7Es/II/7pipx6D6dRTuPW3GcKkwqfA+enWQomhp5l1aPHvrTKP7RtkdJ2p6BR5ORIub8k0Xvazm8tu4S//YMP4LkBQT3KSCUEix2ZHAu+jL7zqxkOvn69pWYt12XZbdnOLf/dzA6hvjOQAfB6D7yzsT0D6N9k3Hy6MHGcSlSYRjB4HUofgAlaT4Dm6O4XFGWwwaOU0k5uc8Lb/DsCCto5XegY6O12ZH2I3sNiuvx8UtxR6aNR0MYPgX9EBU9xd2pn4ZJPB//rwxHzz3+6eQFBPcpIPwjxWn56InGo02bljSY8fXtK7F//TvLbom2Z/iD6BqUu0R8OxDUN1DE+ESJz6alQOM68tlMG4TilPNH0U9K8wrDiHij130uDLZG4VmfRHHjc5xJXR+HSscpVitIAlIckAotmvpNKE7Lx/Wd2Gn+LWHC8JFoKYpfinFQGpHMK0pOzE+hro/Jbc/4n9Dj6K/owIQFw4XkRdzdGRV+n4JJ9D7jaZTuOib2F1LZfYXpTehSFP+LC9AZyf6Vt5zYvoq684nxjdAIdAWK/7FgEaekB5fYFYZRKd7jiT2Oz6Ew7nEb4sAy26Oyx6m87UeV4mkqQYKDiV+H8cv5OZh0dKXB9BbGRzI9/slMJQJwiUcKk1OG67jIax2KOCPkPvUPHC8bWt++wry49r6SfWzly1Ys0AQ4hNlHJ2xZ97CLxIhY40dNHIeX9IYDq5U9TnUvI4/jGnweW9WYJCABCUig8AQKfw2+8HuAACQgAQlIIJcENPhcNqtBSUACEpBA0Qlo8EXfA4xfAhKQgARySUCDz2WzGpQEJCABCRSdgAZf9D3A+CUgAQlIIJcENPhcNqtBSUACEpBA0Qlo8EXfA4xfAhKQgARySUCDz2WzGpQEJCABCRSdgAZf9D3A+CUgAQlIIJcENPhcNqtBSUACEpBA0Qlo8EXfA4xfAhKQgARySUCDz2WzGpQEJCABCRSdgAZf9D3A+CUgAQlIIJcENPhcNqtBSUACEpBA0Qlo8EXfA4xfAhKQgARySUCDz2WzGpQEJCABCRSdgAZf9D3A+CUgAQlIIJcENPhcNqtBSUACEpBA0Qlo8EXfA4xfAlUg0NTU9DDaqwpFWYQEJFAlAhp8lUBajAQkIAEJSCBLBDT4LLWGdZFAYxM4kl78Q+gRdGyEEr16dAX6NnoK/Ri9srHDtPYSaAwCGnxjtJO1lEAjEDiASr4VnYZmY+RDyUMfRE+i3dBCdDEySUACNSbQtHbt2hpvwuIlIIG8E8DMHybG4zie3BCxMv40WRj+SPQbtCnzljJ9Z4bvR8MZX0lukoAEakTAHnyNwFqsBApI4IkuMT/P8LDS+L1h7jFM/leyZWi7GDdJQAK1I6DB146tJUugaAQ6KgS8Az335phHvi3ZRujRGDdJQAK1I6DB146tJUtAAi8RGEX2Acy9ifzf0a/pyS8nN0lAAjUk0FLDsi1aAhKQQBCIG+ymovPRc+hQZJKABGpMwJvsagzY4iVQZAJ02vcl/svpse/GcNxot7jIPIxdAvUk4Cn6etJ2WxIoMAHNvcCNb+ipEPh/IQwk3TvTQlwAAAAASUVORK5CYII=" /> <br />
<h2>
Inline code</h2>
Inline R code is also supported, e.g. the value of <code>x</code> is 2, and 2 × π
= 6.2832.<br />
<h2>
Math</h2>
LaTeX math as usual: \( f(\alpha, \beta) \propto x^{\alpha-1}(1-x)^{\beta-1} \).<br />
<h2>
Misc</h2>
You can indent code chunks so they can nest within other environments such as lists.<br />
<ol>
<li>the area of a circle with radius x<br />
<pre><code class="r">pi * x^2
</code></pre>
<pre><code>## [1] 12.57
</code></pre>
</li>
<li>OK, that is great</li>
</ol>
To compile me, use<br />
<pre><code class="r">library(knitr)
knit("knitr-minimal.Rmd")
</code></pre>
<h2>
Conclusion</h2>
Markdown is super easy to write. Go to <strong>knitr</strong> <a href="http://yihui.name/knitr">homepage</a> for details<br />
.<br />
-->
<span class="GingerNoCheckEnd"></span>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-4786951575044412222012-08-21T14:46:00.000-07:002012-08-21T14:46:34.991-07:00Clase 6: Índices de biodiversidad en R-project<div style="overflow: auto;">
<div class="geshifilter">
<br />
<h2 style="text-align: center;">
Índices de biodiversidad</h2>
</div>
<div class="geshifilter">
<h4>
</h4>
<div>
Continuando con este breve curso sobre bioestadística en R (<a href="http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=3755505426694997489;onPublishedMenu=overview;onClosedMenu=overview;postNum=5;src=postname">Clase 1</a>, <a href="http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=4940257633356261182;onPublishedMenu=overview;onClosedMenu=overview;postNum=4;src=postname">Clase 2</a>, <a href="http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=9062747400333318423;onPublishedMenu=overview;onClosedMenu=overview;postNum=3;src=postname">Clase 3</a>, <a href="http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=4881665738337319329;onPublishedMenu=overview;onClosedMenu=overview;postNum=2;src=postname">Clase 4</a> y <a href="http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=7638722570605816071;onPublishedMenu=overview;onClosedMenu=overview;postNum=1;src=postname">Clase 5</a>), en esta oportunidad intentaremos abordar el problema de los índices de biodiversidad utilizando R. Comenzaremos con algunos conceptos previos.</div>
<h4>
¿Qué queremos decir por biodiversidad?</h4>
</div>
<div class="geshifilter">
<span class="Apple-style-span" style="font-family: TimesNewRoman; font-size: 15px;"><i>La biodiversidad o diversidad biológica se define como “la variabilidad entre los
organismos vivientes de todas las fuentes, incluyendo, entre otros, los organismos terrestres,
marinos y de otros ecosistemas acuáticos, así como los complejos ecológicos de los que
forman parte; esto incluye diversidad dentro de las especies, entre especies y de ecosistemas”
(UNEP, 1992). El término comprende, por tanto, diferentes escalas biológicas: desde la
variabilidad en el contenido genético de los individuos y las poblaciones, el conjunto de
especies que integran grupos funcionales y comunidades completas, hasta el conjunto de
comunidades de un paisaje o región (Solbrig, 1991; Halffter y Ezcurra, 1992; Heywood, 1994;
UNEP, 1992; Harper y Hawksworth, 1994).</i></span></div>
<div class="geshifilter">
<span style="font-family: 'TimesNewRoman'; font-size: 11.000000pt;"><i>Fuente: </i><a href="http://entomologia.rediris.es/sea/manytes/metodos.pdf">http://entomologia.rediris.es/sea/manytes/metodos.pdf</a></span><br />
<span style="font-family: 'TimesNewRoman'; font-size: 11.000000pt;"><br /></span>
<br />
Es decir, existen distintas <b>escalas</b> de diversidad:<br />
<br />
<ul>
<li><b>Diversidad a escala genética.</b> </li>
<li><b>Diversidad de especies</b>: la diversidad <i>alfa</i> es la riqueza de especies de una comunidad particular a la que consideramos homogénea.</li>
<li><b>Diversidad a nivel de comunidades</b>: la diversidad <i>beta</i> es el grado de cambio o reemplazo en la composición de especies entre diferentes comunidades en un paisaje, y la diversidad <i>gamma</i> es la riqueza de especies del conjunto de comunidades que integran un paisaje, resultante tanto de las diversidades alfa como de las diversidades beta (Whittaker, 1972).</li>
</ul>
<br />
<br />
<h3>
Diversidad de especies. </h3>
La diversidad <i>alfa</i> tiene dos componentes fundamentales:<br />
<ul>
<li><i>riqueza específica</i>: número de especies que tiene un ecosistema</li>
<li><i>estructura (equitabilidad)</i>: mide la distribución de la abundancia de las especies, es decir, cómo de uniforme es un ecosistema.</li>
</ul>
<br />
<h4>
¿Cómo podemos traducir un concepto intuitivo en algo que podamos medir?</h4>
<br />
<img src="webkit-fake-url://3010ECCD-ED30-47C0-AF6F-401EB1073F3D/image.tiff" /><br />
Fuente:<a href="http://entomologia.rediris.es/sea/manytes/metodos.pdf"> http://entomologia.rediris.es/sea/manytes/metodos.pdf</a><br />
<br />
<h4>
Métodos paramétricos: índices</h4>
La medida de ocurrencia Jaccard o Coeficiente de comunidad es una medida muy simple e intuitiva (Shi 1993; Magurran 2004). <br />
La medida de Sorenson o índice de Dice, Czekanowski or Coincidencem hace más énfasis a las especies presentes compartidas más que a las no compartidas, y su cálculo es relativamente simple e intuitivo (Wolda 1981; Hub?lek 1982). <br />
Otros dos índices similares sue se utilizan ocasionalmente son las medidas de Ochiai y Kulczynski. Sin embargo, se eligen las medidas de Jaccard o Soreson porque son las más utilizadas actualmente.<br />
El índice de Simpson intenta solucionar uno de los problemas más típicos, el de diferentes tamaños muestrales entre localidades. Comprar sitios con intensidades desiguales puede generar visiones sesgadas. Teniendo esto en cuenta, Simpson (1960) desarrolló una medida para tamaños muestrales distintos. Su fórmula reescala el valor por el número de especies del último sitio muestreo, por lo que una submuestra tendrá similitud con la muestra original. Por tanto, este índice es utilizado con datos altamente variables en intensidad de muestreo, por ejemplo los datos de fósiles.<br />
La medida de Bray-Curtis es posiblemente la más utilizada, debido a su gran relación con las distancias ecológicas bajo condiciones variadas (Bray and Curtis 1957; Faith etal. 1987; Minchin 1987; Clarke 1993). Esta medida es equivalente a la de Soreson, cuando la utilizamos como medida de similitud con datos de ocurrencia.<br />
A pesar de ser menos conocido, el índice de Mosisita-Horn es recomendable debido a su relativa independencia del tamaño muestral y diversidad (Wolda 1981; Magurran 2004). Existen muchas variantes de este índice.</div>
<div class="geshifilter">
<br /></div>
<br />
<h4>
Métodos no paramétricos y de rarefacción.</h4>
Uno de los métodos típicos para solucionar los problemas de muestreo desigual en intensidad, es el índice de rareza (o también conocido con el peculiar nombre de "rarefacción" o interpolación de los datos; Sanders 1968). Los índices de rareza permiten comparar entre distintas comunidades donde cada una es "enrarecida" a un número igual de ejemplares muestreados (Heck et al. 1975; Foote 1992; Colwell and Coddington 1994). <br />
<br />
Los índices de rareza interpolan nuevos datos, mientras que los estimadores no paramétricos de especies extrapolan los datos para encontrar el "verdadero" número de especies (Colwell and Coddington 1994).<br />
<br />
Los estimadores no paramétricos utilizan el número de especies raras para encontrar la manera de calcular qué tan probable es que hayan más especies sin encontrar en la muestra.<br />
<br />
Por ejemplo, el estimador de Chao (Chao 1984; Colwell and Coddington 1994) calcula la diversidad de especies verdadera estimada de la muestra mediante los datos de abundancia. En caso de que no contemos con datos de abundancia sino de ocurrencia, podremos utilizer el estimator Chao 2 (Chao 1987; Colwell and Coddington 1994), que estima la diversidad de las especies a partir de los datos de ocurrencia de múltiples muestras.<br />
<br />
Otro estimador es el de Jackknife (Burnham & Overton, 1978, 1979) originalmente utilizados para dates de capture y recaptura.<br />
<br />
Dentro del paquete fossil existe un método de rarefacción conocido como la curva de Coleman(Coleman 1981; Coleman et al. 1982), que se desarrolla como un método de remuestreo más que mediante una fórmula de rareza. <br />
<br />
Por último está el estimator de riqueza boostrap (Smith & van Belle, 1984). <br />
<br /></div>
<div style="overflow: auto;">
<h4>
Aplicación en R: paquetes fossil y vegan</h4>
</div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Análisis de datos con R #</span>
<span style="color: #666666; font-style: italic;"># 2012 #</span>
<span style="color: #666666; font-style: italic;"># Msc. Rosana Ferrero #</span>
<span style="color: #666666; font-style: italic;"># http://statisticalecology.blogspot.com/ #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;"><span class="Apple-style-span" style="color: black; font-family: Times; font-style: normal; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">###########################################################################</span></pre>
</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;"># 6. ANÁLISIS DE BIODIVERSIDAD #</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"># librerías fossil y vegan
<span style="color: #666666; font-style: italic;">###########################################################################</span>
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">############ con la librería fossil #################</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;"><span class="Apple-style-span" style="color: black; font-family: Times; font-style: normal; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">############ 1. Manipulación de los datos </span></pre>
</span><span class="Apple-style-span" style="color: black; font-family: Times; font-style: normal; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">############ 2. </span></pre>
</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">###########################################################################</span></pre>
</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/fossil">fossil</a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.list<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/head"><span style="color: #003399; font-weight: bold;">head</span></a><span style="color: #009900;">(</span>fdata.list<span style="color: #009900;">)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">############ 1. Manipulación de los datos </span></pre>
</span><span class="Apple-style-span" style="color: #666666; font-style: italic;">#las funciones estimadoras de especies pueden agruparse en 2 categorías: las que usan los datos de ocurrencia y aquellas que usan los datos de abundancia. </span>
<span class="Apple-style-span">
<span style="color: #666666; font-style: italic;">#matriz de ocurrencias</span>
ocurrencias=create.matrix<span style="color: #009900;">(</span>fdata.list<span style="color: #339933;">,</span>tax.name=<span style="color: blue;">"species"</span><span style="color: #339933;">,</span>locality=<span style="color: blue;">"locality"</span><span style="color: #009900;">)</span> </span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#toma la lista de las especies y su ocurrencias, y las convierte en una matriz de especies (filas) y localidades (columnas).</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;">
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#matriz de abundancia</span>
abundancias=create.matrix<span style="color: #009900;">(</span>fdata.list<span style="color: #339933;">,</span>tax.name=<span style="color: blue;">"species"</span><span style="color: #339933;">,</span>locality=<span style="color: blue;">"locality"</span><span style="color: #339933;">,</span> abund=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>abund.col=<span style="color: blue;">"abundance"</span><span style="color: #009900;">)</span> </pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#crea una matriz de abundancias, se trata de la misma función anterior pero ahora incluye la opción abund=TRUE y el argumento abund.col que corresponde a la columna de los valores de abundancia.</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#para este ejemplo, el resultado es el mismo que data(fdaa.mat)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;">
<span class="Apple-style-span" style="color: #666666; font-style: italic;">#matriz de coordenadas</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;">coordenadas=create.lats<span style="color: #009900;">(</span>fdata.list<span style="color: #339933;">,</span>loc=<span style="color: blue;">"locality"</span><span style="color: #339933;">,</span>long=<span style="color: blue;">"longitude"</span><span style="color: #339933;">,</span> lat=<span style="color: blue;">"latitude"</span><span style="color: #009900;">)</span> </pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#extrae las coordenadas de los sitios, eliminando aquellas entradas que se repiten.</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;"><span class="Apple-style-span" style="color: black; font-family: Times; font-style: normal; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;">
</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;">#La función dino.dist() toma una matrix de ocurrencia de especies vs. localidad (o grupos) y devuelve una matriz de distancia pareadas.</span></span></pre>
<div>
<span class="Apple-style-span"><span style="color: #666666; font-style: italic;">
</span></span></div>
</span><span class="Apple-style-span" style="color: black; font-family: Times; font-style: normal; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;">##matriz de distancia entre cualquier número de localidades</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"> <span style="color: #666666; font-style: italic;">#ecol.dist(x, method = sorenson, type = "dis") </span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;"> #dino.dist(x, method = sorenson, type = "dis")</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
ecol.dist<span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
ecol.dist<span style="color: #009900;">(</span>fdata.mat<span style="color: #339933;">,</span>simpson<span style="color: #339933;">,</span><span style="color: blue;">"sim"</span><span style="color: #009900;">)</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #009900;">
</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #009900;">
</span></span></pre>
</span></pre>
</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;">#gráfico
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
fdata.dist=dino.dist<span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
fdata.mst=dino.mst<span style="color: #009900;">(</span>fdata.dist<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.lats<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/maps">maps</a><span style="color: #009900;">)</span>
map<span style="color: #009900;">(</span><span style="color: blue;">"state"</span><span style="color: #009900;">)</span>
mstlines<span style="color: #009900;">(</span>fdata.mst<span style="color: #339933;">,</span>coordinates<span style="color: #009900;">(</span>fdata.lats<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/points"><span style="color: #003399; font-weight: bold;">points</span></a><span style="color: #009900;">(</span>coordinates<span style="color: #009900;">(</span>fdata.lats<span style="color: #009900;">)</span><span style="color: #339933;">,</span>pch=<span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"white"</span><span style="color: #339933;">,</span>cex=<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/points"><span style="color: #003399; font-weight: bold;">points</span></a><span style="color: #009900;">(</span>coordinates<span style="color: #009900;">(</span>fdata.lats<span style="color: #009900;">)</span><span style="color: #339933;">,</span>pch=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>cex=<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span>coordinates<span style="color: #009900;">(</span>fdata.lats<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<span style="color: black; font-weight: bold;">LETTERS</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">12</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="color: #666666;"><i>
</i></span></pre>
</span>
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">############ 2. Índices de distancia/similitud/diversidad beta</span></pre>
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;">#Coefficient Name Formulae Alternate Name Function Call</span>
<span style="color: #666666; font-style: italic;">#Jaccard a/(a+b+c) Coefficient of Community jaccard()</span>
<span style="color: #666666; font-style: italic;">#Sorenson 2a/(2a+b+c) Dice, Czekanowski, Coincidence Inde sorenson()</span>
<span style="color: #666666; font-style: italic;">#Simpson a/(a+min(b,c)) - simpson()</span>
<span style="color: #666666; font-style: italic;">#Braun-Blanquet a/(a+max(b,c)) - braun.blanquet()</span>
<span style="color: #666666; font-style: italic;">#Ochiai a/sqrt{(a+b)(a+c)} Coefficient of Closeness ochiai()</span>
<span style="color: #666666; font-style: italic;">#Kulczynski [a/(a+b)+a/(a+c)]/2 - kulczynski()</span>
<span style="color: #666666; font-style: italic;">#otros:</span>
braun.blanquet<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
bray.curtis<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
euclidean<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
kulczynski<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
jaccard<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
manhattan<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
morisita.horn<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
ochiai<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
simpson<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
sorenson<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Todas las funciones de similitud se utilizan de la misma manera. Las funciones necesitan dos argumentos que representen las dos muestras. Es importante que las ocurrencias de especies estén colocadas de misma manera para cada sitio, y que cada especie ausente se represente por el cero.</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"> sampleA=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
sampleB=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
sorenson<span style="color: #009900;">(</span>sampleA<span style="color: #339933;">,</span>sampleB<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">##Conteo de especies de 2 localizaciones diferentes.</span>
a = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span>
b = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">)</span>
bray.curtis<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
jaccard<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
simpson<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
sorenson<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
morisita.horn<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
<span class="Apple-style-span" style="color: #666666;"><i>
</i></span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><span class="Apple-style-span" style="color: #666666;"><i>
</i></span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><span class="Apple-style-span" style="color: #666666; font-style: italic;">##### 3. Métodos con muestras complejas (incompletas) o pequeñas: aleatorización, remuestreo, boostrap.</span></span> </pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/head"><span style="color: #003399; font-weight: bold;">head</span></a><span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#es nuestra matriz de "abundancias"</span>
chao1<span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
jack1<span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## con aleatorización</span>
<span style="color: #666666; font-style: italic;">#spp.est(). La función tiene varias opciones: el número de aleatorizaciones y si utilizar o no datos de abundancias. Calcula la curva de rareza de Chao</span></span><span class="Apple-style-span" style="color: #666666; font-style: italic;">, Coverage Estimators y Jacknife, así como las desviaciones estándar para todas las estimaciones. Por defecto la función corre 10 aleatorizaciones de los datos, sin embargo si queremos mejores estimaciones tendremos que aumentar dicho número.</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;">e run.</span>
<span style="color: #666666; font-style: italic;">#ejemplo con datos de abundancia </span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
spp.est<span style="color: #009900;">(</span>fdata.mat<span style="color: #339933;">,</span> abund = <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> counter = <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#ejemplo con datos de ocurrencia</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>fdata.mat<span style="color: #009900;">)</span>
spp.est<span style="color: #009900;">(</span>fdata.mat<span style="color: #339933;">,</span> abund = <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span> counter = <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## los estimators de Chao-Jaccard y Chao-Sorenson como estimadores de las especies cuando tenemos datos incompletos</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"> <span style="color: #666666; font-style: italic;">#chao.sorenson(x, y) chao.jaccard(x, y)</span>
<span style="color: #666666; font-style: italic;">#se deben datos dos vectores separados, con las especies dispuestas con el mismo orden, desde el área A a la B. Si la especie está presente en uno de los sitios pero no en el otro, debe ser identificada en ambos sitio; en el sitio donde no se encontró se codificará con un cero. Especies que no estén presentes en ninguno de los dos sitios serán ignoradas.</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"> <span style="color: #666666; font-style: italic;">##conteo de especies en 2 localizaciones diferentes</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"> a = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span>
b = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">)</span>
chao.sorenson<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
chao.jaccard<span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## Método de Bootstrap en estimadores de riqueza de especies, para datos de abundancia o para datos de presencia-ausencia.</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><span style="color: #666666;"></span>
<span style="color: #666666; font-style: italic;">## vector muestra</span>
a=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">45</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>samples=<span style="color: #cc66cc;">45</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## formato matricial</span>
a=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">45</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">(</span>a<span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">(</span>a<span style="color: #339933;">,,</span><span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## matriz de presencia-ausencia</span>
a=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">(</span>a<span style="color: #339933;">,,</span><span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## matiz de abundancia</span>
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">(</span>fdata.mat<span style="color: #339933;">,,</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>samples=<span style="color: #cc66cc;">1000</span><span style="color: #009900;">)</span>
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">############## con la librería vegan ###################</span>
</span><span class="Apple-style-span" style="color: #666666; font-style: italic;">###########################################################################</span></pre>
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br /><br />#Vamos a utilizar unos datos clásicos de la diversidad de árboles de Barro Colorado. Los datos consisten de 50 parcelas (filas) de 1 hectárea donde se realiza el conteo de los árboles en cada parcela y la identificación de su especie (columnas). Se incluyeron todos los árboles con diámetros por encima de 10cm. Se encontraron 225 especies.</span><br />
<div>
<a href="http://inside-r.org/r-doc/base/library">library</a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/vegan">vegan</a><span style="color: #009900;">)</span> </div>
<div>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>BCI<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Barro Colorado Island tree data</span>
<span style="color: #666666; font-style: italic;"># 50 1-ha plots, counts of 225 spp.</span></div>
<pre class="r geshifilter-R" style="font-family: monospace;">View(BCI) #para ver los datos</pre>
<pre class="r geshifilter-R" style="font-family: monospace;">fix(BCI)
<span style="color: #666666; font-style: italic;">### Riqueza específica: índices</span></pre>
<pre class="r geshifilter-R"><a href="http://inside-r.org/packages/cran/cts" style="font-family: monospace;">cts</a>= <a href="http://inside-r.org/r-doc/base/c" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">20</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">12</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">5</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">3</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">0</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">0</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">0</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># counts for 10 spp</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#1. Sin utilizar vegan</span>
<a href="http://inside-r.org/r-doc/base/sum" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900; font-family: monospace;">(</span>cts><span style="color: #cc66cc; font-family: monospace;">0</span><span style="color: #009900; font-family: monospace;">)</span> #<span style="color: #cc66cc; font-family: monospace;">7</span>:<span style="color: #666666; font-family: monospace; font-style: italic;"> sums a logical vector: T=1,F=0</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#2. Utilizando vegan</span>
specnumber<span style="color: #009900; font-family: monospace;">(</span>BCI<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># función para encontrar el número de especies, trabaja con matrices</span>
specnumber<span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/packages/cran/cts" style="font-family: monospace;">cts</a><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># error</span>
specnumber<span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/base/matrix" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/packages/cran/cts" style="font-family: monospace;">cts</a><span style="color: #339933; font-family: monospace;">,</span><a href="http://inside-r.org/r-doc/base/nrow" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">nrow</span></a>=<span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#Diversidad: otras métricas. Función diversity()</span>
diversity<span style="color: #009900; font-family: monospace;">(</span>BCI<span style="color: #339933; font-family: monospace;">,</span> index="shannon"<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># Shannon-Wiener, H</span>
diversity<span style="color: #009900; font-family: monospace;">(</span>BCI<span style="color: #339933; font-family: monospace;">,</span> index="simpson"<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># Simpson, 1-D</span>
diversity<span style="color: #009900; font-family: monospace;">(</span>BCI<span style="color: #339933; font-family: monospace;">,</span> index="invsimpson"<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># Inverse Simpson, 1/D</span>
fisher.alpha<span style="color: #009900; font-family: monospace;">(</span>BCI<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># parameter alpha of the log-series</span>
Vegan no tiene índices de equitatividad o igualdad, peru podemos calcular el más común de ellos, el índice de Pielou: J = H′/ log(S) de una manera sencilla
J <- H/log(specnumber(BCI))
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;"># Rarefacción:</span>
<span style="color: #666666; font-style: italic;"># min N in BCI data is 340</span>
bci.340 = rarefy<span style="color: #009900;">(</span>BCI<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a>=<span style="color: #cc66cc;">340</span><span style="color: #339933;">,</span> se=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>bci.340<span style="color: #009900;">)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#Curvas de rarefacción.</span>
<span style="color: #666666; font-style: italic;">#? Based on rarefying to many different subsample sizes.</span>
<span style="color: #666666; font-style: italic;">#? rarefy() can only take a single value for sample.</span>
<span style="color: #666666; font-style: italic;">#? Can call it lots of times, and collect the data: tedious!</span>
<span style="color: #666666; font-style: italic;">#? In Session 4, we?ll make our own function to do this.</span>
</pre>
</div>
</div>
<br />
<h4>
También existen las funciones:</h4>
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse; font-family: CMSS10; font-size: 16px;">specaccum #</span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span style="font-family: CMSS10; font-size: 12pt;">Species accumulation </span></span><br />
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span style="font-family: CMSS10; font-size: 12pt;">renyiaccum; </span></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span style="font-family: CMTT10; font-size: 12pt;">tsallisaccum </span></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span class="Apple-style-span" style="font-family: CMSS10; font-size: small;">#diversity accumulation </span></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span style="font-family: CMSS10; font-size: 12pt;">fisherfit; </span><span style="font-family: CMTT10; font-size: 12pt;">prestonfit; radfit #</span></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span class="Apple-style-span" style="font-family: CMSS10; font-size: small;">Species abundance models </span></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span style="font-family: CMSS10; font-size: 12pt;">specpool, </span><span style="font-family: CMTT10; font-size: 12pt;">estimateR #</span> </span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span class="Apple-style-span" style="font-family: CMSS10; font-size: small;">Extrapolated species richness </span></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse; font-family: CMSS10; font-size: 16px;">beta diver; </span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse; font-family: CMSS10; font-size: 16px;">betadisper</span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse; font-family: CMSS10; font-size: 16px;"> #</span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span class="Apple-style-span" style="font-family: CMSS10; font-size: 16px;">Indices of betadiversity</span></span><br />
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse; font-family: CMSS10; font-size: 16px;">taxondive #</span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span class="Apple-style-span" style="font-family: CMSS10; font-size: 16px;">Taxonomic diversity</span></span><br />
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse; font-family: CMSS10; font-size: 16px;">treedive </span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; border-collapse: collapse;"><span class="Apple-style-span" style="font-family: CMSS10; font-size: 16px;">#one option of functional diversity </span></span><br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<span style="font-family: CMSS10; font-size: 12pt;">Null models and nestedness (oecosimu with support functions)</span></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<span style="font-family: CMSS10; font-size: 12pt;">Technical and advanced functions like dissimilarity indices (vegdist, </span><span style="font-family: CMTT10; font-size: 12pt;">designdist) </span><span style="font-family: CMSS10; font-size: 12pt;">and their extensions (step across)</span></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<span style="font-family: CMSS10; font-size: 12pt;"><br /></span></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<span style="font-family: CMSS10; font-size: 12pt;"><br /></span></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<span style="font-family: CMSS10; font-size: 12pt;"><br /></span></div>
<h4>
<span style="font-family: CMSS10; font-size: 12pt;">Bibliografía y material para profundizar:</span></h4>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://entomologia.rediris.es/sea/manytes/metodos.pdf</span></div>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://duncanjg.files.wordpress.com/2008/02/clasediversidad1.pdf</span></div>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://www.um.es/docencia/geobotanica/ficheros/practica2.pdf</span></div>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://prof.usb.ve/eklein/labeco2/biblio/Estructura%20de%20las%20comunidades.pdf</span></div>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://palaeo-electronica.org/2011_1/238/238.pdf</span></div>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://cran.r-project.org/web/packages/vegan/vignettes/diversity-vegan.pdf</span></div>
<div>
<span style="font-family: CMSS10; font-size: 12pt;">http://www.bio.utk.edu/fesin/msa2009/snowbeam4.pdf</span></div>
Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com3tag:blogger.com,1999:blog-6841022238556785612.post-76387225706058160712012-08-21T08:05:00.001-07:002012-08-21T08:11:59.668-07:00Clase 5: Remuestreo en R-project<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
</div>
<h2 style="text-align: center;">
Remuestreo</h2>
<div class="geshifilter">
<br /></div>
<div class="geshifilter">
Los métodos de remuestreo o submuestreo (i.e. <i>resampling</i>) evalúan los estadísticos en remuestras o submuestras obtenidas a partir de los datos originales, y mediante estos valores obtienen estimadores de lmedidas de exactitud o de la distribución muestral del estadístico.<span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px;"> </span></div>
<div class="geshifilter">
<span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px;"></span><br />
<div class="geshifilter" style="font-family: Times; font-size: medium;">
<span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px;"><br /></span></div>
<span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px;">
</span>
<div class="geshifilter" style="font-family: Times; font-size: medium;">
<span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px;">El remuestreo son métodos que permiten:</span></div>
<span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px;">
<div class="geshifilter" style="font-family: Times; font-size: medium;">
<ul>
<li>Estimar la precisión de muestras estadísticas (según su media, mediana, varinza, etc..).</li>
<li>Intercambiar marcadores de puntos de datos al realizar test de significación (test de permutación, tambien denominados tests exactos, tests de aleatoriedad, o pruebas de re-aleatoriedad)</li>
<li>Validar modelos para el uso de subconjuntos aleatorios (bootstrapping, validación cruzada)</li>
</ul>
</div>
</span></div>
<h4>
</h4>
<h4>
Tipos de remuestreo.</h4>
<div class="geshifilter">
Existen al menos 4 grupos principales de remuestreo:</div>
<div class="geshifilter">
<br />
<ol>
<li>aleatorización de pruebas exactas (<i>randomization exact test</i>)</li>
<li>validación cruzada (<i>cross-validation</i>): se usa para evaluar los resultados de un análisis estadístico y garantizar que son independientes de la partición entre datos de entrenamiento y prueba. Definida una muestra, se realizan particiones de las mismas y se evalúan los estadísticos para cada una de las particiones. El caso más sencillo consiste en tomar 2 particiones, una de entrenamiento y la otra de prueba.</li>
<li><i>jackknife</i>: para la estimación del sesgo y del error estándar de un estadístico. El nombre alude a las navajas, y más precisamente a aquellas multiuso, que sirven pa tó. Definida una muestra de observaciones de tamaño n, el método jacknife consiste en suprimir cada vez un conjunto de observaciones g y calcular sobre el conjunto (n-g) restante de datos el estadístico de interés. Una vez obtenidas las n estimaciones del estadístico para cada una de las muestras Jackknife, se puede calcular una estimación del sesgo asociado a la muestra original, así como una estimación no paramétrica del error estándar del estadístico.<br /> </li>
<li><i>boostrap</i>: su nombre refiere a su autosuficiencia, ya que significa “levantarse tirando hacia arriba de las propias correas de las botas”. El Bootstrap se basa en el remuestreo con reposición a partir de la muestra original para obtener nuevas muestras, calcular en cada una de ellas el estadístico deseado, y así obtener una distribución de valores para el estadístico en la que podemos calcular su dispersión (análogo del error estándar) y determinar unos límites de confianza utilizando esa distribución.</li>
</ol>
<h4>
<ol>Descripción y aplicación en R</ol>
</h4>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#### 1) Aleatorización de pruebas exactas o test de permutación (Randomization exact test or permutation test): </span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">NOTA: Also known as the permutation test, this test was developed by R. A. Fisher (1935/1960), the founder of classical statistical testing. However, in his later years Fisher lost interest in the permutation method because there were no computers in his days to automate such a laborious method.</span>
El propósito es simular artificialmente la aleatoriedad.
Los datos se re-asignan aleatoriamente <span style="color: #009900;">(</span>datos permutados<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> tal manera <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> obtener p-valores basados en estos nuevos datos.
EJ: evaluar la diferencia entre dos m?todos. Luego <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> evaluar cada conjunto <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> datos permutados<span style="color: #339933;">,</span> por ejemplo <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> el investigador puede poner juntos los t-valores y obtener un <span style="color: blue;">"p-valor exacto"</span> <span style="color: #009900;">(</span>la probabilidad <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> que la diferencia solo ocurra por azar<span style="color: #009900;">)</span>. Por ejemplo<span style="color: #339933;">,</span> si el t-valor <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> <span style="color: #cc66cc;">1.55</span> ocurre solo <span style="color: #cc66cc;">5</span> veces en las <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> el p-valor exacto ser?a <span style="color: #cc66cc;">.05</span>.
Problemas: d?bil ante tama?os <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> muestras peque?os.
<span style="color: #666666; font-style: italic;">#### 2) Cross-validation: </span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">NOTA: </span><span class="Apple-style-span" style="color: #666666; font-style: italic;">Simple cross-validation was proposed by Kurtz (1948) as a remedy for the Rorschach test, a form of personality test which was criticized by psychometricians for its lack of common psychometric attributes such as data normality. Based on Kurtz's simple cross-validation, Mosier (1951) developed double cross-validation, which was later extended to multicross-validation by Krus and Fuller (1982).</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"> El propósito es averiguar si los resultados son replicables o solo una cuesti?n <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> fluctuaciones aleatorias<span style="color: #339933;">,</span> es decir<span style="color: #339933;">,</span> verificar la replicabilidad <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> los resultados.
La muestra se divide aleatoriamente en <span style="color: #cc66cc;">2</span> o má<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> subconjuntos y los resultados <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la prueba <a href="http://inside-r.org/packages/cran/sn">sn</a> validados comparando las sub-muestras.
Varias categor?<a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a>: simple<span style="color: #339933;">,</span> doble o m?ltiple:
*Simple cross-validation. Take regression <a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> an example. In the process of implementing a simple cross-validation<span style="color: #339933;">,</span> the first sub-<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> usually used <span style="color: black; font-weight: bold;">for</span> deriving the regression equation <span style="color: black; font-weight: bold;">while</span> another sub-<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> used <span style="color: black; font-weight: bold;">for</span> generating predicted scores from the first regression equation. Next<span style="color: #339933;">,</span> the cross-validity coefficient <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> computed <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a> correlating the predicted scores and the observed scores on the outcome variable.
*Double cross-validation. Double cross-validation <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> a <a href="http://inside-r.org/r-doc/stats/step"><span style="color: #003399; font-weight: bold;">step</span></a> further than <a href="http://inside-r.org/packages/cran/its">its</a> simple counterpart. Take regression <a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> an <a href="http://inside-r.org/r-doc/utils/example"><span style="color: #003399; font-weight: bold;">example</span></a> again. In <a href="http://inside-r.org/r-doc/base/double"><span style="color: #003399; font-weight: bold;">double</span></a> cross-validation regression equations are generated <span style="color: black; font-weight: bold;">in</span> both sub-samples<span style="color: #339933;">,</span> and then both equations are used to generate predicted scores and cross-validity coefficients.
*Multicross-validation. Multicross-validation <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> an extension of <a href="http://inside-r.org/r-doc/base/double"><span style="color: #003399; font-weight: bold;">double</span></a> cross-validation. In this form of cross-validation<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/double"><span style="color: #003399; font-weight: bold;">double</span></a> cross-validation procedures are repeated many times <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a> randomly selecting sub-samples from the <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> set. In the context of regression analysis<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a> <a href="http://inside-r.org/r-doc/stats/weights"><span style="color: #003399; font-weight: bold;">weights</span></a> computed <span style="color: black; font-weight: bold;">in</span> each sub-<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> are used to <a href="http://inside-r.org/r-doc/stats/predict"><span style="color: #003399; font-weight: bold;">predict</span></a> the outcome variable <span style="color: black; font-weight: bold;">in</span> the corresponding sub-sample. Next<span style="color: #339933;">,</span> the observed and predicted scores of the outcome variable <span style="color: black; font-weight: bold;">in</span> each sub-<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> are used to compute the cross validated coefficient.
Problemas: d?bil ante tama?os <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> muestras peque?os. Ang <span style="color: #009900;">(</span><span style="color: #cc66cc;">1998</span><span style="color: #009900;">)</span> argued that cross-validation <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> problematic because splitting an already small <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> increases the risk that the <a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a> <a href="http://inside-r.org/r-doc/stats/weights"><span style="color: #003399; font-weight: bold;">weights</span></a> are just artifacts of the sub-sample. Thus<span style="color: #339933;">,</span> Ang endorsed use of Jackknife<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a> will be discussed next.
Es ?til cuando tenemos que elegir entre modelos <span style="color: #009900;">(</span>por ejemplo<span style="color: #339933;">,</span> anidados<span style="color: #009900;">)</span> ya que la validaci?n cruzada nos permite elegir el modelo que <span style="color: blue;">"mejor predice"</span> los valores<span style="color: #339933;">,</span> a diferencia <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> los m?todos <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> m?nimos cuadrados o m?xima verosimilitud que eligen los <a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a>?metros dentro <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> un conjunto <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> modelos que <span style="color: blue;">"mejor ajustan"</span> los valores.
NOTA: a pesar <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> que la validaci?n cruzada es ?til para elegir entre modelos<span style="color: #339933;">,</span> no tenemos idea <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> si la diferencia entre ellos es estad?sticamente significativa.
La manera m?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> f?cil <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> hacer validaci?n cruzada es dividir los datos en dos partes: el conjunto <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> entrenamiento <span style="color: #009900;">(</span>training set<span style="color: #009900;">)</span> que ser? usado para ajustar varios modelos a los datos<span style="color: #339933;">,</span> y el conjunto <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> prueba <span style="color: #009900;">(</span>test set<span style="color: #009900;">)</span> que se usar? para elegir entre modelos.
un inconveniente del m?todo anterior es que reduce el poder <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> los datos para ajustar un modelo ocultando algunas observaciones para validaci?n. Podemos minimizar esta p?rdida tomando un conjunto <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> entrenamiento <a href="http://inside-r.org/r-doc/base/tan"><span style="color: #003399; font-weight: bold;">tan</span></a> grande como sea posible<span style="color: #339933;">,</span> pero en tal caso no tendr?amos suficientes puntos para la validaci?n. Evitamos esto utilizando una estratgia <span style="color: blue;">"leave one out"</span><span style="color: #339933;">,</span> superficialmente similar al jackknife. <span style="color: #009900;">(</span>el jackknife trabaja con un modelo particular paara estimar un estimador <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> sesgo y varianza muestral<span style="color: #339933;">,</span> mientras que en la validaci?n cruzada estamos eligiendo entre modelos<span style="color: #009900;">)</span>.
Ejemplo: an?lisis <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> regersi?n
? Leaving out the ith pair of <a href="http://inside-r.org/r-doc/graphics/points"><span style="color: #003399; font-weight: bold;">points</span></a> <span style="color: #009900;">(</span>Xi<span style="color: #339933;">,</span> Yi<span style="color: #009900;">)</span><span style="color: #339933;">,</span> use least squares <a href="http://inside-r.org/r-doc/utils/methods"><span style="color: #003399; font-weight: bold;">methods</span></a> to <a href="http://inside-r.org/r-doc/utils/find"><span style="color: #003399; font-weight: bold;">find</span></a> parameter estimates ?a-i and ?b-i.
? Hence<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/find"><span style="color: #003399; font-weight: bold;">find</span></a> the best predictor of the left out Yi: ?Y-i = ?a-i + ?b-i*Xi
? Repeat the above <span style="color: black; font-weight: bold;">for</span> each point and calculate the cross validation score<span style="color: #339933;">,</span> or estimate predictive error <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/n<span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>Yi-?Y-i<span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span>
? If we also calculate the cross validation score <span style="color: black; font-weight: bold;">for</span> the <a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span>X<span style="color: #009900;">)</span> model we can again use predictive error <a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> a basis to <a href="http://inside-r.org/r-doc/base/choose"><span style="color: #003399; font-weight: bold;">choose</span></a> between the models.
<span style="color: #666666; font-style: italic;">#### 3) Jackknife: Also known as the Quenouille-Tukey Jackknife, this tool was invented by Maurice Quenouille (1949) and later developed by John W. Tukey (1958). As the father of EDA, John Tukey attempted to use Jackknife to explore how a model is influenced by subsets of observations when outliers are present. The name "Jackknife" was coined by Tukey to imply that the method is an all-purpose statistical tool.</span>
El prop?sito es detectar valores extremos y <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a>?mo cada sub-muestra afecta al modelo.
Se repite el mismo test dejando un sujetopor vez <span style="color: #009900;">(</span><span style="color: blue;">"leave one out"</span><span style="color: #009900;">)</span>. Este procedimiento es especialmente ?til cuando la dispersi?n <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la distribuci?n es amplia o existen valores extremos en el conjunto <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> datos. En estos casos se espera que el Jackknife retorne una estimaci?n <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> menor sesgo.
Se lo puede ver como una versi?n m?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> organizada o sistem?tica del bootstrap.
Algoritmo:
Given a <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> set of n observations we create n <a href="http://inside-r.org/r-doc/methods/new"><span style="color: #003399; font-weight: bold;">new</span></a> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> <a href="http://inside-r.org/packages/cran/sets">sets</a><span style="color: #339933;">,</span> each of size n - <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a> leaving out one point at a time.
? Suppose we have a <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> of observations X = <span style="color: #009900;">{</span>X1<span style="color: #339933;">,</span> . . . Xn<span style="color: #009900;">}</span> that we will use to estimate a parameter Theta.
? Suppose that T = T<span style="color: #009900;">(</span>X<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> an estimator of Theta.
? Draw n jackknife samples X-<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> X-<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> ....<span style="color: #339933;">,</span> X-n <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a> leaving out the first<span style="color: #339933;">,</span> second . . . nth observation respectively.
? Calculate T-i=T<span style="color: #009900;">(</span>X-i<span style="color: #009900;">)</span> <span style="color: black; font-weight: bold;">for</span> each i.
? Calculate T* =<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/n<span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>T-i<span style="color: #009900;">)</span>
? The jackknife estimate of variance <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> <span style="color: #009900;">(</span><span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>/n<span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>T-i-T*<span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span>
? And the jackknife estimate of bias <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> <span style="color: #009900;">(</span>n - <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">(</span>T*- T<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a> leads to a bias corrected estimate of nT - <span style="color: #009900;">(</span>n - <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>T* when we subtract this from T.
Ejemplo:
dat=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3.9140011</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.1819694</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2.5604846</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.3703330</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6.7950865</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4.0200424</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.660441</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># queremos estimar su media.</span>
jk = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>jk<span style="color: #009900;">)</span><span style="color: #009900;">)</span> jk<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">[</span>-i<span style="color: #009900;">]</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>jk<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#[1] 2.643194</span>
<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>jk<span style="color: #009900;">)</span>-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>/length<span style="color: #009900;">(</span>jk<span style="color: #009900;">)</span> * <a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>jk-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>jk<span style="color: #009900;">)</span><span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># [1] 0.855013</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#[1] 2.643194, observemos que la media de la muestra jackknife es igual a la media de los datos originales.</span>
<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span>/length<span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#[1] 0.855013, observemos que la estimaci?n usual de la varianza (insesgada) de la media muestra es la misma que la del estimados jackknife de la varianza. Sin embargo, la estimaci?n bootstrap de la varianza de ma media muestral conveerge al MLE (sesgado).</span>
<span style="color: #666666; font-style: italic;"># f?rmula general de jackknife</span>
jackknife=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>T<span style="color: #339933;">,</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
n = <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
jk = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>n<span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:n<span style="color: #009900;">)</span>
jk<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = T<span style="color: #009900;">(</span>x<span style="color: #009900;">[</span>-i<span style="color: #009900;">]</span><span style="color: #009900;">)</span>
Tstar = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>jk<span style="color: #009900;">)</span>
jkvar = <span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>/n * <a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>jk-Tstar<span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a> = jkvar<span style="color: #339933;">,</span> bias = <span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>*<span style="color: #009900;">(</span>Tstar - T<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> corrected = n*T<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>-<span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>*Tstar<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
salida=jackknife<span style="color: #009900;">(</span>T=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>x=dat<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># la estimaci?n de la varianza por jackknife puede utilizarse para realizar intervalos de confianza y test de hip?tesis, tal como se realiza con la manera usual de estimar la varianza.</span>
<span style="color: #666666; font-style: italic;">#### 4) Bootstrap: This technique was invented by Bradley Efron (1979, 1981, 1982) and further developed by Efron and Tibshirani (1993). "Bootstrap" means that one available sample gives rise to many others by resampling (a concept reminiscent of pulling yourself up by your own bootstrap). While the original objective of cross-validation is to verify replicability of results and that of Jackknife is to detect outliers, Efron (1981, 1982) developed bootstrap with inferential purposes.</span>
El prop?sito es la inferencia.
Genera resultados con menor sesgo y m?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> consistentes<span style="color: #339933;">,</span> respecto al Jackknife <span style="color: #009900;">(</span>Fan & Wang<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1996</span><span style="color: #009900;">)</span>.
Comparado con la <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>?cnica <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> Jackknife<span style="color: #339933;">,</span> la estrategia <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> remuestreo del Bootstrao es m?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> rigurosa en <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>?rminos <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la magnitud <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la replicaci?n. En Jackknife<span style="color: #339933;">,</span> el n?mero <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> remuestreos est? limitado por el n?mero <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> observaciones <span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> pero en <a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #339933;">,</span> la muestra original puede ser duplicada tantas veces como que quiera y por tanto esta muestra expandida se trata como una poblaci?n virtual. Las muestras se obtienen <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> esta poblaci?n para veerificar los estimadores.
A diferencia <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la validaci?n cruzada o el Jackknife<span style="color: #339933;">,</span> el <a href="http://inside-r.org/packages/cran/bootstrap">Bootstrap</a> emplea un muestreo con reemplazamiento<span style="color: #339933;">,</span> que -en <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>?rminos <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> simulaci?n <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> aleatoriedad- es m?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> adecuado que el muestreo <a href="http://inside-r.org/r-doc/base/sin"><span style="color: #003399; font-weight: bold;">sin</span></a> reemplazo.
A diferencia <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la validaci?n cruzada o el Jackknife<span style="color: #339933;">,</span> donde el n <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> las submuestras es menor que el <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la muestra original<span style="color: #339933;">,</span> en <a href="http://inside-r.org/packages/cran/bootstrap">Bootstrap</a> cada remuestreo tiene el mismo n?mero <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> observacinones que la muestra original. Por tanto<span style="color: #339933;">,</span> tiene la ventaja <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> modelar los impactos del tama?o <a href="http://inside-r.org/r-doc/base/real"><span style="color: #003399; font-weight: bold;">real</span></a> <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la muestra <span style="color: #009900;">(</span>Fan & Wang<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1996</span><span style="color: #009900;">)</span>.
Algoritmo:
? Suppose we have a <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> of observations X = <span style="color: #009900;">{</span>X1<span style="color: #339933;">,</span> . . . Xn<span style="color: #009900;">}</span> that we want to use to estimate a parameter Theta.
? Suppose also that we have derived T<span style="color: #009900;">(</span>X<span style="color: #009900;">)</span><span style="color: #339933;">,</span> an estimator of Theta<span style="color: #339933;">,</span> and want to evaluate <a href="http://inside-r.org/packages/cran/its">its</a> <a href="http://inside-r.org/packages/cran/sampling">sampling</a> variance.
? Draw a <a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a> <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a> X1 of size n <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a> <a href="http://inside-r.org/packages/cran/sampling">sampling</a> <a href="http://inside-r.org/r-doc/base/with"><span style="color: #003399; font-weight: bold;">with</span></a> replacement from X.
? Calculate T1=T<span style="color: #009900;">(</span>X1<span style="color: #009900;">)</span>
? Repeat the resampling b times to <a href="http://inside-r.org/r-doc/base/get"><span style="color: #003399; font-weight: bold;">get</span></a> T1<span style="color: #339933;">,</span> T2<span style="color: #339933;">,</span> ...<span style="color: #339933;">,</span> Tb
? Calculate the <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> of the bootstrapped statistics meanT=<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/b<span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>Ti<span style="color: #009900;">)</span>
? Finally<span style="color: #339933;">,</span> calculate the <a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a> estimate of variance <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/<span style="color: #009900;">(</span>b-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>Ti-meanT<span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span>
Ejemplo:
dat=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3.9140011</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.1819694</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2.5604846</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.3703330</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6.7950865</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4.0200424</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.660441</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#sea el conjunto de datos dat, queremos usarlo para estimar la media de la poblaci?n de la que proviene</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#para evaluar la varianza de esta estimaci?n, podemos comenzar tomando una muestra bootstrap y calculando la media en ella.</span>
boot1=<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>dat<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span>
boot1
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>boot1<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># esto nos da T1=2.461595</span>
<span style="color: #666666; font-style: italic;"># si lo ponemos en un loop podemos generar una muestra completa de bootstrap y evaluar la varianza.</span>
<a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a>=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>dat<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># mi estimaci?n de la varianza muestra del estimador es 0.7376547</span>
<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>dat-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span><span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> / <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span> / <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>dat<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># la comparamos con la forma usual de obtener la varianza muestral</span>
<span style="color: #666666; font-style: italic;">#cuantas muestras tomar depende en qu? tan r?pido queramos calcular nuestras estimaciones.</span>
<span style="color: #666666; font-style: italic;"># f?rmula general de bootsrap</span>
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a>=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>T<span style="color: #339933;">,</span>x<span style="color: #339933;">,</span>b=<span style="color: #cc66cc;">10000</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a> = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:b<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = T<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/splines/bs"><span style="color: #003399; font-weight: bold;">bs</span></a>
<span style="color: #009900;">}</span>
salida=<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">(</span>T=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>x=dat<span style="color: #339933;">,</span>b=<span style="color: #cc66cc;">10000</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#mi estimaci?n de la varianza muestra del estimador es 0.7312076</span>
<span style="color: #666666; font-style: italic;"># la estimaci?n de la varianza por bootstrap puede utilizarse para realizar intervalos de confianza y test de hip?tesis, tal como se realiza con la manera usual de estimar la varianza.</span>
<span style="color: #666666; font-style: italic;">## Variantes del bootstrap</span>
<span style="color: #666666; font-style: italic;"># i) smoothed bootstrap. Se agrega a cada remuestreo una cantidad comparativamente peque?a de valores generados aleatoriamente. Esto tiene el mismo efecto que ajustar una estimaci?n del n?cle (kernel) de densidad a los datos, y remuestrar en ellos.</span>
<span style="color: #666666; font-style: italic;"># ii) parametric bootstrap. Se ajusta una densidad param?trica a los datos observados y se realiza el remuestreo en ella.</span>
<span style="color: #666666; font-style: italic;"># iii) wild bootstrap. Para datos distribuidos sim?tricamente respecto a 0. Cada valor remuestrado se multiplica por -1 con una probabilidad 0.5 antes de ser usado.</span>
<span style="color: #666666; font-style: italic;"># iv) balanced bootstrap. concatenamos b copias del conjunto original de datos en un vector y permutamso aleatoriamente en ?l. Los primeros n elementos se vuelven la primer muestra bootstrap, los degundos n elementos la segunda muestra y as? sucesivamente.</span>
<span style="color: #666666; font-style: italic;">#### 6) Markov chain Monte Carlo or MCMC (o Metropolis & Hastings samplers)</span>
Dada una distribuci?n phi en un n?mero finito <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> estados<span style="color: #339933;">,</span> podemos construir alg?n proceso equivalente a una cadena <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> Markov irreducible P <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> tal manera que la distribuci?n erg?dica <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> la cadena sea phi.
mirar tambi?n: http://cran.r-project.org/web/packages/mcmc/vignettes/demo.pdf
Mirar en: http://www.iiap.res.in/astrostat/School07/R/MCMC.html
A toy <a href="http://inside-r.org/r-doc/utils/example"><span style="color: #003399; font-weight: bold;">example</span></a> to calculate P<span style="color: #009900;">(</span>-<span style="color: #cc66cc;">1</span> < X < <span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> when X <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> a Normal<span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <a href="http://inside-r.org/packages/cran/random">random</a> variable:
xs = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># simulate 10,000 draws from N(0,1)</span>
xcount = <a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>xs>-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> & <span style="color: #009900;">(</span>xs<<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># count number of draws between -1 and 0</span>
xcount/<span style="color: #cc66cc;">10000</span> <span style="color: #666666; font-style: italic;"># Monte Carlo estimate of probability</span>
<a href="http://inside-r.org/r-doc/stats/pnorm"><span style="color: #003399; font-weight: bold;">pnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>-<a href="http://inside-r.org/r-doc/stats/pnorm"><span style="color: #003399; font-weight: bold;">pnorm</span></a><span style="color: #009900;">(</span>-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Compare it to R's answer (cdf at 0) - (cdf at -1)</span>
<span style="color: #666666; font-style: italic;">#i) Metropolis sampling</span>
Implica una etapa <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> proposici?n y una etapa <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> aceptaci?n<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> tal manera que para movernos desde el estado i al estado j debemos primero proponer el movimiento y luego aceptar el movimiento.
Algoritmo:
? Proposals are generated according to a symmetric transition <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a> Q<span style="color: #339933;">,</span> so that Qi<span style="color: #339933;">,</span>j = Qj<span style="color: #339933;">,</span>i <span style="color: #339933;">,</span>or the probability of proposing a move to <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> j from <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> i <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> the same <a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> proposing a move to <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> i from <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> j.
? A proposed move from <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> i to <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> j <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> accepted <a href="http://inside-r.org/r-doc/base/with"><span style="color: #003399; font-weight: bold;">with</span></a> probability <a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>phij/phii<span style="color: #009900;">)</span>
? If the proposal <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> accepted we move to <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> j<span style="color: #339933;">,</span> and <span style="color: black; font-weight: bold;">if</span> it <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> rejected<span style="color: #339933;">,</span> we stay <span style="color: black; font-weight: bold;">in</span> <a href="http://inside-r.org/r-doc/datasets/state"><span style="color: #003399; font-weight: bold;">state</span></a> i.
para correr esta cadena solo neceistamos definir el mecanismo <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> propuesta<span style="color: #339933;">,</span> que no depende <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> phi<span style="color: #339933;">,</span> y luego evaluar la propuesta utilizando la proporci?n phij/phii. Note que no necesitariamente tenemos que conocer Qi<span style="color: #339933;">,</span>j<span style="color: #339933;">,</span> solo necesitamos conocer que Q sea sim?trica.
Note que para muestras v?lidas <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> phi<span style="color: #339933;">,</span> la cadena <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> markov debe ser irreducible.
Ejemplo: implementar Metropolis sampler dado un <a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a> <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> probabilidades phi.
f.metro=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">pi</span><span style="color: #339933;">,</span>n=<span style="color: #cc66cc;">10000</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> = <span style="color: #cc66cc;">1</span>:<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">pi</span><span style="color: #009900;">)</span>
x = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>n<span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">2</span>:n<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> < <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">)</span>
z = x<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> - <span style="color: #cc66cc;">1</span>
<span style="color: black; font-weight: bold;">else</span>
z = x<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> + <span style="color: #cc66cc;">1</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span>z < <span style="color: #cc66cc;">1</span> | z > <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
a = <span style="color: #cc66cc;">0</span>
<span style="color: black; font-weight: bold;">else</span>
a = <a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">pi</span><span style="color: #009900;">[</span>z<span style="color: #009900;">]</span>/pi<span style="color: #009900;">[</span>x<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> < a<span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = z
<span style="color: black; font-weight: bold;">else</span>
x<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = x<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
<span style="color: #009900;">}</span>
x
<span style="color: #009900;">}</span>
f.runmc<span style="color: #009900;">(</span>P<span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">pi</span>=
z=f.metro<span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">pi</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>/length<span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#z</span>
<span style="color: #666666; font-style: italic;">#1 2 3 4 5 6</span>
<span style="color: #666666; font-style: italic;">#0.20470 0.18060 0.04159 0.04179 0.12459 0.40673</span>
<span style="color: #666666; font-style: italic;"># esta salida concuerda bastante bien con el vector phi de entrada.</span>
<span style="color: #666666; font-style: italic;"># From Robert & Casells: A Metropolis{Hastings sample is then generated with the following R code:</span>
a=<span style="color: #cc66cc;">2.7</span><span style="color: #339933;">;</span> b=<span style="color: #cc66cc;">6.3</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a>=<span style="color: #cc66cc;">2.669</span> <span style="color: #666666; font-style: italic;"># initial values</span>
Nsim=<span style="color: #cc66cc;">5000</span>
X=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>Nsim<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># initialize the chain</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">2</span>:Nsim<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
Y=<a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
rho=<a href="http://inside-r.org/r-doc/stats/dbeta"><span style="color: #003399; font-weight: bold;">dbeta</span></a><span style="color: #009900;">(</span>Y<span style="color: #339933;">,</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>/dbeta<span style="color: #009900;">(</span>X<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">)</span>
X<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>=X<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> + <span style="color: #009900;">(</span>Y-X<span style="color: #009900;">[</span>i-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>*<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><rho<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#i) Hasting?s sampling</span>
Este m?todo generaliza el m?todo <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> Metropolis al caso en el cual Q no es sim?trico.
Tiene la misma estructura <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> propuesta y aceptaci?n<span style="color: #339933;">,</span> pero la probabilidad <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> aceptaci?n viene dada por <a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">(</span>phij*Qj<span style="color: #339933;">,</span>i<span style="color: #009900;">)</span>/<span style="color: #009900;">(</span>phii*Qi<span style="color: #339933;">,</span>j<span style="color: #009900;">)</span><span style="color: #009900;">)</span>.
Para realizar este muestreo necesitamos conocer m?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a> expl?citamente en detalle la matriz Q<span style="color: #339933;">,</span> especificamente necesitamos la proporci?n Qi<span style="color: #339933;">,</span>j/Qj<span style="color: #339933;">,</span>i.
<span style="color: #666666; font-style: italic;">### M?s ejemplos:</span>
http://www.mayin.org/ajayshah/KB/R/documents/boot.html
http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-bootstrapping.pdf
http://www.statmethods.net/advstats/bootstrapping.html
http://cran.r-project.org/doc/Rnews/Rnews_2002-<span style="color: #cc66cc;">3</span>.pdf</pre>
</div>
</div>
<br />
<br />
<br />
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">##### Types of resampling:</span>
<span style="color: #666666; font-style: italic;"># bibliograf?a:</span>
http://pareonline.net/getvn.asp?v=<span style="color: #cc66cc;">8</span>&n=<span style="color: #cc66cc;">19</span>
http://episun7.med.utah.edu/~alun/teach/compstats/notes.pdf
http://cran.r-project.org/web/packages/mcmc/vignettes/demo.pdf
</pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"></pre>
<h3>
</h3>
<h3>
</h3>
<h3>
</h3>
<h3>
EJEMPLOS</h3>
<div>
<br /></div>
<div>
<br /></div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;">#### Resample data</span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;">## Basado en http://www.zoology.ubc.ca/~schluter/zoo502stats/Rtips.resample.html</span>
<span style="color: #666666; font-style: italic;">#Let's assume that the data are a sample of measurements for a single variable stored in a vector "x". The data may be numeric or categorical. </span>
<span style="color: #666666; font-style: italic;">#A single bootstrap replicate is obtained as follows. The replace option is used to indicate that sampling is carried out with replacement.</span>
xboot = <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Calculate the statistic of interest (for example, the mean) on the resampled data in "xboot" and store the result in a vector created for this purpose.</span>
z = <a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># initialize z (do only once)</span>
z<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>xboot<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># first bootstrap replicate estimate</span>
<span style="color: #666666; font-style: italic;">#Repeat steps (1) and (2) many times. The result will be a large collection of bootstrap replicate estimates for subsequent analysis.</span>
<span style="color: #666666; font-style: italic;">#In other cases, two or more variables are measured on individuals (e.g., stem height, leaf area, petal diameter, etc). Assume that each row of a data frame "mydata" is a different individual, and each column a different variable. </span>
<span style="color: #666666; font-style: italic;">#To resample individuals (i.e., rows), </span>
iboot = <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a><span style="color: #009900;">(</span>mydata<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
bootdata = mydata<span style="color: #009900;">[</span>iboot<span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">#The data frame "bootdata" will contain a single bootstrap replicate including all the variables.</span>
<span style="color: #666666; font-style: italic;">#Calculate the statistic of interest on the resampled data and store the result in vector created for this purpose. For example, to calculate the correlation between two variables x and y in "bootdata", </span>
z = <a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># initialize z (do only once)</span>
z<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>bootdata$x<span style="color: #339933;">,</span> bootdata$y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Repeat steps (1) and (2) many times. The result will be a large collection of bootstrap replicate estimates for subsequent analysis. </span>
<span style="color: #666666; font-style: italic;"># Basado en http://www.zoology.ubc.ca/~schluter/zoo502stats/Rtips.resample.html</span>
<span style="color: #666666; font-style: italic;">### Randomization test</span>
<span style="color: #666666; font-style: italic;">#A randomization test uses resampling and the computer to generate a null distribution for a test statistic. The test statistic is a measure of association between two variables or difference between groups. Each randomization step involves randomly resampling without replacement the values of one of the two variables in the data. The two variables may be categorical (character or factor), numeric, or one of each.</span>
<span style="color: #666666; font-style: italic;">## Both variables categorical</span>
<span style="color: #666666; font-style: italic;">#R has a built-in randomization procedure for a contingency test of association when both of two variables are categorical (call them A1 and A2). To apply it, execute the usual command for the ?2 contingency test, but set the "simulate.p.value" option to TRUE. The number of replicates in the randomization is set by the option "B" (default is 2000). Each randomization rearranges the values in the contingency table while keeping all the row and column totals fixed to their observed values.</span>
<a href="http://inside-r.org/r-doc/stats/chisq.test"><span style="color: #003399; font-weight: bold;">chisq.test</span></a><span style="color: #009900;">(</span>A1<span style="color: #339933;">,</span> A2<span style="color: #339933;">,</span> simulate.p.value = <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> B = <span style="color: #cc66cc;">5000</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## Data are numeric</span>
<span style="color: #666666; font-style: italic;">#If one or both of the variables is numeric, then you will need to create a short loop to carry out the resampling necessary for the randomization test. Choose one of the two variables to resample (call it "x"). It doesn't matter which of the two variables you choose. Keep the other variable (call it "y") unchanged (there is no benefit to resampling both variables). </span>
<span style="color: #666666; font-style: italic;">#Resample "x" without replacement to create a new vector (call it x1).</span>
x1 = <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a> = <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Calculate the test statistic to measure association between y and the randomized variable x1. Store the result in a vector created for this purpose. For example, to calculate the correlation between the two variables,</span>
z = <a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># initialize z (do only once)</span>
z<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>x1<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># first randomization result</span>
<span style="color: #666666; font-style: italic;">#Repeat steps (1) and (2) many times. The result will be a large collection of replicates representing the null distribution of your test statistic. </span>
<span style="color: #666666; font-style: italic;">#Use this null distribution to calculate an approximate P-value for your test. This involves calculating the proportion of values in the null distribution that are as extreme or more extreme than the observed value of the test statistic (calculated from the real data). Whether this calculation must take account of just one or both tails of the null distribution depends on your test statistic.</span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;">### Permutation test: randomization test for small size samples</span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;">## Basado en http://www.burns-stat.com/pages/Tutor/bootstrap_resampling.html</span>
<span style="color: #666666; font-style: italic;"># The idea: Permutation tests are restricted to the case where the null hypothesis really is null -- that is, that there is no effect. If changing the order of the data destroys the effect (whatever it is), then a random permutation test can be done. The test checks if the statistic with the actual data is unusual relative to the distribution of the statistic for permuted data. </span>
<span style="color: #666666; font-style: italic;"># Our example permutation test is to test volatility clustering of the S&P returns. Below is an R function that computes the statistic for Engle's ARCH test. </span>
engle.arch.test = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a> <span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a>=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
xsq = x^<span style="color: #cc66cc;">2</span>
nobs = <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
inds = <a href="http://inside-r.org/r-doc/base/outer"><span style="color: #003399; font-weight: bold;">outer</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span>:<span style="color: #009900;">(</span>nobs - <a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a> - <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a>:<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: blue;">"+"</span><span style="color: #009900;">)</span>
xmat = xsq<span style="color: #009900;">[</span>inds<span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>xmat<span style="color: #009900;">)</span> = <a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>inds<span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/XReg">xreg</a> = <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">(</span>xsq<span style="color: #009900;">[</span>-<span style="color: #cc66cc;">1</span>:-<a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">]</span> ~ xmat<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/XReg">xreg</a><span style="color: #009900;">)</span>$r.squared * <span style="color: #009900;">(</span>nobs - <a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># All you need to know is that the function returns the test statistic and that a big value means there is volatility clustering, but here is an explanation of it if you are interested. The test does a regression with the squared returns as the response and some number of lags (most recent previous data) of the squared returns as explanatory variables. (An estimate of the mean of the returns is generally removed first, but this has little impact in practice.) If the last few squared returns have power to predict tomorrow's squared return, then there must be volatility clustering. The tricky part of the function is the line that creates inds. This object is a matrix of the desired indices of the squared returns for the matrix of explanatory variables. Once the explanatory matrix is created, the regression is performed and the desired statistic is returned. The default number of lags to use is 10. </span>
<span style="color: #666666; font-style: italic;"># A random permutation test compares the value of the test statistic for the original data to the distribution of test statistics when the data are permuted. We do this below for our example. </span>
spx.arch.perm = <a href="http://inside-r.org/r-doc/base/numeric"><span style="color: #003399; font-weight: bold;">numeric</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">1000</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
spx.arch.perm<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = engle.arch.test<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>spxret<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>spx.arch.perm<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">'yellow'</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>v=engle.arch.test<span style="color: #009900;">(</span>spxret<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">'blue'</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># The simplest way to get a random permutation in R is to put your data vector as the only argument to the sample function. The call: </span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>spxret<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#is equivalent to: </span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>spxret<span style="color: #339933;">,</span> size=<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>spxret<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># A simple calculation for the p-value of the permutation test is to count the number of statistics from permuted data that exceed the statistic for the original data and then divide by the number of permutations performed. In R a succinct form of this calculation is: </span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>spx.arch.perm >= engle.arch.test<span style="color: #009900;">(</span>spxret<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># In my case I get 0.016. That is, 16 of the 1000 permutations produced a test statistic larger than the statistic from the real data. A test using 100,000 permutations gave a value of 0.0111. </span>
<span style="color: #666666; font-style: italic;">## Basado en ayuda del paquete BHH2</span>
<span style="color: #666666; font-style: italic;">## Permutation test for means and variance comparisons.</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/BHH2">BHH2</a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Permutation test for Tomato Data</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>tomato.data<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/cat"><span style="color: #003399; font-weight: bold;">cat</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Tomato Data (not paired):<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span>tomato.data<span style="color: #009900;">)</span>
a = pounds<span style="color: #009900;">[</span>fertilizer==<span style="color: blue;">"A"</span><span style="color: #009900;">]</span>
b = pounds<span style="color: #009900;">[</span>fertilizer==<span style="color: blue;">"B"</span><span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/base/print"><span style="color: #003399; font-weight: bold;">print</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span>test = <a href="http://inside-r.org/packages/cran/permtest">permtest</a><span style="color: #009900;">(</span>b<span style="color: #339933;">,</span>a<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Permutation test for Boy's Shoes Example</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>shoes.data<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/cat"><span style="color: #003399; font-weight: bold;">cat</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Shoes Data (paired):<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span>shoes.data<span style="color: #009900;">)</span>
x = matB-matA
<a href="http://inside-r.org/r-doc/base/print"><span style="color: #003399; font-weight: bold;">print</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span>test = <a href="http://inside-r.org/packages/cran/permtest">permtest</a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;">###############################################################</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># to do a leave-on-out jackknife estimate for the mean of the</span>
<span style="color: #666666; font-style: italic;"># data ?jackknife gives an example</span>
<span style="color: #666666; font-style: italic;"># Having a look at the jackknife function we see that it demands</span>
<span style="color: #666666; font-style: italic;"># two parameters: x and theta. x is supposed to contain the data</span>
<span style="color: #666666; font-style: italic;"># and theta the function that is applied to the data</span>
x = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span>
theta = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">{</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">}</span>
results = jackknife<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>theta<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">###############################################################</span>
<span style="color: #666666; font-style: italic;">#### Jackknife</span>
<span style="color: #666666; font-style: italic;">###############################################################</span>
<span style="color: #666666; font-style: italic;">#Bootstrap</span>
<span style="color: #666666; font-style: italic;">#Now I want to program the estimation for one coefficient of a linear regression model. </span>
DF = <a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">250</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># my data</span>
model.lm = <a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a><span style="color: #009900;">(</span>V1 ~ V2 + V3 + V4<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># my model </span>
<span style="color: #666666; font-style: italic;"># Now I need to specify the theta function. Here x is not the</span>
<span style="color: #666666; font-style: italic;"># data itself but is used as the row index vector to select</span>
<span style="color: #666666; font-style: italic;"># a subset from the data frame (xdata). Also the coefficient</span>
<span style="color: #666666; font-style: italic;"># to be returned is specified. </span>
theta = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> xdata<span style="color: #339933;">,</span> coefficient<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">(</span>model.lm<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=xdata<span style="color: #009900;">[</span>x<span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">[</span>coefficient<span style="color: #009900;">]</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># So now at each leave-on-out run the model is calculated with</span>
<span style="color: #666666; font-style: italic;"># a subset defined by the vector x (here one is left out) and one</span>
<span style="color: #666666; font-style: italic;"># coefficient is returned: </span>
results = jackknife<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> theta<span style="color: #339933;">,</span> xdata=DF<span style="color: #339933;">,</span> coefficient=<span style="color: blue;">"(Intercept)"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## To expand this code onto the estimation of all the regression coefficients is only a small step now. As the theta function is supposed to return a scalar and not a list of estimates for each coefficient, the following workaround is used: The sapply function calls the jackknife function four times prompting a different parameter estimate at each run. The prompted coeffient is passed on to the jackknife function by the three point (?) argument .</span>
<span style="color: #666666; font-style: italic;"># The following function calculates all then coefficients</span>
jackknife.apply = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> xdata<span style="color: #339933;">,</span> coefs<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>coefs<span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>coefficient<span style="color: #009900;">)</span> jackknife<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> theta<span style="color: #339933;">,</span> xdata=xdata<span style="color: #339933;">,</span>
coefficient=coefficient<span style="color: #009900;">)</span><span style="color: #339933;">,</span>
simplify=F<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># now jackknife.apply() can be called</span>
results = jackknife.apply<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> DF<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"(Intercept)"</span><span style="color: #339933;">,</span> <span style="color: blue;">"V2"</span><span style="color: #339933;">,</span> <span style="color: blue;">"V3"</span><span style="color: #339933;">,</span> <span style="color: blue;">"V3"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">###############################################################</span>
<span style="color: #666666; font-style: italic;">### Cross Validation</span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;"># Basado en http://www.burns-stat.com/pages/Tutor/bootstrap_resampling.html</span>
<span style="color: #666666; font-style: italic;">#The idea: Models should be tested with data that were not used to fit the model. If you have enough data, it is best to hold back a random portion of the data to use for testing. Cross validation is a trick to get out-of-sample tests but still use all the data. The sleight of hand is to do a number of fits, each time leaving out a different portion of the data. </span>
<span style="color: #666666; font-style: italic;">#Cross validation is perhaps most often used in the context of prediction. Everyone wants to predict the stock market. So let's do it. </span>
<span style="color: #666666; font-style: italic;">#Below we predict tomorrow's IBM return with a quadratic function of today's IBM and S&P returns. </span>
predictors = <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span>spxret<span style="color: #339933;">,</span> ibmret<span style="color: #339933;">,</span> spxret^<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> ibmret^<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
spxret * ibmret<span style="color: #009900;">)</span><span style="color: #009900;">[</span>-<span style="color: #cc66cc;">251</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
predicted = ibmret<span style="color: #009900;">[</span>-<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
predicted.lm = <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">(</span>predicted ~ predictors<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#The p-value for this regression is 0.048, so it is good enough to publish in a journal. However, you might want to hold off putting money on it until we have tested it a bit more. </span>
<span style="color: #666666; font-style: italic;">#In cross validation we divide the data into some number of groups. Then for each group we fit the model with all of the data that are not in the group, and test that fit with the data that are in the group. Below we divide the data into 5 groups. </span>
<a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a> = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a>=<span style="color: #cc66cc;">250</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a> = <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a><span style="color: #009900;">)</span>
mse.group = <a href="http://inside-r.org/r-doc/base/numeric"><span style="color: #003399; font-weight: bold;">numeric</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
group.lm = <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">(</span>predicted<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a> != i<span style="color: #009900;">]</span> ~
predictors<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a> != i<span style="color: #339933;">,</span> <span style="color: #009900;">]</span><span style="color: #009900;">)</span>
mse.group<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>predicted<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a> == i<span style="color: #009900;">]</span> -
<a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> predictors<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a> == i<span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> %*% <a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a><span style="color: #009900;">(</span>group.lm<span style="color: #009900;">)</span><span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#The first command above repeats the numbers 1 through 5 to get a vector of length 250. We do not want to use this as our grouping because we may be capturing systematic effects. In our case a group would be on one particular day of the week until a holiday interrupted the pattern. Hence the next line permutes the vector so we have random assignment, but still an equal number of observations in each group. The for loop estimates each of the five models and computes the out-of-sample mean squared error. </span>
<span style="color: #666666; font-style: italic;">#The mean squared error of the predicted vector taking only its mean into account is 0.794. The in-sample mean squared error from the regression is 0.759. This is a modest improvement, but in the context of market prediction might be of use if the improvement were real. However, the cross validation mean squared error is 0.800 -- even higher than from the constant model. This is evidence of overfitting (which this regression model surely is doing). </span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
<span style="color: #666666; font-style: italic;">#### Boostrap</span>
<span style="color: #666666; font-style: italic;">#################################################################################</span>
There are a number of R packages that are either confined to or touch upon bootstrapping or <a href="http://inside-r.org/packages/cran/its">its</a> relatives. These include:
<a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a>: This package incorporates quite a wide variety of bootstrapping tricks.
<a href="http://inside-r.org/packages/cran/bootstrap">bootstrap</a>: A package of relatively simple functions <span style="color: black; font-weight: bold;">for</span> bootstrapping and related techniques.
<a href="http://inside-r.org/packages/cran/coin">coin</a>: A package <span style="color: black; font-weight: bold;">for</span> permutation tests <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a> are discussed below<span style="color: #009900;">)</span>.
<a href="http://inside-r.org/packages/cran/Design">Design</a>: This package includes bootcov <span style="color: black; font-weight: bold;">for</span> bootstrapping the covariance of estimated regression parameters and validate <span style="color: black; font-weight: bold;">for</span> testing the quality of <a href="http://inside-r.org/r-doc/stats/fitted"><span style="color: #003399; font-weight: bold;">fitted</span></a> models <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a> cross validation or bootstrapping.
<a href="http://inside-r.org/packages/cran/MChtest">MChtest</a>: This package <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a> <span style="color: black; font-weight: bold;">for</span> Monte Carlo hypothesis tests<span style="color: #339933;">,</span> that <a href="http://inside-r.org/r-doc/methods/is"><span style="color: #003399; font-weight: bold;">is</span></a><span style="color: #339933;">,</span> tests using some form of resampling. This includes code <span style="color: black; font-weight: bold;">for</span> <a href="http://inside-r.org/packages/cran/sampling">sampling</a> rules where the number of samples taken depend on how certain the result is.
<a href="http://inside-r.org/packages/cran/meboot">meboot</a>: Provides a method of bootstrapping a <a href="http://inside-r.org/r-doc/stats/time"><span style="color: #003399; font-weight: bold;">time</span></a> series.
<a href="http://inside-r.org/packages/cran/permtest">permtest</a>: A package containing a <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a> <span style="color: black; font-weight: bold;">for</span> permutation tests of microarray data.
<a href="http://inside-r.org/packages/cran/resper">resper</a>: A package <span style="color: black; font-weight: bold;">for</span> doing restricted permutations.
<a href="http://inside-r.org/packages/cran/scaleboot">scaleboot</a>: This package produces approximately unbiased hypothesis tests via bootstrapping.
<a href="http://inside-r.org/packages/cran/simpleboot">simpleboot</a>: A package of a few functions that perform <span style="color: #009900;">(</span>or present<span style="color: #009900;">)</span> bootstraps <span style="color: black; font-weight: bold;">in</span> simple situations<span style="color: #339933;">,</span> such <a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> one and two samples<span style="color: #339933;">,</span> and linear regression.
There are a large number of R packages that include bootstrapping. Examples include <a href="http://inside-r.org/packages/cran/multtest">multtest</a> that has the boot.resample <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #339933;">,</span> and <a href="http://inside-r.org/packages/cran/Matching">Matching</a> <a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a> has a <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a> <span style="color: black; font-weight: bold;">for</span> a bootstrapped Kolmogorov-Smirnov test <span style="color: #009900;">(</span>the equality of two probability <a href="http://inside-r.org/packages/cran/distributions">distributions</a><span style="color: #009900;">)</span>.
<span style="color: #666666; font-style: italic;">################################################################################################</span>
Basado en http://www.zoology.ubc.ca/~schluter/zoo502stats/Rtips.resample.html
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Single variable</span>
<span style="color: #666666; font-style: italic;">#To use the boot package you will need to write a function to calculate the statistic of interest. The format is illustrated below for the sample mean, but any univariate function would be handled similarly. We'll call our function "boot.mean". When you have finished writing the script for a function you will need to cut and paste it into the command window so that R can access it (you'll need to do this just once in an R session). Here, "x" refers to the vector of data. "i" refers to a vector of indices, which must be included as an argument in the function and employed as shown.</span>
boot.mean = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>i<span style="color: #009900;">)</span><span style="color: #009900;">{</span>boot.mean = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#The command "boot" will automatically carry out all the resampling and computations required. For this example, "x" is the vector of original data and "boot.mean" is the name of the function we created above to calculate the statistic of interest. R specifies the number of bootstrap replicate estimates desired.</span>
z = <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> boot.mean<span style="color: #339933;">,</span> R=<span style="color: #cc66cc;">2000</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#The resulting object (which here named "z") is a boot object containing all the results. Use the following additional commands to pull out the results. </span>
<a href="http://inside-r.org/r-doc/base/print"><span style="color: #003399; font-weight: bold;">print</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Bootstrap calculation of bias and SE</span>
<a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>z$t<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Another way to get the standard error</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>z$t<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Histogram of boostrap replicate estimates</span>
<a href="http://inside-r.org/r-doc/stats/qqnorm"><span style="color: #003399; font-weight: bold;">qqnorm</span></a><span style="color: #009900;">(</span>z$t<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Normal quantiles of replicate estimates</span>
<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span>z<span style="color: #339933;">,</span>type=<span style="color: blue;">"bca"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 95% confidence interval using BCa</span>
<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span>z<span style="color: #339933;">,</span>type=<span style="color: blue;">"perc"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Same using percentile method</span>
<span style="color: #666666; font-style: italic;">### Two or more variables</span>
<span style="color: #666666; font-style: italic;">#Here's how you would use the boot command If the statistic of interest must be calculated from two (or more) variables (for example, a correlation, regression slope, or odds ratio). Assume that there are two variables of interest, "x" and "y". If not done already, put the two variables into a data frame (here called "mydata"),</span>
mydata = cbind.data.frame<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> stringsAsFactors=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Then create a function to calculate the statistic of interest on the variables in "mydata". For example, to create a function that calculates the correlation coefficient between the two variables "x" and "y", use </span>
boot.cor = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>mydata<span style="color: #339933;">,</span>i<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
x = mydata$x<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>
y = mydata$y<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>
boot.cor = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#Here, "i" refers to a vector of indices, which must be included as an argument in the function and employed as shown. </span>
<span style="color: #666666; font-style: italic;">#Finally, pass your data frame and function to the boot command,</span>
z = <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span>mydata<span style="color: #339933;">,</span> boot.cor<span style="color: #339933;">,</span> R=<span style="color: #cc66cc;">2000</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#See the previous section for a list of commands to pull results from the boot object (here named "z").</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
Basado en el difmediasTestBoot.pdf
<span style="color: #666666; font-style: italic;">#Uso de library( boot ) Tres ejemplos sencillos</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#1) Estimaci?n del par?metro exp( media ) mediante xbar=mean( x ):</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<a href="http://inside-r.org/r-doc/base/set.seed"><span style="color: #003399; font-weight: bold;">set.seed</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">89723</span> <span style="color: #009900;">)</span>
x = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>-<span style="color: #cc66cc;">1.034</span><span style="color: #339933;">,</span>-<span style="color: #cc66cc;">1.044</span><span style="color: #339933;">,</span>-<span style="color: #cc66cc;">1.445</span><span style="color: #339933;">,</span>-<span style="color: #cc66cc;">0.954</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.223</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.096</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.487</span><span style="color: #339933;">,</span>-<span style="color: #cc66cc;">0.485</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.491</span><span style="color: #339933;">,</span>-<span style="color: #cc66cc;">1.122</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#Los datos x</span>
xbar = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
n = <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
expm.fun = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #009900;">{</span> <a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/Matrix/expm"><span style="color: #003399; font-weight: bold;">expm</span></a> = expm.fun<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> n<span style="color: #339933;">,</span> xbar<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/Matrix/expm"><span style="color: #003399; font-weight: bold;">expm</span></a> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#n xbar expm</span>
<span style="color: #666666; font-style: italic;">#[1,] 10 -0.4787 0.6195883</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a> <span style="color: #009900;">)</span>
R = <span style="color: #cc66cc;">999</span>
<span style="color: #666666; font-style: italic;">#Es preciso definir una funci?n de los datos, x, y de los ?ndices, que ser?n tomados al azar al hacer bootstrap, que calcule el estad?stico a "bootstrapear":</span>
expm.i = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span> x<span style="color: #339933;">,</span> indices <span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
expmb = <a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span> x<span style="color: #009900;">[</span>indices<span style="color: #009900;">]</span> <span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span> expmb <span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#Bootstrap del estad?stico exp(media):</span>
expm.boot = <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=x<span style="color: #339933;">,</span> statistic=expm.i<span style="color: #339933;">,</span> R=R <span style="color: #009900;">)</span>
expm.boot
<span style="color: #666666; font-style: italic;">#ORDINARY NONPARAMETRIC BOOTSTRAP</span>
<span style="color: #666666; font-style: italic;">#Call:</span>
<span style="color: #666666; font-style: italic;">#boot(data = x, statistic = expm.i, R = R)</span>
<span style="color: #666666; font-style: italic;">#Bootstrap Statistics :</span>
<span style="color: #666666; font-style: italic;">#original bias std. error</span>
<span style="color: #666666; font-style: italic;">#t1* 0.6195883 0.01076195 0.1456947</span>
<span style="color: #666666; font-style: italic;">#Boostrap del intervalo de confianza. La funci?n boot.ci aplicada a un objeto de tipo boot permite calcular diferentes IC bootstrap:</span>
<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span> expm.boot<span style="color: #339933;">,</span> conf=<span style="color: #cc66cc;">0.95</span><span style="color: #339933;">,</span> type=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"perc"</span><span style="color: #339933;">,</span> <span style="color: blue;">"bca"</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS</span>
<span style="color: #666666; font-style: italic;">#Based on 999 bootstrap replicates</span>
<span style="color: #666666; font-style: italic;">#CALL :</span>
<span style="color: #666666; font-style: italic;">#boot.ci(boot.out = expm.boot, conf = 0.95, type = c("perc", "bca"))</span>
<span style="color: #666666; font-style: italic;">#Intervals :</span>
<span style="color: #666666; font-style: italic;">#Level Percentile BCa</span>
<span style="color: #666666; font-style: italic;">#95% ( 0.3928, 0.9627 ) ( 0.4067, 0.9822 )</span>
<span style="color: #666666; font-style: italic;">#Calculations and Intervals on Original Scale</span>
<span style="color: #666666; font-style: italic;">#Las r?plicas bootstrap se encuentran en el elemento expm.boot$t del objeto expm.boot:</span>
expmb = expm.boot$t <span style="color: #666666; font-style: italic;">### valores bootstrap de expm</span>
<a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span> expmb <span style="color: #009900;">)</span> <span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.1456947</span>
<span style="color: #666666; font-style: italic;">### se bootstrap de expm</span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span> expmb<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">0.025</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#2.5% 97.5%</span>
<span style="color: #666666; font-style: italic;">#0.3946921 0.9600645</span>
<span style="color: #666666; font-style: italic;">#La funci?n plot aplicada al objeto de tipo boot, expm.boot, produce por defecto un histograma de expmb y un plot de normalidad:</span>
<a href="http://inside-r.org/r-doc/grDevices/windows"><span style="color: #003399; font-weight: bold;">windows</span></a><span style="color: #009900;">(</span> width=<span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> height=<span style="color: #cc66cc;">4</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span> pty=<span style="color: blue;">"s"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span> expm.boot <span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### funci?n plot.boot: produce dos plots</span>
<a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a><span style="color: #009900;">(</span> main=<span style="color: blue;">"Resultados de expm.boot"</span><span style="color: #339933;">,</span> font.main=<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/line"><span style="color: #003399; font-weight: bold;">line</span></a>=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> col.main=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> expmb<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> probability=T <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/Matrix/expm"><span style="color: #003399; font-weight: bold;">expm</span></a><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>expmb<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.025</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/Matrix/expm"><span style="color: #003399; font-weight: bold;">expm</span></a><span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/density"><span style="color: #003399; font-weight: bold;">density</span></a><span style="color: #009900;">(</span> expmb <span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#### 2) Regresi?n Bootstrap sobre la muestra de pares. IC para beta1</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">### y respuesta; x explicativa; IC para beta1</span>
x = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">109</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">88</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">109</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">116</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">114</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">85</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">113</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">117</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">107</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">104</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">101</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">81</span><span style="color: #009900;">)</span>
y = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">116</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">77</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">95</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">79</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">113</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">122</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">109</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">94</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">91</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">88</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">115</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">119</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">115</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">95</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">)</span>
lmyx = <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">(</span> y ~ x <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span> lmyx <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Call:</span>
<span style="color: #666666; font-style: italic;">#lm(formula = y ~ x)</span>
<span style="color: #666666; font-style: italic;">#Coefficients:</span>
<span style="color: #666666; font-style: italic;">#Estimate Std. Error t value Pr(>|t|)</span>
<span style="color: #666666; font-style: italic;">#(Intercept) -9.3016 20.0530 -0.464 0.65</span>
<span style="color: #666666; font-style: italic;">#x 1.0826 0.1955 5.537 7.32e-05 ***</span>
<span style="color: #666666; font-style: italic;">#Residual standard error: 8.414 on 14 degrees of freedom</span>
<span style="color: #666666; font-style: italic;">#Multiple R-squared: 0.6865, Adjusted R-squared: 0.6641</span>
<span style="color: #666666; font-style: italic;">#F-statistic: 30.66 on 1 and 14 DF, p-value: 7.319e-05</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a> <span style="color: #009900;">)</span>
R = <span style="color: #cc66cc;">999</span>
datos = <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
beta1.i = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span> datos<span style="color: #339933;">,</span> i <span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">(</span> datos<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> ~ datos<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> <span style="color: #009900;">)</span> <span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>
<span style="color: #009900;">}</span>
beta1.b = <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=datos<span style="color: #339933;">,</span> statistic=beta1.i<span style="color: #339933;">,</span> R=R <span style="color: #009900;">)</span>
beta1.b
<span style="color: #666666; font-style: italic;"># ORDINARY NONPARAMETRIC BOOTSTRAP</span>
<span style="color: #666666; font-style: italic;"># Call:</span>
<span style="color: #666666; font-style: italic;"># boot(data = datos, statistic = beta1.i, R = R)</span>
<span style="color: #666666; font-style: italic;"># Bootstrap Statistics :</span>
<span style="color: #666666; font-style: italic;"># original bias std. error</span>
<span style="color: #666666; font-style: italic;"># t1* 1.082613 0.02671124 0.2162240</span>
<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span> beta1.b<span style="color: #339933;">,</span> type=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"perc"</span><span style="color: #339933;">,</span> <span style="color: blue;">"bca"</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS</span>
<span style="color: #666666; font-style: italic;"># Based on 999 bootstrap replicates</span>
<span style="color: #666666; font-style: italic;"># CALL :</span>
<span style="color: #666666; font-style: italic;"># boot.ci(boot.out = beta1.b, type = c("perc", "bca"))</span>
<span style="color: #666666; font-style: italic;"># Intervals :</span>
<span style="color: #666666; font-style: italic;"># Level Percentile BCa</span>
<span style="color: #666666; font-style: italic;"># 95% ( 0.795, 1.653 ) ( 0.774, 1.628 )</span>
<span style="color: #666666; font-style: italic;"># Calculations and Intervals on Original Scale</span>
<a href="http://inside-r.org/r-doc/grDevices/windows"><span style="color: #003399; font-weight: bold;">windows</span></a><span style="color: #009900;">(</span> width=<span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> height=<span style="color: #cc66cc;">4</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span> pty=<span style="color: blue;">"s"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span> beta1.b<span style="color: #339933;">,</span> cex.main=<span style="color: #cc66cc;">0.8</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a><span style="color: #009900;">(</span> main=<span style="color: blue;">"Regresi?n lineal: beta1.b"</span><span style="color: #339933;">,</span> font.main=<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/line"><span style="color: #003399; font-weight: bold;">line</span></a>=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> col.main=<span style="color: blue;">"blue"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> beta1.b$t<span style="color: #339933;">,</span> probability=T<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> main=<span style="color: blue;">"Histograma de beta1.b"</span><span style="color: #339933;">,</span> cex.main=<span style="color: #cc66cc;">0.9</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>v=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a><span style="color: #009900;">(</span>lmyx<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>beta1.b$t<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.025</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> Histograma <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> beta1.bbeta1.b$tDensity0.60.81.01.21.41.61.80.00.51.01.52.0
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a><span style="color: #009900;">(</span>lmyx<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/density"><span style="color: #003399; font-weight: bold;">density</span></a><span style="color: #009900;">(</span>beta1.b$t<span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#### 1) Bootstrap para la comparaci?n de medias (IC y tests bootstrap)</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#Dadas dos muestras, x e y, de sendas poblaciones independientes, con distribuciones F y G respectivamente, se quiere calcular un IC para la diferencia de medias.</span>
<span style="color: #666666; font-style: italic;">#Por ejemplo: ?Cu?l es la diferencia de medias del consumo diario de energ?a de individuos normales y obesos?</span>
<span style="color: #666666; font-style: italic;">#Consumo diario (MJ/d?a) en un grupo de 13 individuos de peso normal: 6.13, 7.05, 7.48, 7.48, 7.53, 7.58, 7.90, 8.08, 8.09, 8.11, 8.40, 10.15, 10.88.</span>
<span style="color: #666666; font-style: italic;">#Consumo en un grupo de 9 individuos obesos: 7.67, 8.19, 8.21, 8.79, 9.11, 9.21, 10.34, 11.53, 12.10.</span>
<span style="color: #666666; font-style: italic;">### Aplicaci?n: IC percentil bootstrap para la diferencia de medias</span>
x = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">6.13</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.05</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.48</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.48</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.53</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.58</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.90</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.08</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.09</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.11</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10.15</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10.88</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### x: consumo diario de energia en individuos de peso normal</span>
y = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">7.67</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.21</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8.79</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9.11</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9.21</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10.34</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11.53</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12.10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### y: consumo diario de energia en individuos obesos</span>
n = <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
m = <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
mx = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
my = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
se.x = <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>/sqrt<span style="color: #009900;">(</span>n<span style="color: #009900;">)</span>
se.y = <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>/sqrt<span style="color: #009900;">(</span>m<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Estimaci?n de la diferencia de medias ###</span>
dif = my-mx <span style="color: #666666; font-style: italic;">### diferencia de medias observadas</span>
se1.dif = <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>/m<span style="color: #009900;">)</span>+<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>/n<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### se varianzas distintas</span>
se2.dif = <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>+<span style="color: #009900;">(</span><span style="color: #009900;">(</span>m-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>/<span style="color: #009900;">(</span>n+m-<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/m<span style="color: #009900;">)</span>+<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/n<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### se2.dif: se para varianzas iguales</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> n<span style="color: #339933;">,</span> mx<span style="color: #339933;">,</span> se.x<span style="color: #339933;">,</span> m<span style="color: #339933;">,</span> my<span style="color: #339933;">,</span> se.y<span style="color: #339933;">,</span> dif<span style="color: #339933;">,</span> se1.dif<span style="color: #339933;">,</span> se2.dif <span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#n mx se.x m my se.y dif se1.dif se2.dif</span>
<span style="color: #666666; font-style: italic;">#[1,] 13 8.066 0.343 9 9.461 0.514 1.395 0.618 0.593</span>
<span style="color: #666666; font-style: italic;">###Se puede obtener f?cilmente un IC basado en la t de student para la diferencia de medias:</span>
alfa = <span style="color: #cc66cc;">0.05</span><span style="color: #339933;">;</span> confianza = <span style="color: #cc66cc;">1</span>-alfa<span style="color: #339933;">;</span>
infdif = dif - <a href="http://inside-r.org/r-doc/stats/qt"><span style="color: #003399; font-weight: bold;">qt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-alfa/<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> n+m-<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> * se2.dif
supdif = dif + <a href="http://inside-r.org/r-doc/stats/qt"><span style="color: #003399; font-weight: bold;">qt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-alfa/<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> n+m-<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> * se2.dif
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> dif<span style="color: #339933;">,</span> se2.dif<span style="color: #339933;">,</span> infdif<span style="color: #339933;">,</span> supdif<span style="color: #339933;">,</span> confianza <span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#dif se2.dif infdif supdif confianza</span>
<span style="color: #666666; font-style: italic;">#[1,] 1.395 0.593 0.157 2.632 0.95</span>
<span style="color: #666666; font-style: italic;">#Mediante bootstrap podemos obtener un IC para la diferencia de medias utilizando simplemente su estimador, la diferencia de medias muestrales. Usando R=1000 r?plicas bootstrap, realizadas por separado, para x, y para y, se simula la distribuci?n bootstrap de la diferencia de medias muestrales, de la que obtenemos el IC bootstrap percentil para la diferencia de medias.</span>
<span style="color: #666666; font-style: italic;">### bootstrap para la diferencia de medias: IC percentil bootstrap ###</span>
R = <span style="color: #cc66cc;">1000</span>
<a href="http://inside-r.org/r-doc/base/set.seed"><span style="color: #003399; font-weight: bold;">set.seed</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">48761</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/tm">tm</a> = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span>R*m<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #339933;">,</span> R<span style="color: #339933;">,</span> m<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### r?plicas bootstrap separado</span>
<a href="http://inside-r.org/r-doc/grDevices/cm"><span style="color: #003399; font-weight: bold;">cm</span></a> = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>R*n<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #339933;">,</span> R<span style="color: #339933;">,</span> n<span style="color: #009900;">)</span>
tb = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/tm">tm</a><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
cb = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/grDevices/cm"><span style="color: #003399; font-weight: bold;">cm</span></a><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
difb = tb-cb <span style="color: #666666; font-style: italic;">### replicas de diferencia de medias bootstrap</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>difb<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>difb<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 1.3928984 0.5880266</span>
ppp = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.025</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.05</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.95</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>difb<span style="color: #339933;">,</span> ppp<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### percentiles bootstrap de diferencia de medias</span>
<span style="color: #666666; font-style: italic;">#2.5% 5% 50% 95% 97.5%</span>
<span style="color: #666666; font-style: italic;">#0.2375 0.3918 1.4011 2.3413 2.5311</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> difb<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> probability=T<span style="color: #339933;">,</span> main=<span style="color: blue;">"histograma de difb"</span><span style="color: #339933;">,</span> cex.main=<span style="color: #cc66cc;">0.9</span><span style="color: #339933;">,</span> xlab=<span style="color: blue;">"difb: diferencia de medias bootstrap"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> dif<span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>difb<span style="color: #339933;">,</span><span style="color: #cc66cc;">0.025</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>difb<span style="color: #339933;">,</span><span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> dif<span style="color: #009900;">)</span><span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>difb<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>difb<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> add=T<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#### 2) Bootstrap de igualdad de distribuciones de dos muestras independientes</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#Problema .Hay diferencias en la distribucion del consumo diario de energia de individuos normales y obesos? H0: F=G.</span>
<span style="color: #666666; font-style: italic;">#El problema puede ser resuelto mediante un test de aleatorizacion, pero la metodologia bootstrap nos proporciona otros procedimientos para resolver el mismo problema.</span>
<span style="color: #666666; font-style: italic;">#Supongamos que para hacer el contraste de igualdad de distribuciones utilizamos el estadistico diferencia de medias muestrales D=meanY-meanX, estimador de la diferencia de medias mu1-mu2. de ambas poblaciones, aunque D sea mas apropiado para valorar solo la diferencia de medias.</span>
<span style="color: #666666; font-style: italic;">#Fijado un numero R de replicas, de la muestra conjunta z = (x, y) extraemos al azar y con reemplazamiento n+m valores en cada replica.</span>
<span style="color: #666666; font-style: italic;">#Con cada replica r, tenemos un vector en el que llamaremos a los n primeros elementos e a los m ultimos, calculando con ellos el valor de la replica r bootstrap de la diferencia de medias *rz*rx*ry***rxryrd.=.</span>
<span style="color: #666666; font-style: italic;">#Disponemos asi de las R simulaciones de la distribucion bootstrap de la diferencia de medias con las que podemos aproximar el valor-p bootstrap de nuestro test: p-valor bootstrap~#(dr*>d)/R, donde d es el valor observado de la diferencia de medias muestrales D.</span>
<span style="color: #666666; font-style: italic;">#El proceso es similar al llevado a cabo en el test de aleatorizacion. La diferencia estriba en que ahora se extraen muestras con reemplazamiento de la muestra conjunta z = (x, y).</span>
<span style="color: #666666; font-style: italic;">#Aplicaci?n del procedimiento:</span>
z = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
zm = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>z<span style="color: #339933;">,</span> R*<span style="color: #009900;">(</span>n+m<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #339933;">,</span> R<span style="color: #339933;">,</span> n+m <span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### remuestreo bootstrap conjunto</span>
xzm = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span> zm<span style="color: #009900;">[</span> <span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>:n<span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> <span style="color: #009900;">)</span>
yzm = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span> zm<span style="color: #009900;">[</span> <span style="color: #339933;">,</span> <span style="color: #009900;">(</span>n+<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>:<span style="color: #009900;">(</span>n+m<span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> <span style="color: #009900;">)</span>
difzb = yzm-xzm <span style="color: #666666; font-style: italic;">### replicas de diferencia de medias bootstrap</span>
pv.difzb = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span> difzb >= dif <span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### p-valor bootstrap de la difer de medias</span>
cbi
<span style="color: #666666; font-style: italic;">#nd( dif, pv.difzb) dif pv.difzb</span>
<span style="color: #666666; font-style: italic;">#[1,] 1.394957 0.026</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#par( mfrow=c(2,2), pty="s" )</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> difzb<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> probability=T<span style="color: #339933;">,</span> main=<span style="color: blue;">"histograma de difzb"</span><span style="color: #339933;">,</span> xlab=<span style="color: blue;">"difzb: diferencia de medias bootstrap"</span><span style="color: #339933;">,</span> cex.main=<span style="color: #cc66cc;">0.9</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> dif<span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=dif<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>difzb<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>difzb<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> add=T<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#El mismo procedimiento puede ser aplicado a cualquier otro test para contrastar la igualdad de distribuciones. Veamos el uso del test t (dise?ado para la comparaci?n de medias, cuando las varianzas de ambas poblaciones, supuestamente normales, son iguales):</span>
<span style="color: #666666; font-style: italic;">### R?plicas del test t bootstrap basado en la muestra conjunta ###</span>
sumx2zm = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span> zm<span style="color: #009900;">[</span> <span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>:n<span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a> <span style="color: #009900;">)</span>*<span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
sumy2zm = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span> zm<span style="color: #009900;">[</span> <span style="color: #339933;">,</span> <span style="color: #009900;">(</span>n+<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>:<span style="color: #009900;">(</span>n+m<span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a> <span style="color: #009900;">)</span>*<span style="color: #009900;">(</span>m-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
se2b = <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span> <span style="color: #009900;">(</span>sumx2zm+sumy2zm<span style="color: #009900;">)</span>/<span style="color: #009900;">(</span>n+m-<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/m<span style="color: #009900;">)</span>+<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/n<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
t2 = difzb/se2b <span style="color: #666666; font-style: italic;">### replicas del estadistico t bootstrap</span>
tobs2 = dif/se2.dif <span style="color: #666666; font-style: italic;">### estadistico t observado</span>
tobs2
<span style="color: #666666; font-style: italic;">#[1] 2.351313</span>
pv.tboot = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span> t2>=tobs2 <span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### p-valor bootstrap del t bootstrap (un lado)</span>
pv.t = <span style="color: #cc66cc;">1</span>-<a href="http://inside-r.org/r-doc/stats/pt"><span style="color: #003399; font-weight: bold;">pt</span></a><span style="color: #009900;">(</span> tobs2<span style="color: #339933;">,</span>n+m-<span style="color: #cc66cc;">2</span> <span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### p-valor del test t para igualdad de medias</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> dif<span style="color: #339933;">,</span> pv.difzb<span style="color: #339933;">,</span> tobs2<span style="color: #339933;">,</span> pv.t<span style="color: #339933;">,</span> pv.tboot <span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#dif pv.difzb tobs2 pv.t pv.tboot</span>
<span style="color: #666666; font-style: italic;">#[1,] 1.395 0.026 2.3513 0.0145 0.024</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> t2<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> probability=T<span style="color: #339933;">,</span> main=<span style="color: blue;">"histograma de t2"</span><span style="color: #339933;">,</span> cex.main=<span style="color: #cc66cc;">0.9</span><span style="color: #339933;">,</span> xlab=<span style="color: blue;">"t2: estad?stico t bootstrap para diferencia de medias"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=tobs2<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>t2<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>t2<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> add=T<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Obs?rvese que no hay diferencia de procedimiento en la obtenci?n del p-valor del test basado en la diferencia de medias o en el estad?stico t de student, y el resultado es similar (ver pv.difzb y pv.tboot). En cambio, la diferencia con el p-valor del test t de student exacto es notable (ver pv.t). Hay que insistir en que contrastar F=G no es equivalente a contrastar la igualdad de sus medias.</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#### 3) Bootstrap de igualdad de medias de muestras independientes</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#Si unicamente tuvieramos interes en contrastar la hipotesis 21:0?g?g=H, mas debil que la anterior H0 : F = G, entonces lo mas adecuado es utilizar el test t-Student si se cumplen las condiciones de independencia, normalidad e igualdad de varianzas.</span>
<span style="color: #666666; font-style: italic;">#Si las varianzas fueran diferentes podria plantearse el uso de otro estadistico t con una estimacion de las varianzas por separado, 221211smsnXYt+.=, donde en el denominador tenemos las varianzas muestrales de x e y.</span>
<span style="color: #666666; font-style: italic;">#Dicho estadistico no tiene bajo H0 una distribucion t-Student y solo se dispone de soluciones aproximadas para obtener el valor-p. Es conocido en la estadistica clasica como ??problema de Behrens-Fisher??.</span>
<span style="color: #666666; font-style: italic;">#El uso del bootstrap permite soslayar este problema, cuando tratamos de contrastar 21:0?g?g=H. Bajo esta hipotesis, es admisible que las distribuciones de ambas poblaciones, F y G, puedan ser diferentes.</span>
<span style="color: #666666; font-style: italic;">#Los datos x nos dan la distribucion empirica para estimar F y los datos y la G para estimar G. Para calcular el valor-p de un test mediante bootstrap necesitaremos que ambas distribuciones empiricas sean del tipo indicado por F..21:0?g?g=H, esto es tengan iguales medias. Una forma razonable de conseguirlo sin alterar otras caracterirticas de ambas distribuciones consiste en relocalizar los datos x e y observados, de forma que mediante dicha traslacion ambas muestras presenten la misma media, por ejemplo la media de la muestra conjunta.</span>
<span style="color: #666666; font-style: italic;">#Si z = (x, y) es la muestra conjunta, con media muestral )(1ymxnmnz++=, consideramos zxxxloc+.= e zyyyloc+.= y adoptamos ahora xloc e yloc como si fueran nuestros datos de partida, teniendo ambas muestras como media muestral el valor z.</span>
<span style="color: #666666; font-style: italic;">#Con xloc e yloc extraemos por separado muestras bootstrap xloc* e yloc*, calculando con cada una de ellas sus medias y varianzas bootstrap para obtener las replicas bootstrap del estadistico t, t1*, ..., tR*.</span>
<span style="color: #666666; font-style: italic;">#El valor-p bootstrap es Robstrt)*(#., donde es el valor observado del estadistico t. obst</span>
<span style="color: #666666; font-style: italic;">#A continuacion aplicamos el procedimiento, relocalizando las muestras de modo que ambas tengan la misma media comun global.</span>
<span style="color: #666666; font-style: italic;">#Aplicaci?n</span>
xloc = x-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>+<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### controles relocalizados en la media global</span>
yloc = y-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>+<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### tratados relocalizados</span>
<span style="color: #666666; font-style: italic;">### bootstrap separado para test de igualdad de medias</span>
xlocm = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>xloc<span style="color: #339933;">,</span>R*n<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #339933;">,</span>R<span style="color: #339933;">,</span>n<span style="color: #009900;">)</span>
ylocm = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>yloc<span style="color: #339933;">,</span>R*m<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #339933;">,</span>R<span style="color: #339933;">,</span>m<span style="color: #009900;">)</span>
xlocb = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>xlocm<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
ylocb = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>ylocm<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
dyxlocb = ylocb-xlocb <span style="color: #666666; font-style: italic;">### diferencias de medias bootstrap tras relocalizacion</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span> dyxlocb >= dif <span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### p-valor bootstrap de test basado en diferencia de medias</span>
<span style="color: #666666; font-style: italic;">#[1] 0.01</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>dyxlocb<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> probability=T<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> dif<span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=dif<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dyxlocb<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>dyxlocb<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> add=T<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### usando el estad?stico t</span>
vxlocb = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>xlocm<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">)</span>
vylocb = <a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>ylocm<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## replicas bootstrap del estadistico t con varianzas distintas</span>
tyxlocb = dyxlocb/sqrt<span style="color: #009900;">(</span><span style="color: #009900;">(</span>vxlocb/n<span style="color: #009900;">)</span>+<span style="color: #009900;">(</span>vylocb/m<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>tyxlocb>=tobs2<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### p-valor bootstrap de tyxlocb</span>
<span style="color: #666666; font-style: italic;">#[1] 0.01</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> tyxlocb<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> probability=T <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> tobs2<span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=tobs2<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>tyxlocb<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>tyxlocb<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> add=T<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#### 4) CORRELACI?N BOOTSTRAP</span>
<span style="color: #666666; font-style: italic;">################################################################################################</span>
<span style="color: #666666; font-style: italic;">#Se quiere conocer la correlaci?n entre las puntuaciones de expresi?n verbal y capacidad matem?tica de los estudiantes de un curso. Para ello se ha pasado un test a 16 estudiantes y se han obtenido los resultados de x:expresi?n verbal e y:capacidad matem?tica. (Pawitan)</span>
<span style="color: #666666; font-style: italic;">### Datos IQ: x expresi?n verbal, y capacidad matem?tica ###</span>
x = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">109</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">88</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">109</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">116</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">114</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">96</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">85</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">113</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">117</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">107</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">104</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">101</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">81</span><span style="color: #009900;">)</span>
y = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">116</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">77</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">95</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">79</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">113</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">122</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">109</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">94</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">91</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">88</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">115</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">119</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">115</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">95</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">)</span>
n = <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
xm = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> ym = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
s2x = <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> sdx = <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span>s2x<span style="color: #009900;">)</span>
s2y = <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> sdy = <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span>s2y<span style="color: #009900;">)</span>
cxy = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> xlab=<span style="color: blue;">"verbal"</span><span style="color: #339933;">,</span> ylab=<span style="color: blue;">"matematica"</span><span style="color: #339933;">,</span> main=<span style="color: blue;">"datos IQ"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/lsfit"><span style="color: #003399; font-weight: bold;">lsfit</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>$coef<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span>n<span style="color: #339933;">,</span> xm<span style="color: #339933;">,</span> ym<span style="color: #339933;">,</span> s2x<span style="color: #339933;">,</span> sdx<span style="color: #339933;">,</span> s2y<span style="color: #339933;">,</span> sdy<span style="color: #339933;">,</span> cxy<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#n xm ym s2x sdx s2y sdy cxy</span>
<span style="color: #666666; font-style: italic;">#[1,] 16 102 101.125 123.4667 11.1116 210.7833 14.5184 0.8286</span>
<span style="color: #666666; font-style: italic;">#Bajo normalidad conjunta de (X, Y), un estimador clasico del error estandar de la correlacion muestral rho. viene dado por (1-rho^2)/sqrt(n-3).</span>
<span style="color: #666666; font-style: italic;">#Si se considera la transformacion de Fisher tau=0.5log((1+rho)/(1-rho)) entonces el estimador ?n. es aproximadamente normal de media tau y desviacion estandar 1/sqrt(n-3).</span>
<span style="color: #666666; font-style: italic;">#Esto permite obtener IC's para tau y aplicando la transformacion inversa de Fisher a los extremos del IC, obtendremos un I.C. para la correlacion.</span>
<span style="color: #666666; font-style: italic;">### se de la correlacion bajo normalidad conjunta de (X,Y)</span>
se.cxy = <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-cxy^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>/sqrt<span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### tau: transformacion de Fisher de la correlacion</span>
<a href="http://inside-r.org/r-doc/boot/tau"><span style="color: #003399; font-weight: bold;">tau</span></a> = <span style="color: #cc66cc;">0.5</span>*<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span> <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+cxy<span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-cxy<span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
alfa = <span style="color: #cc66cc;">0.05</span><span style="color: #339933;">;</span> confianza = <span style="color: #cc66cc;">1</span>-alfa<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">### IC para tau</span>
tau.inf = tau-<a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-alfa/<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>*<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/sqrt<span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
tau.sup = <a href="http://inside-r.org/r-doc/boot/tau"><span style="color: #003399; font-weight: bold;">tau</span></a>+<a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-alfa/<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>*<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/sqrt<span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### IC para la correlacion</span>
cxy.inf = <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*tau.inf<span style="color: #009900;">)</span>-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*tau.inf<span style="color: #009900;">)</span>+<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
cxy.sup = <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*tau.sup<span style="color: #009900;">)</span>-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*tau.sup<span style="color: #009900;">)</span>+<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> n<span style="color: #339933;">,</span> cxy<span style="color: #339933;">,</span> se.cxy<span style="color: #339933;">,</span> cxy.inf<span style="color: #339933;">,</span> cxy.sup<span style="color: #339933;">,</span> confianza <span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#n cxy se.cxy cxy.inf cxy.sup confianza</span>
<span style="color: #666666; font-style: italic;">#[1,] 16 0.8286 0.0869 0.5649 0.9387 0.95</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a> <span style="color: #009900;">)</span>
R = <span style="color: #cc66cc;">999</span>
datos = <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
correla = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span> datos<span style="color: #339933;">,</span> indices <span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
x = datos<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>indices<span style="color: #009900;">]</span>
y = datos<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>indices<span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span> x<span style="color: #009900;">[</span>indices<span style="color: #009900;">]</span><span style="color: #339933;">,</span> y<span style="color: #009900;">[</span>indices<span style="color: #009900;">]</span> <span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
corxy.b = <a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=datos<span style="color: #339933;">,</span> statistic=correla<span style="color: #339933;">,</span> R=R <span style="color: #009900;">)</span>
corxy.b
<span style="color: #666666; font-style: italic;">#ORDINARY NONPARAMETRIC BOOTSTRAP</span>
<span style="color: #666666; font-style: italic;">#Call:</span>
<span style="color: #666666; font-style: italic;">#boot(data = datos, statistic = correla, R = R)</span>
<span style="color: #666666; font-style: italic;">#Bootstrap Statistics :</span>
<span style="color: #666666; font-style: italic;">#original bias std. error</span>
<span style="color: #666666; font-style: italic;">#t1* 0.8285718 0.01169248 0.08414495</span>
<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span> corxy.b<span style="color: #339933;">,</span> conf=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.95</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> type=<span style="color: blue;">"all"</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS</span>
<span style="color: #666666; font-style: italic;">#Based on 999 bootstrap replicates</span>
<span style="color: #666666; font-style: italic;">#CALL :</span>
<span style="color: #666666; font-style: italic;">#boot.ci(boot.out = corxy.b, conf = c(0.9, 0.95), type = "all")</span>
<span style="color: #666666; font-style: italic;">#Intervals :</span>
<span style="color: #666666; font-style: italic;">#Level Normal Basic</span>
<span style="color: #666666; font-style: italic;">#90% ( 0.6785, 0.9553 ) ( 0.7048, 0.9575 )</span>
<span style="color: #666666; font-style: italic;">#95% ( 0.6520, 0.9818 ) ( 0.6929, 1.0093 )</span>
<span style="color: #666666; font-style: italic;">#Level Percentile BCa</span>
<span style="color: #666666; font-style: italic;">#90% ( 0.6997, 0.9523 ) ( 0.5930, 0.9213 )</span>
<span style="color: #666666; font-style: italic;">#95% ( 0.6478, 0.9642 ) ( 0.5170, 0.9407 )</span>
<span style="color: #666666; font-style: italic;">#Calculations and Intervals on Original Scale</span>
<span style="color: #666666; font-style: italic;">#Some BCa intervals may be unstable</span>
<span style="color: #666666; font-style: italic;">#Warning message:</span>
<span style="color: #666666; font-style: italic;">#In boot.ci(corxy.b, conf = c(0.9, 0.95), type = "all") : bootstrap variances needed for studentized intervals</span>
<a href="http://inside-r.org/r-doc/grDevices/windows"><span style="color: #003399; font-weight: bold;">windows</span></a><span style="color: #009900;">(</span> width=<span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> height=<span style="color: #cc66cc;">4</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span> pty=<span style="color: blue;">"s"</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span> corxy.b<span style="color: #339933;">,</span> cex.main=<span style="color: #cc66cc;">0.8</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a><span style="color: #009900;">(</span> main=<span style="color: blue;">"Correlaci?n: corxy.b"</span><span style="color: #339933;">,</span> font.main=<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/line"><span style="color: #003399; font-weight: bold;">line</span></a>=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> col.main=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">##### Bootstarap No Parametrico</span>
B = <span style="color: #cc66cc;">1000</span>
<a href="http://inside-r.org/r-doc/base/set.seed"><span style="color: #003399; font-weight: bold;">set.seed</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">98725</span> <span style="color: #009900;">)</span>
corxy.boot = <span style="color: black; font-weight: bold;">NULL</span>
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:B<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/packages/cran/STAR">star</a> = <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:n<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span>
xstar = x<span style="color: #009900;">[</span><a href="http://inside-r.org/packages/cran/STAR">star</a><span style="color: #009900;">]</span>
ystar = y<span style="color: #009900;">[</span><a href="http://inside-r.org/packages/cran/STAR">star</a><span style="color: #009900;">]</span>
corxy.boot<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span> = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>xstar<span style="color: #339933;">,</span> ystar<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
qcorxy.boot = <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>corxy.boot<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.025</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.05</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.95</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span>qcorxy.boot<span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#2.5% 5% 50% 95% 97.5%</span>
<span style="color: #666666; font-style: italic;">#0.7136 0.7341 0.8415 0.9258 0.9365</span>
<span style="color: #666666; font-style: italic;">#Los percentiles 2.5% y 97.5% determinan los extremos de un IC bootstrap percentil del 95%.</span>
bias.corxy = cxy - <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>corxy.boot<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### sesgo bootstrap</span>
se.corxy.boot = <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>corxy.boot<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### se bootstrap</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span>cxy<span style="color: #339933;">,</span> se.corxy.boot<span style="color: #339933;">,</span> bias.corxy<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>corxy.boot<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#cxy se.corxy.boot bias.corxy</span>
<span style="color: #666666; font-style: italic;">#[1,] 0.8286 0.0574 -0.0091 0.8377</span>
<span style="color: #666666; font-style: italic;">#El error estandar bootstrap de rho. se ha calculado mediante...</span>
<span style="color: #666666; font-style: italic;">#El estimador de la correlacion es sesgado, pero en este caso el sesgo estimado es pequeno, de modo que la correccion del sesgo apenas cambiara el valor estimado de la correlacion. El estimador bootstrap del sesgo es (1/B)*sum(rhp^p*-rho), y por lo tanto, un estimador de la correlacion con correccion del sesgo viene dado por: 2rho-(1/B)*sum(rho^b).</span>
<span style="color: #666666; font-style: italic;">#Si la distribucion de la correlacion muestral fuera aproximadamente normal, un IC del tipo Wald, seria muy sencillo de calcular, utilizando la estimacion bootstrap de su error estandar:</span>
<span style="color: #666666; font-style: italic;">### IC tipo Wald del 95% ###</span>
wic.inf = cxy - <a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span> * se.corxy.boot
wic.sup = cxy + <a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.975</span><span style="color: #009900;">)</span> * se.corxy.boot
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> wic.inf<span style="color: #339933;">,</span> cxy<span style="color: #339933;">,</span> wic.sup<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#wic.inf cxy wic.sup</span>
<span style="color: #666666; font-style: italic;">#[1,] 0.7161 0.8286 0.941</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> corxy.boot<span style="color: #339933;">,</span> probability=T<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> main=<span style="color: blue;">""</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> qcorxy.boot <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>v=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>qcorxy.boot<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> cxy<span style="color: #009900;">)</span><span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">### IC percentil bootstrap 95%</span>
<a href="http://inside-r.org/r-doc/graphics/rug"><span style="color: #003399; font-weight: bold;">rug</span></a><span style="color: #009900;">(</span> cxy<span style="color: #339933;">,</span> lwd=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#La transformaci?n tau de Fisher, bootstrap:</span>
tau.boot = <span style="color: #cc66cc;">0.5</span>*<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span> <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+corxy.boot<span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-corxy.boot<span style="color: #009900;">)</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span> tau.boot<span style="color: #339933;">,</span> probability=T<span style="color: #339933;">,</span> breaks=<span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> main=<span style="color: blue;">""</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/boot/tau"><span style="color: #003399; font-weight: bold;">tau</span></a><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>/sqrt<span style="color: #009900;">(</span>n-<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> add=T <span style="color: #009900;">)</span>
qb.025 = <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span> corxy.boot<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.025</span> <span style="color: #009900;">)</span>
qb.975 = <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span> corxy.boot<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.975</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span> v=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/boot/tau"><span style="color: #003399; font-weight: bold;">tau</span></a><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.5</span>*<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+qb.025<span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-qb.025<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.5</span>*<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+qb.975<span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-qb.975<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span> <span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## Basado en http://www.ats.ucla.edu/stat/R/library/bootstrap.htm</span>
<span style="color: #666666; font-style: italic;">#calculating the standard error of the median</span>
<span style="color: #666666; font-style: italic;">#creating the data set by taking 100 observations </span>
<span style="color: #666666; font-style: italic;">#from a normal distribution with mean 5 and stdev 3</span>
<span style="color: #666666; font-style: italic;">#we have rounded each observation to nearest integer</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> = <a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">]</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span>
<span style="color: #666666; font-style: italic;">#obtaining 20 bootstrap samples </span>
<span style="color: #666666; font-style: italic;">#display the first of the bootstrap samples</span>
resamples = <a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>i<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a> = T<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
resamples<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span>:
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">7</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">49</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">73</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">97</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">7</span>
<span style="color: #666666; font-style: italic;">#calculating the median for each bootstrap sample </span>
r.median = <a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>resamples<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">)</span>
r.median
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">4.0</span> <span style="color: #cc66cc;">4.5</span> <span style="color: #cc66cc;">4.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">4.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">4.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">4.0</span> <span style="color: #cc66cc;">5.0</span> <span style="color: #cc66cc;">5.0</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">19</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">6.0</span> <span style="color: #cc66cc;">5.0</span>
<span style="color: #666666; font-style: italic;">#calculating the standard deviation of the distribution of medians</span>
<a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>r.median<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.5250313</span>
<span style="color: #666666; font-style: italic;">#displaying the histogram of the distribution of the medians </span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>r.median<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#We can put all these steps into a single function where all we would need to specify is which data set to use and how many times we want to resample in order to obtain the adjusted standard error of the median. For more information on how to construct functions please consult the R library pages on introduction to functions and advanced functions.</span>
<span style="color: #666666; font-style: italic;">#function which will bootstrap the standard error of the median</span>
b.median = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> num<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
resamples = <a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:num<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>i<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
r.median = <a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>resamples<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">)</span>
std.err = <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>r.median<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>std.err=std.err<span style="color: #339933;">,</span> resamples=resamples<span style="color: #339933;">,</span> medians=r.median<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#generating the data to be used (same as in the above example)</span>
data1 = <a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#saving the results of the function b.median in the object b1</span>
b1 = b.median<span style="color: #009900;">(</span>data1<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#displaying the first of the 30 bootstrap samples</span>
b1$resamples<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span>:
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">2</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">49</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">6</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">73</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">6</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">97</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span>
<span style="color: #666666; font-style: italic;">#displaying the standard error</span>
b1$std.err
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.5155477</span>
<span style="color: #666666; font-style: italic;">#displaying the histogram of the distribution of medians</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>b1$medians<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#we can input the data directly into the function and display </span>
<span style="color: #666666; font-style: italic;">#the standard error in one line of code</span>
b.median<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>$std.err
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.5104178</span>
<span style="color: #666666; font-style: italic;">#displaying the histogram of the distribution of medians</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>b.median<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>$medians<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Basado en http://www.statoo.com/en/publications/bootstrap_scgn_v131.pdf</span>
<span style="color: #666666; font-style: italic;"># los datos son de un experimento en el cual se utilizan aleatoriamente dos tratamientos de l?ser en los ojos de los pacientes. La respuesta es la adecuaci?n visual, medida por el n?mero de letras identificadas correctamente en un test est?ndard de ojos. </span>
<span style="color: #666666; font-style: italic;"># algunos pacientes tienen solo un ojo sano, y reciben un tratamiento localizado aleatoriamente. Existen 20 apcientes con datos pareados y 20 pacientes con con solo 1 observaci?n disponible, por lo que tenemos una mezcla de comparaciones pareadas y dos muestras.</span>
blue=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">69</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">87</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">79</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">31</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">95</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">68</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">62</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">70</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">84</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">79</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">75</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">59</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">77</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">86</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">39</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">85</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">74</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">69</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">85</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">85</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">72</span><span style="color: #009900;">)</span>
red=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">62</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">82</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">83</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">81</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">28</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">69</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">48</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">63</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">77</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">83</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">85</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">54</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">58</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">68</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">88</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">83</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">78</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">58</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">45</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">78</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">87</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">65</span><span style="color: #009900;">)</span>
acui=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/str"><span style="color: #003399; font-weight: bold;">str</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> red<span style="color: #339933;">,</span> blue<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># solo consideraremos los datos pareados y construiremos el cl?sico intervalo de confianza al 95% del estad?stico t-Student para la media de las diferencias.</span>
acu.pd=acui<span style="color: #009900;">[</span>acui$str==<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
dif=acu.pd$blue-acu.pd$red
n=<a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a><span style="color: #009900;">(</span>acu.pd<span style="color: #009900;">)</span>
tmp=<a href="http://inside-r.org/r-doc/stats/qt"><span style="color: #003399; font-weight: bold;">qt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.025</span><span style="color: #339933;">,</span> n-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>dif<span style="color: #009900;">)</span>/sqrt<span style="color: #009900;">(</span>n<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dif<span style="color: #009900;">)</span>+tmp<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dif<span style="color: #009900;">)</span>-tmp<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># [1] -.9270336 15.770335 </span>
<span style="color: #666666; font-style: italic;"># pero un q-q plot de las diferencias parece ma?s una Cauchy que una normal, por lo cual el modelo usual parece ser inviable. Para realizar un boostrap no-param?trico en este caso, necesitamos definir la funci?n boostrap:</span>
acu.pd.fun=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> i<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
d=<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">[</span>i<span style="color: #339933;">,</span><span style="color: #009900;">]</span>
dif=d$blue-d$red
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>dif<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>dif<span style="color: #009900;">)</span>/nrow<span style="color: #009900;">(</span>d<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># obtenemos R=999 r?plicas bootstrap </span>
acui.pd.b=<a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span>acu.pd<span style="color: #339933;">,</span> acu.pd.fun<span style="color: #339933;">,</span> R=<span style="color: #cc66cc;">999</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># los IC no-param?tricos resultantes pueden calcularse como antes, o utilizando directamente:</span>
<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span>acu.pd.b<span style="color: #339933;">,</span> type=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"norm"</span><span style="color: #339933;">,</span> <span style="color: blue;">"basic"</span><span style="color: #339933;">,</span> <span style="color: blue;">"stud"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Normal Basic Studentized</span>
<span style="color: #666666; font-style: italic;">#(-8.20, 14.95) (-8.10, 15.05) (-8.66, 15.77)</span>
<span style="color: #666666; font-style: italic;"># una alternatica es considerar solo los datos de dos muestras y comparar las medias de las dos poblaciones issuing los pacientes que solo tienen una observacion disponible</span>
acu.ts=acui<span style="color: #009900;">[</span>acui$str==<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># calculamos el intervalo de confianza al 05% para la diferencia de medias</span>
acu.ts=acui<span style="color: #009900;">[</span>acui$str==<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
dif=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>acu.ts$blue<span style="color: #009900;">)</span>-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>acu.ts$red<span style="color: #009900;">)</span>
tmp=<a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.025</span><span style="color: #009900;">)</span>*<a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>acu.ts$blue<span style="color: #009900;">)</span>/nrow<span style="color: #009900;">(</span>acu.ts<span style="color: #009900;">)</span>+<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>acu.ts$red<span style="color: #009900;">)</span>/nrow<span style="color: #009900;">(</span>acu.ts<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>dif+tmp<span style="color: #339933;">,</span> dif-tmp<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#[1] -13.76901 19.16901</span>
<span style="color: #666666; font-style: italic;"># para construir IC boostrap generamos R=999 r?plicas del estimador y varianza estimada</span>
y=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>acui$blue<span style="color: #009900;">[</span><span style="color: #cc66cc;">21</span>:<span style="color: #cc66cc;">30</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> acui$red<span style="color: #009900;">[</span><span style="color: #cc66cc;">21</span>:<span style="color: #cc66cc;">30</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
acu=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
acu.ts.f=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span> i<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
d=<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">[</span>i<span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
m=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>d$y<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>d$y<span style="color: #009900;">[</span><span style="color: #cc66cc;">11</span>:<span style="color: #cc66cc;">20</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
v=<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>d$y<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>/<span style="color: #cc66cc;">10</span>+<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>d$y<span style="color: #009900;">[</span><span style="color: #cc66cc;">11</span>:<span style="color: #cc66cc;">20</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>/<span style="color: #cc66cc;">10</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span>v<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
acu.ts.boot=<a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span>acu<span style="color: #339933;">,</span> acu.ts.f<span style="color: #339933;">,</span> R=<span style="color: #cc66cc;">999</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/survival/strata">strata</a>=acu$col<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># strata=acu$col asegura la simulaci?n estratificada</span>
<span style="color: #666666; font-style: italic;">### Basado en http://thebiobucket.blogspot.com/2011/04/bootstrap-with-strata.html</span>
<span style="color: #666666; font-style: italic;"># Here's a worked example for comparing group averages with bootstrap confidence intervals and allowing for different subsample sizes by calling the strata argument within the bootstrap function.</span>
<span style="color: #666666; font-style: italic;"># The data is set up analogous to an before-after impact experiment conducted on plots across four different successional stages. Similarity was calculated for plot's composition before and after an impact. One hypothesis was that certain stages would show higher / smaller average similarities, that is, a higher / lower impact on composition. As plots within stages were situated within different subsites and the nr. of replicates was unbalanced, this had to be allowed for by use of the "strata" argument in the boot.ci call:</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/Hmisc">Hmisc</a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### a factor 'stage' and a variable 'MH-Index'</span>
stage=<a href="http://inside-r.org/r-doc/base/sort"><span style="color: #003399; font-weight: bold;">sort</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/as.factor"><span style="color: #003399; font-weight: bold;">as.factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">LETTERS</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
site=<a href="http://inside-r.org/r-doc/base/gl"><span style="color: #003399; font-weight: bold;">gl</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
MH.Index=<a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span>*<span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### simulate effect:</span>
n = <span style="color: #cc66cc;">30</span>
eff = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.25</span><span style="color: #339933;">,</span> n<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.4</span><span style="color: #339933;">,</span> n<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span> n<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">0.65</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
MH.Index = MH.Index*eff
<span style="color: #666666; font-style: italic;">### dataframe:</span>
<a href="http://inside-r.org/r-doc/base/print"><span style="color: #003399; font-weight: bold;">print</span></a><span style="color: #009900;">(</span>sim=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>stage<span style="color: #339933;">,</span> site<span style="color: #339933;">,</span> MH.Index<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### function needed for the bootstrap:</span>
mean.fun = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> index<span style="color: #009900;">)</span><span style="color: #009900;">{</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">[</span>index<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">### set up dataframe to collect results for plotting:</span>
pldat=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a>=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>low=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>upp=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/rownames"><span style="color: #003399; font-weight: bold;">rownames</span></a><span style="color: #009900;">(</span>pldat<span style="color: #009900;">)</span>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"A"</span><span style="color: #339933;">,</span><span style="color: blue;">"B"</span><span style="color: #339933;">,</span><span style="color: blue;">"C"</span><span style="color: #339933;">,</span><span style="color: blue;">"D"</span><span style="color: #009900;">)</span>
pldat
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"A"</span><span style="color: #339933;">,</span><span style="color: blue;">"B"</span><span style="color: #339933;">,</span><span style="color: blue;">"C"</span><span style="color: #339933;">,</span><span style="color: blue;">"D"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>
<span style="color: #666666; font-style: italic;">### the boot sample and the ci's:</span>
<a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a>=<a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #009900;">(</span>sim$MH.Index<span style="color: #009900;">[</span>sim$stage==i<span style="color: #009900;">]</span><span style="color: #339933;">,</span> mean.fun<span style="color: #339933;">,</span> R=<span style="color: #cc66cc;">1000</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/survival/strata">strata</a>=sim$site<span style="color: #009900;">[</span>sim$stage==i<span style="color: #009900;">]</span><span style="color: #009900;">)</span>
ci=<a href="http://inside-r.org/r-doc/boot/boot.ci"><span style="color: #003399; font-weight: bold;">boot.ci</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/boot/boot"><span style="color: #003399; font-weight: bold;">boot</span></a><span style="color: #339933;">,</span>type = <span style="color: blue;">"norm"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### get ci's (method: normal)</span>
ci<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>->pldat<span style="color: #009900;">[</span>i<span style="color: #339933;">,</span><span style="color: blue;">"mean"</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># mean</span>
ci$normal<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>->pldat<span style="color: #009900;">[</span>i<span style="color: #339933;">,</span><span style="color: blue;">"low"</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># lower ci</span>
ci$normal<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>->pldat<span style="color: #009900;">[</span>i<span style="color: #339933;">,</span><span style="color: blue;">"upp"</span><span style="color: #009900;">]</span><span style="color: #009900;">}</span> <span style="color: #666666; font-style: italic;"># upper ci</span>
pldat
<span style="color: #666666; font-style: italic;">### plot:</span>
errbar<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>pldat$mean<span style="color: #339933;">,</span>pldat$upp<span style="color: #339933;">,</span>pldat$low<span style="color: #339933;">,</span>ylab=<span style="color: blue;">"MH-Similarity Index"</span><span style="color: #339933;">,</span>xlab=<span style="color: blue;">"Stage"</span><span style="color: #339933;">,</span>
pch=<span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span>xaxt=<span style="color: blue;">"n"</span><span style="color: #339933;">,</span>xlim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4.5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/axis"><span style="color: #003399; font-weight: bold;">axis</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a><span style="color: #009900;">(</span>pldat<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/legend"><span style="color: #003399; font-weight: bold;">legend</span></a><span style="color: #009900;">(</span><span style="color: blue;">"topleft"</span><span style="color: #339933;">,</span><span style="color: blue;">"errorbars =<span style="color: #000099; font-weight: bold;">\n</span>95%-normal<span style="color: #000099; font-weight: bold;">\n</span>bootstrap<span style="color: #000099; font-weight: bold;">\n</span>conf. int"</span><span style="color: #339933;">,</span>bty=<span style="color: blue;">"n"</span><span style="color: #009900;">)</span>
</pre>
</div>
</div>
<br />
<br />
<h3>
EJEMPLOS: en el siguiente link podrán ver aplicaciones del boostrap en índices de biodiversidad.</h3>
<a href="http://www.wcsmalaysia.org/analysis/Biod_indices_bstrap.htm">http://www.wcsmalaysia.org/analysis/Biod_indices_bstrap.htm</a>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com3tag:blogger.com,1999:blog-6841022238556785612.post-48816657383373193292012-08-21T06:45:00.000-07:002012-08-21T15:39:37.106-07:00Clase 4: Contrastes de hipótesis en R-project<div style="overflow: auto;">
<div class="geshifilter">
<h2 style="text-align: center;">
Inferencia: Contraste de hipótesis</h2>
<br />
Continuando con el breve curso de R que he presentado (<a href="http://statisticalecology-ec.blogspot.com.es/2012/08/clase-1-manejo-de-datos-en-r-project.html">Clase 1</a>, <a href="http://statisticalecology-ec.blogspot.com.es/2012/08/clase-2-analisis-unidimensional-en-r.html">Clase 2</a>, <a href="http://statisticalecology-ec.blogspot.com.es/2012/08/clase-3-distribuciones-de-probabilidad.html">Clase 3</a>), en esta entrada trataremos la Estadística Inductiva o Inferencia Estadística es un conjunto de métodos que se fundamentan en la Teoría de la Probabilidad y que tienen por finalidad generalizar los resultados, obtenidos mediante una muestra, a toda una población. Es decir, se trata de procedimientos para aceptar o rechazar una hipótesis (una afirmación de la realidad) acerca de un parámetro o característica poblacional a partir de una muestra de la población.<br />
Los contrastes pueden ser paramédicos o no paramédicos según asuman una determinada distribución de los datos o no. Los contrastes presentan una hipótesis nula H0 (la que queremos contrastar) y una hipótesis alternativa H1 (lo que ocurre si no es cierta la hipótesis nula). Se dice que un contraste es significativo cuando se rechaza H0; y es no-significativo cuando se acepta H0 (o mejor dicho, cuando no rechazamos H0).<br />
<br />
La elección de la prueba de contraste que debemos aplicar depende de nuestros datos y las preguntas que nos hacemos sobre ellos. Aquí les dejo un esquema muy ilustrativo sobre ello:<br />
<br />
<br />
<a href="http://www.scribd.com/doc/103453503/Inferencial"></a><a href="http://www.scribd.com/doc/103453503/Inferencial" style="display: inline !important; font-family: Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; margin-bottom: 6px; margin-left: auto; margin-right: auto; margin-top: 12px; text-decoration: underline;" title="View Inferencial on Scribd">Inferencial</a><br />
<iframe class="scribd_iframe_embed" data-aspect-ratio="0.706697459584296" data-auto-height="true" frameborder="0" height="600" id="doc_32458" scrolling="no" src="http://www.scribd.com/embeds/103453503/content?start_page=1&view_mode=scroll&access_key=key-af54olx9dbswgnkswt2" width="100%"></iframe>
<br />
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<h4>
</h4>
<h4>
Contrastes paramétricos</h4>
<div>
Asumen la distribución Normal de los datos.</div>
<div>
Existen contrastes para cualquiera de los parámetros poblacionales: media, varianza, proporción; para 1 o para más muestras.</div>
<div>
<br />
<br />
<div>
<h4>
Contrastes no paramétricos</h4>
</div>
<div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
No asumen Normalidad.</div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<br /></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<br /></div>
<h3>
Aplicación en R</h3>
</div>
</div>
<div>
<b>Contraste para el promedio de una muestra</b></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgm_oetMNFx6YLOyujgarjb3eDkSgD2OkjhRT_v2g6IxpH4notR-28yt1kvuXcSH6o0rO1xDn9hL28ssDUlcWKIVKMKJ85tPHFNfy_VuHqe1KYCUcpjYbDuv7FTlAQM_Y32jIZ9JqgOcys/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="164" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgm_oetMNFx6YLOyujgarjb3eDkSgD2OkjhRT_v2g6IxpH4notR-28yt1kvuXcSH6o0rO1xDn9hL28ssDUlcWKIVKMKJ85tPHFNfy_VuHqe1KYCUcpjYbDuv7FTlAQM_Y32jIZ9JqgOcys/s640/1.png" width="640" /></a></div>
<br />
Ejemplo:<br />
<br />
<div style="overflow-x: auto; overflow-y: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>
<span style="color: #666666; font-style: italic;">#contraste de una muestra: promedio o media</span>
<a href="http://inside-r.org/r-doc/stats/t.test"><span style="color: #003399; font-weight: bold;">t.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>$extra<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/t.test"><span style="color: #003399; font-weight: bold;">t.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>$extra<span style="color: #339933;">,</span>mu=<span style="color: #cc66cc;">1.4</span><span style="color: #339933;">,</span> conf.level = <span style="color: #cc66cc;">0.95</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#two.sided</span>
<a href="http://inside-r.org/r-doc/stats/t.test"><span style="color: #003399; font-weight: bold;">t.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>$extra<span style="color: #339933;">,</span>mu=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>alternative=<span style="color: blue;">"greater"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/t.test"><span style="color: #003399; font-weight: bold;">t.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>$extra<span style="color: #339933;">,</span>mu=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>alternative=<span style="color: blue;">"less"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/shapiro.test"><span style="color: #003399; font-weight: bold;">shapiro.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>$extra<span style="color: #009900;">) #para probar si existe normalidad en los datos, es decir, si los datos se distribuyen según una Normal.</span>
<a href="http://inside-r.org/r-doc/stats/wilcox.test"><span style="color: #003399; font-weight: bold;">wilcox.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a>$extra<span style="color: #339933;">,</span>alternative=<span style="color: blue;">"less"</span><span style="color: #339933;">,</span>mu=<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#sin asumir normalidad</span>
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span">
</span><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><b>Contraste para la proporción de una muestra</b></span><span class="Apple-style-span">
<span style="color: #666666; font-style: italic;">#contraste para una muestra: proporción</span>
heads <- <a href="http://inside-r.org/r-doc/stats/rbinom"><span style="color: #003399; font-weight: bold;">rbinom</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> size=<span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/packages/cran/prob">prob</a> = <span style="color: #cc66cc;">.5</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/prop.test"><span style="color: #003399; font-weight: bold;">prop.test</span></a><span style="color: #009900;">(</span>heads<span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#contraste para dos muestras: igualdad de proporción</span>
smokers <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">83</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">129</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">70</span> <span style="color: #009900;">)</span>
patients <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span> <span style="color: #cc66cc;">86</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">93</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">136</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">82</span> <span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/prop.test"><span style="color: #003399; font-weight: bold;">prop.test</span></a><span style="color: #009900;">(</span>smokers<span style="color: #339933;">,</span> patients<span style="color: #009900;">)</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span"><span style="color: #009900;">
</span></span></pre>
</div>
</div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<br /></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<br /></div>
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<b>Contraste de igualdad de medias</b></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcdhkccrQFwz-rLvBbcKQvJ-nWrTb6NZUwrZs6EDAoWvlmqvIHifzrapX-msoCpU7_D-iSjmvN83e9cb-wMIbXmw9Qd8EYuApZzRBlQ2sp_STuo3o-27vIEEeLjFU9UhbxnzyRlvsFhp4/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcdhkccrQFwz-rLvBbcKQvJ-nWrTb6NZUwrZs6EDAoWvlmqvIHifzrapX-msoCpU7_D-iSjmvN83e9cb-wMIbXmw9Qd8EYuApZzRBlQ2sp_STuo3o-27vIEEeLjFU9UhbxnzyRlvsFhp4/s640/2.png" width="640" /></a></div>
<br />
Ejemplo<br />
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">#contraste de dos muestras: igualdad de medias</span>
<a href="http://inside-r.org/r-doc/stats/t.test"><span style="color: #003399; font-weight: bold;">t.test</span></a><span style="color: #009900;">(</span>extra ~ <a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> = <a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a><span style="color: #339933;">,</span> var.equal=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#t.test para muestras independientes</span>
<span style="color: #666666; font-style: italic;">#t.test para muestras relacionadas</span>
<a href="http://inside-r.org/r-doc/stats/wilcox.test"><span style="color: #003399; font-weight: bold;">wilcox.test</span></a><span style="color: #009900;">(</span>extra ~ <a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> = <a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a><span style="color: #339933;">,</span> alternative = <span style="color: blue;">"less"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#sin asumir normalized</span>
</pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #666666; font-style: italic;">
</span></pre>
<div>
<span style="color: #666666; font-style: italic;"><span class="Apple-style-span" style="color: black; font-style: normal;"><b>Contraste para la igualdad de varianzas</b></span></span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUhATgINc33QyBLnK04O4OIAAsmOWUBa5j4zULbP1sZJMP1ixbVd8XPnXPN3DtkKHFD3TigWr3PFYDU_O1LSpOzBFwikcAPswf2TQ03yFkgAJFwgGP6TUk6MQnxK79ppf_p0ZKSkEO8H8/s1600/3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="133" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUhATgINc33QyBLnK04O4OIAAsmOWUBa5j4zULbP1sZJMP1ixbVd8XPnXPN3DtkKHFD3TigWr3PFYDU_O1LSpOzBFwikcAPswf2TQ03yFkgAJFwgGP6TUk6MQnxK79ppf_p0ZKSkEO8H8/s400/3.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
Ejemplo</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><div style="font-family: monospace; text-align: left;">
<span class="Apple-style-span" style="color: #666666; font-style: italic;">#contraste de dos muestras: cociente de varianzas</span></div>
<div style="font-family: monospace; text-align: left;">
<a href="http://inside-r.org/r-doc/stats/var.test"><span style="color: #003399; font-weight: bold;">var.test</span></a><span style="color: #009900;">(</span>extra ~ <a href="http://inside-r.org/r-doc/grDevices/group"><span style="color: #003399; font-weight: bold;">group</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> = <a href="http://inside-r.org/r-doc/datasets/sleep"><span style="color: #003399; font-weight: bold;">sleep</span></a><span style="color: #339933;">,</span> alternative = <span style="color: blue;">"two.sided"</span><span style="color: #339933;">,</span> conf.level = <span style="color: #cc66cc;">0.95</span><span style="color: #009900;">)</span></div>
<div style="font-family: monospace; text-align: left;">
levene.test<span style="color: #009900;">(</span>acero$pr.galv1<span style="color: #339933;">,</span> acero$averias<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#sin asumir normalidad</span></div>
bartlett.test(consumo ~ temperatura, data = acero) #para más de 2 muestras</pre>
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: left;"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><b>Contraste de medias para más de dos muestras: </b></span></pre>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvBmcfkjD4cjFnRiAiaDj5lp4ZulDYYCw1C3iH1qnxi4CzIqmbbhDLFxZwA29uUK2HN0MPSlUoTAXDqy3D_2b8sh2Q28K5bXqGOYPb6WmJ3MCF984Uvnq0mk-9ei8-7icvCeng3hIX114/s1600/5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="113" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvBmcfkjD4cjFnRiAiaDj5lp4ZulDYYCw1C3iH1qnxi4CzIqmbbhDLFxZwA29uUK2HN0MPSlUoTAXDqy3D_2b8sh2Q28K5bXqGOYPb6WmJ3MCF984Uvnq0mk-9ei8-7icvCeng3hIX114/s400/5.png" width="400" /></a></div>
<div>
<br /></div>
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />> Anova1 <- aov(consumo ~ temperatura, data = acero)
> summary(Anova1)
<br /><br /><br />#comparaciones múltiples<br />> comparacion <- glht(Anova1, linfct = mcp(temperatura = "Tukey"))
> summary(comparacion)
<br /><br /><br />#si no hay homocedasticidad<br /> kruskal.test(consumo ~ linea, data = acero) <br /><br /><br /></span></div>
<div class="geshifilter">
<h4>
Aplicación en R mediante el paquete ctest</h4>
</div>
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Analisis de datos con R #</span>
<span style="color: #666666; font-style: italic;"># 2012 #</span>
<span style="color: #666666; font-style: italic;"># Msc. Rosana Ferrero #</span>
<span style="color: #666666; font-style: italic;"># http://statisticalecology.blogspot.com/ #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 4. Contrastes de hip?tesis #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#Existe en R una librer?a de funciones para realizar contrastes estad?sticos, llamada ctest.</span>
<span style="color: #666666; font-style: italic;">#Para cargarla, escribiremos:</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span>ctest<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Dicha librer?a contiene bastantes funciones, pero s?lo nos vamos a fijar en algunas que implementan</span>
<span style="color: #666666; font-style: italic;">#contrastes muy com?nmente utilizados. Las funciones que vamos a ver son las siguientes:</span>
<span style="color: #666666; font-style: italic;"># binom.test Test exacto sobre el par?metro de una binomial</span>
<span style="color: #666666; font-style: italic;"># cor.test Test de asociaci?n entre muestras apareadas</span>
<span style="color: #666666; font-style: italic;"># wilcox.test Test de suma de rangos de Wilcoxon para una y dos muestras</span>
<span style="color: #666666; font-style: italic;"># prop.test Test de igualdad de proporciones</span>
<span style="color: #666666; font-style: italic;"># chisq.test Test de la chi-cuadrado para datos de conteo</span>
<span style="color: #666666; font-style: italic;"># fisher.test Test exacto de Fisher para datos de conteo</span>
<span style="color: #666666; font-style: italic;"># ks.test Test de Kolmogorov-Smirnov para ajuste de datos a distribuciones dadas</span>
<span style="color: #666666; font-style: italic;"># shapiro.test Test de Shapiro para comprobar ajuste de datos a una distribuci?n normal</span>
<span style="color: #666666; font-style: italic;"># oneway.test Test para comprobar la igualdad de medias entre varios grupos de datos</span>
<span style="color: #666666; font-style: italic;"># var.test Test para comprobar la igualdad de varianzas entre dos grupos de datos</span>
<span style="color: #666666; font-style: italic;">#Vamos a poner ejemplos de cada contraste, para ver qu? tipo de problemas resuelven y tener una base para su aplicaci?n.</span>
<span style="color: #666666; font-style: italic;">#### binom.test ################################################################</span>
<span style="color: #666666; font-style: italic;">#Lleva a cabo un contraste exacto (no aproximado) sobre el valor de la probabilidad de ?xito en un experimento de Bernoulli.</span>
<span style="color: #666666; font-style: italic;">#Por ejemplo, tiramos una moneda al aire 600 veces, y nos salen 300 caras. Y queremos contrastar la hip?tesis de que la moneda est? equilibrada, esto es, la probabilidad de que p=0.5. Para ello diremos cu?ntas veces sali? cara y cu?ntas veces sali? cruz:</span>
<a href="http://inside-r.org/r-doc/stats/binom.test"><span style="color: #003399; font-weight: bold;">binom.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">300</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>p=<span style="color: #cc66cc;">0.5</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Exact binomial test</span>
<span style="color: #666666; font-style: italic;"># data: 300 and 600</span>
<span style="color: #666666; font-style: italic;"># number of successes = 300, number of trials = 600, p-value = 1</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true probability of success is not equal to 0.5</span>
<span style="color: #666666; font-style: italic;"># 95 percent confidence interval:</span>
<span style="color: #666666; font-style: italic;"># 0.4592437 0.5407563</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># probability of success</span>
<span style="color: #666666; font-style: italic;"># 0.5</span>
<span style="color: #666666; font-style: italic;">## Como el p-valor ha salido 1 (es lo m?s alto posible), no rechazaremos la hip?tesis nula. Esto es, daremos por bueno que la moneda estaba equilibrada. Observamos que tambi?n obtenemos un intervalo de confianza al 95% para el verdadero valor de la probabilidad de obtener cara. Dicho intervalo es (0.459, 0.540) en nuestro ejemplo.</span>
<span style="color: #666666; font-style: italic;">#Otro ejemplo: tiramos la moneda al aire y obtenemos 30 caras y 100 cruces:</span>
<a href="http://inside-r.org/r-doc/stats/binom.test"><span style="color: #003399; font-weight: bold;">binom.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>p=<span style="color: #cc66cc;">0.5</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Exact binomial test</span>
<span style="color: #666666; font-style: italic;"># data: 30 and 130</span>
<span style="color: #666666; font-style: italic;"># number of successes = 30, number of trials = 130, p-value = 5.421e-10</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true probability of success is not equal to 0.5</span>
<span style="color: #666666; font-style: italic;"># 95 percent confidence interval:</span>
<span style="color: #666666; font-style: italic;"># 0.1614375 0.3127614</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># probability of success</span>
<span style="color: #666666; font-style: italic;"># 0.2307692</span>
<span style="color: #666666; font-style: italic;">##En este caso, el p-valor es pr?cticamente cero, lo que quiere decir que debemos rechazar la hip?tesis de que la moneda est? equilibrada. Como el intervalo de confianza resulta ser (0.16, 0.31), concluiremos que la probabilidad de obtener cara para la moneda utilizada est? probablemente dentro de dicho intervalo.</span>
<span style="color: #666666; font-style: italic;">#### prop.test #################################################################</span>
<span style="color: #666666; font-style: italic;">#Con esta funci?n contrastaremos si las proporciones en varios grupos son las mismas, o bien que dichas proporciones equivalen a unas determinadas.</span>
<span style="color: #666666; font-style: italic;">#Para empezar, tiraremos una moneda al aire 100 veces, y nos preguntaremos si la proporci?n de caras puede ser considerada el 50% :</span>
caras = <a href="http://inside-r.org/r-doc/stats/rbinom"><span style="color: #003399; font-weight: bold;">rbinom</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> size=<span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> pr = <span style="color: #cc66cc;">.5</span><span style="color: #009900;">)</span>
caras
<span style="color: #666666; font-style: italic;">#[1] 51</span>
<a href="http://inside-r.org/r-doc/stats/prop.test"><span style="color: #003399; font-weight: bold;">prop.test</span></a><span style="color: #009900;">(</span>caras<span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># 1-sample proportions test with continuity correction</span>
<span style="color: #666666; font-style: italic;"># data: caras out of 100, null probability 0.5</span>
<span style="color: #666666; font-style: italic;"># X-squared = 0.01, df = 1, p-value = 0.9203</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true p is not equal to 0.5</span>
<span style="color: #666666; font-style: italic;"># 95 percent confidence interval:</span>
<span style="color: #666666; font-style: italic;"># 0.4086512 0.6105719</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># p</span>
<span style="color: #666666; font-style: italic;"># 0.51</span>
<span style="color: #666666; font-style: italic;">## Como el p-valor es muy alto, 0.92, no rechazaremos la hip?tesis nula de que p=0.5. En este caso, el test funciona como en el caso de binom.test.</span>
<span style="color: #666666; font-style: italic;">#Supongamos ahora que queremos saber si dos muestras han salido de la misma poblaci?n.</span>
<span style="color: #666666; font-style: italic;">#Por ejemplo, queremos saber si las mujeres estudian un grupo de carreras diferentes en la misma proporcion. Para ello miramos la lista de matriculados en cuatro carreras y comprobamos si las proporciones de mujeres en las mismas coinciden:</span>
estudiantes = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>estudiantes<span style="color: #009900;">)</span> = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"quimica"</span><span style="color: #339933;">,</span><span style="color: blue;">"derecho"</span><span style="color: #339933;">,</span><span style="color: blue;">"matematicas"</span><span style="color: #339933;">,</span><span style="color: blue;">"economia"</span><span style="color: #009900;">)</span>
estudiantes
<span style="color: #666666; font-style: italic;"># quimica derecho matematicas economia</span>
<span style="color: #666666; font-style: italic;"># 100 200 50 500</span>
estudiantes.mujeres = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">120</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/prop.test"><span style="color: #003399; font-weight: bold;">prop.test</span></a><span style="color: #009900;">(</span>estudiantes.mujeres<span style="color: #339933;">,</span>estudiantes<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># 4-sample test for equality of proportions without continuity correction</span>
<span style="color: #666666; font-style: italic;"># data: estudiantes.mujeres out of estudiantes</span>
<span style="color: #666666; font-style: italic;"># X-squared = 44.5373, df = 3, p-value = 1.160e-09</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: two.sided</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># prop 1 prop 2 prop 3 prop 4</span>
<span style="color: #666666; font-style: italic;"># 0.6 0.6 0.2 0.4</span>
<span style="color: #666666; font-style: italic;">## La conclusion es que no. Las mujeres, en este ejemplo, se matriculan de las carreras en distinta proporcion. Es aparente que se matriculan menos en Matematicas, en este ejemplo, pero el uso del contraste nos asegura con un alto grado de seguridad que esas diferencias observadas no son debidas al azar, sino que son estructurales.</span>
<span style="color: #666666; font-style: italic;">#### chisq.test ################################################################</span>
<span style="color: #666666; font-style: italic;"># Este contraste se utiliza para tablas de contingencia. El caso habitual ser? disponer de una tabla de contingencia que cruza dos variables, contando el n?mero de coincidencias seg?n las categor?as de ambas variables, y se trata de ver si las variables son o no son independientes.</span>
<span style="color: #666666; font-style: italic;"># Para poner un ejemplo, supongamos que tenemos una muestra de personas en las que hemos observado dos variables: el color de ojos, que puede ser "claro" y "oscuro", y el color de pelo, que puede ser "rubio" ? "moreno". Entonces hacemos una tabla de contingencia para cruzar dichas caracter?sticas, y aplicamos el test para ver si el "color de ojos" y el "color de pelo" son variables independientes:</span>
<span style="color: #666666; font-style: italic;"># Suponemos que los datos han sido recogidos previamente en una variable llamada 'datos':</span>
datos
<span style="color: #666666; font-style: italic;"># claro oscuro</span>
<span style="color: #666666; font-style: italic;"># rubio 20 2</span>
<span style="color: #666666; font-style: italic;"># moreno 3 18</span>
<a href="http://inside-r.org/r-doc/stats/chisq.test"><span style="color: #003399; font-weight: bold;">chisq.test</span></a><span style="color: #009900;">(</span>datos<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Pearson's Chi-squared test with Yates' continuity correction</span>
<span style="color: #666666; font-style: italic;"># data: datos</span>
<span style="color: #666666; font-style: italic;"># X-squared = 22.3693, df = 1, p-value = 2.249e-06</span>
<span style="color: #666666; font-style: italic;">## Como el p-valor es pr?cticamente cero, concluiremos que hay que rechazar la hip?tesis nula, que es que hay independencia entre las variables observadas: el color de ojos y el color de pelo guardan correlaci?n en estos datos.</span>
<span style="color: #666666; font-style: italic;">#Repitamos el ejemplo, construyendo la tabla de contingencia para repasar c?mo se hace, para un conjunto de datos distinto y m?s peque?o que el anterior:</span>
color.ojos = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"oscuro"</span><span style="color: #339933;">,</span><span style="color: blue;">"oscuro"</span><span style="color: #339933;">,</span><span style="color: blue;">"oscuro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span> <span style="color: blue;">"oscuro"</span><span style="color: #339933;">,</span><span style="color: blue;">"oscuro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"claro"</span><span style="color: #339933;">,</span><span style="color: blue;">"oscuro"</span><span style="color: #339933;">,</span><span style="color: blue;">"oscuro"</span><span style="color: #009900;">)</span>
color.pelo = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"moreno"</span><span style="color: #339933;">,</span><span style="color: blue;">"moreno"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"moreno"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span> <span style="color: blue;">"moreno"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"moreno"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"rubio"</span><span style="color: #339933;">,</span><span style="color: blue;">"moreno"</span><span style="color: #339933;">,</span><span style="color: blue;">"moreno"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>color.ojos<span style="color: #339933;">,</span>color.pelo<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># color.pelo</span>
<span style="color: #666666; font-style: italic;"># color.ojos moreno rubio</span>
<span style="color: #666666; font-style: italic;"># claro 2 6</span>
<span style="color: #666666; font-style: italic;"># oscuro 5 2</span>
<a href="http://inside-r.org/r-doc/stats/chisq.test"><span style="color: #003399; font-weight: bold;">chisq.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>color.ojos<span style="color: #339933;">,</span>color.pelo<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Pearson's Chi-squared test with Yates' continuity correction</span>
<span style="color: #666666; font-style: italic;"># data: table(color.ojos, color.pelo)</span>
<span style="color: #666666; font-style: italic;"># X-squared = 1.637, df = 1, p-value = 0.2007</span>
<span style="color: #666666; font-style: italic;"># Warning message:</span>
<span style="color: #666666; font-style: italic;"># Chi-squared approximation may be incorrect in: chisq.test(table(color.ojos, color.pelo))</span>
<span style="color: #666666; font-style: italic;">## En este caso, el p-valor es 0.2, que es muy alto: no rechazaremos la hip?tesis de que las variables color de ojos y color de pelo son independientes. El mensaje de aviso que da el test es porque el n?mero de casos utilizado es muy peque?o.</span>
<span style="color: #666666; font-style: italic;">#### cor.test y wilcox.test ###############################################################</span>
<span style="color: #666666; font-style: italic;"># Son dos tests para ver si hay asociaci?n entre muestras de datos. Por ejemplo, una serie de alumnos pueden ser sometidos a dos test de inteligencia distintos. Si hay 100 alumnos, tendr?amos dos vectores de longitud 100: el primero ser?a la puntuaci?n alcanzada por los alumnos en el primer test, y el segundo la puntuaci?n de dichos alumnos para el segundo test. Los contrastes cor.test y wilcox.test servir?an para ver si hay correlaci?n entre ambos tests.</span>
<span style="color: #666666; font-style: italic;"># Como advertimos al principio del tema, se supone que el fundamento del contraste se conoce. De todos modos, digamos que el test de Wilcoxon es no param?trico, y tiene m?s en cuenta el orden correlativo de los datos que el valor de los datos mismos. Para apreciar las diferencias, pongamos unos ejemplos:</span>
<span style="color: #666666; font-style: italic;"># Primero generamos una secuencia de datos, y a partir de la misma, otras dos, relacionadas de alguna manera, y veremos qu? dicen los tests al respecto:</span>
x = <a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span>-<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x
<span style="color: #666666; font-style: italic;"># [1] -5 -4 -3 -2 -1 0 1 2 3 4 5</span>
y = x^<span style="color: #cc66cc;">2</span>
y
<span style="color: #666666; font-style: italic;"># [1] 25 16 9 4 1 0 1 4 9 16 25</span>
<span style="color: #666666; font-style: italic;"># Vemos que entre x e y hay una relaci?n, aunque sea no lineal</span>
z = x^<span style="color: #cc66cc;">3</span>
z
<span style="color: #666666; font-style: italic;"># [1] -125 -64 -27 -8 -1 0 1 8 27 64 125</span>
<span style="color: #666666; font-style: italic;"># Vemos si existe relaci?n entre x e y</span>
<a href="http://inside-r.org/r-doc/stats/cor.test"><span style="color: #003399; font-weight: bold;">cor.test</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Pearson's product-moment correlation</span>
<span style="color: #666666; font-style: italic;"># data: x and y</span>
<span style="color: #666666; font-style: italic;"># t = 0, df = 9, p-value = 1</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true correlation is not equal to 0</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># cor</span>
<span style="color: #666666; font-style: italic;"># 0</span>
<span style="color: #666666; font-style: italic;"># La relaci?n entre x e y no es detectada por el test cor.test: el p-valor ha salido 1.</span>
<a href="http://inside-r.org/r-doc/stats/wilcox.test"><span style="color: #003399; font-weight: bold;">wilcox.test</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Wilcoxon rank sum test with continuity correction</span>
<span style="color: #666666; font-style: italic;"># data: x and y</span>
<span style="color: #666666; font-style: italic;"># W = 17.5, p-value = 0.005106</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true mu is not equal to 0</span>
<span style="color: #666666; font-style: italic;"># Warning message:</span>
<span style="color: #666666; font-style: italic;"># Cannot compute exact p-value with ties in: wilcox.test.default(x, y)</span>
<span style="color: #666666; font-style: italic;">## La relaci?n entre x e y s? ha sido detectada por wilcox.test.</span>
<span style="color: #666666; font-style: italic;"># Vemos si existe relaci?n entre x y z</span>
<a href="http://inside-r.org/r-doc/stats/cor.test"><span style="color: #003399; font-weight: bold;">cor.test</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>z<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Pearson's product-moment correlation</span>
<span style="color: #666666; font-style: italic;"># data: x and z</span>
<span style="color: #666666; font-style: italic;"># t = 7.1257, df = 9, p-value = 5.51e-05</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true correlation is not equal to 0</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># cor</span>
<span style="color: #666666; font-style: italic;"># 0.921649</span>
<span style="color: #666666; font-style: italic;">## En este caso, cor.test s? detecta la relaci?n, al mantenerse el orden de los datos.</span>
<span style="color: #666666; font-style: italic;">#### fisher.test ###############################################################</span>
<span style="color: #666666; font-style: italic;"># Este test tiene el mismo fin que el test chisq.test, pero otro fundamento, y es ?til para muestras peque?as, caso para el cual chisq.test no es adecuado.</span>
<span style="color: #666666; font-style: italic;"># El ejemplo que trae la librer?a es muy adecuado. Cierta dama fina (inglesa, por supuesto), era aficionada a tomar te con leche (?c?mo no!), y presum?a de ser capaz de distinguir cuando su criada le echaba primero la leche y luego el t?, o bien echaba primero el t? y despu?s la leche. Tal habilidad era mirada con incredulidad por sus allegados. Para salir de dudas, Fisher le dio a probar a la dama una serie de tazas de t?, en las que unas veces se hab?a echado primero la leche y otras primero el t?. Fisher apunt? los aciertos de la dama y con la tabla de contingencia as? construida, veamos lo que pas?....</span>
TeaTasting = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> nr = <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a> = <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>Guess = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Milk"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Tea"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> Truth = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Milk"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Tea"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/fisher.test"><span style="color: #003399; font-weight: bold;">fisher.test</span></a><span style="color: #009900;">(</span>TeaTasting<span style="color: #339933;">,</span> alternative = <span style="color: blue;">"greater"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Fisher's Exact Test for Count Data</span>
<span style="color: #666666; font-style: italic;"># data: TeaTasting</span>
<span style="color: #666666; font-style: italic;"># p-value = 0.2429</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true odds ratio is greater than 1</span>
<span style="color: #666666; font-style: italic;"># 95 percent confidence interval:</span>
<span style="color: #666666; font-style: italic;"># 0.3135693 Inf</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># odds ratio</span>
<span style="color: #666666; font-style: italic;"># 6.408309</span>
<span style="color: #666666; font-style: italic;">## Dado el p-valor obtenido, el test no mostr? evidencia suficiente a favor de la habilidad de la dama. Sin embargo, vemos que de 4 veces que se le dio primero la leche, acert? 3, y lo mismo para el t?.</span>
<span style="color: #666666; font-style: italic;"># Otro ejemplo, con los datos de color de pelo y ojos que hemos utilizado m?s arriba:</span>
<a href="http://inside-r.org/r-doc/stats/fisher.test"><span style="color: #003399; font-weight: bold;">fisher.test</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>color.ojos<span style="color: #339933;">,</span>color.pelo<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Fisher's Exact Test for Count Data</span>
<span style="color: #666666; font-style: italic;"># data: table(color.ojos, color.pelo)</span>
<span style="color: #666666; font-style: italic;"># p-value = 0.1319</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true odds ratio is not equal to 1</span>
<span style="color: #666666; font-style: italic;"># 95 percent confidence interval:</span>
<span style="color: #666666; font-style: italic;"># 0.007811653 1.922966759</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># odds ratio</span>
<span style="color: #666666; font-style: italic;"># 0.1563219</span>
<span style="color: #666666; font-style: italic;">## En este caso, tampoco somos concluyentes, como no lo ?ramos al utilizar la funci?n chisq.test. A pesar de todo, para el tama?o de la muestra, que es peque?o, este test es m?s fiable.</span>
<span style="color: #666666; font-style: italic;">#### ks.test ##################################################################</span>
<span style="color: #666666; font-style: italic;"># Utilizaremos este contraste para comprobar si dos conjuntos de datos siguen la misma distribuci?n, o bien para ver si un determinado conjunto de datos se ajusta a una distribuci?n determinada. Las muestras no necesitan ser del mismo tama?o.</span>
<span style="color: #666666; font-style: italic;"># Por ejemplo, vamos a generar 50 datos de una distribuci?n normal, y 30 de una uniforme, a ver si el test es capaz de advertirnos que las distribuciones de origen no son las mismas:</span>
x = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>
y = <a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/ks.test"><span style="color: #003399; font-weight: bold;">ks.test</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Two-sample Kolmogorov-Smirnov test</span>
<span style="color: #666666; font-style: italic;"># data: x and y</span>
<span style="color: #666666; font-style: italic;"># D = 0.52, p-value = 2.903e-05</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: two.sided</span>
<span style="color: #666666; font-style: italic;"># Como el p-valor es pr?cticamente cero, rechazaremos la hip?tesis de que los datos vienen de la misma distribuci?n.</span>
<span style="color: #666666; font-style: italic;">#### shapiro.test ##############################################################</span>
<span style="color: #666666; font-style: italic;"># Es como ks.test, pero est? especializado en la distribuci?n normal.</span>
<span style="color: #666666; font-style: italic;"># Por ejemplo, con los mismos datos x generados en el ejemplo anterior, comprobemos si el test detecta que provienen de una distribuci?n normal:</span>
<a href="http://inside-r.org/r-doc/stats/shapiro.test"><span style="color: #003399; font-weight: bold;">shapiro.test</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Shapiro-Wilk normality test</span>
<span style="color: #666666; font-style: italic;"># data: x</span>
<span style="color: #666666; font-style: italic;"># W = 0.9758, p-value = 0.3905</span>
<span style="color: #666666; font-style: italic;">## En efecto, el p-valor es grande (0.39), y no rechazaremos la hip?tesis nula de que los datos x provienen de una normal.</span>
<span style="color: #666666; font-style: italic;">#### oneway.test ###############################################################</span>
<span style="color: #666666; font-style: italic;">#Este es el contraste de igualdad de medias que se estudia en los modelos ANOVA. Se supone que hay varios grupos de datos, que cada grupo sigue una distribuci?n normal (no necesariamente con la misma varianza), y se trata de ver si las medias son todas iguales o no. Este test aparece mucho en procesos de calidad, de dise?o de experimentos, en los que se fabrica un producto de varias maneras distintas, y se trata de ver si los procedimientos conducen al mismo resultado o no.</span>
<span style="color: #666666; font-style: italic;"># Para poner un ejemplo, consideraremos los datos de fabricaci?n de camisas de trabajo sacados del libro de Dise?o y Analisis de Experimentos de Montgomery, del grupo Editorial Iberoamericana. Se trata de fabricar camisas de trabajo que tengan la mayor resistencia posible a la rotura. Para ellos se fabrican 5 tipos de camisas, donde en cada grupo se da una proporci?n determinada de algod?n en la composici?n de la camisa. Las camisas del grupo 1 tienen un 15% de algod?n, las del grupo 2 un 20%, las del grupo 3 un 25%, las del grupo 4 un 30%, y las del grupo 5 un 35% de algod?n. Se fabrican 5 camisas de cada tipo, se rompen y se mide su resistencia. Se trata de determinar si la resistencia media de las camisas de cada grupo es la misma o no. Vamos all?:</span>
algodon = <a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># introducimos los datos por teclado</span>
<span style="color: #cc66cc;">1</span>: <span style="color: #cc66cc;">7</span>
<span style="color: #cc66cc;">2</span>: <span style="color: #cc66cc;">7</span>
<span style="color: #cc66cc;">3</span>: <span style="color: #cc66cc;">15</span>
<span style="color: #cc66cc;">4</span>: <span style="color: #cc66cc;">11</span>
<span style="color: #cc66cc;">5</span>: <span style="color: #cc66cc;">9</span>
<span style="color: #cc66cc;">6</span>: <span style="color: #cc66cc;">12</span>
<span style="color: #cc66cc;">7</span>: <span style="color: #cc66cc;">17</span>
<span style="color: #cc66cc;">8</span>: <span style="color: #cc66cc;">12</span>
<span style="color: #cc66cc;">9</span>: <span style="color: #cc66cc;">18</span>
<span style="color: #cc66cc;">10</span>: <span style="color: #cc66cc;">18</span>
<span style="color: #cc66cc;">11</span>: <span style="color: #cc66cc;">14</span>
<span style="color: #cc66cc;">12</span>: <span style="color: #cc66cc;">18</span>
<span style="color: #cc66cc;">13</span>: <span style="color: #cc66cc;">18</span>
<span style="color: #cc66cc;">14</span>: <span style="color: #cc66cc;">19</span>
<span style="color: #cc66cc;">15</span>: <span style="color: #cc66cc;">19</span>
<span style="color: #cc66cc;">16</span>: <span style="color: #cc66cc;">19</span>
<span style="color: #cc66cc;">17</span>: <span style="color: #cc66cc;">25</span>
<span style="color: #cc66cc;">18</span>: <span style="color: #cc66cc;">22</span>
<span style="color: #cc66cc;">19</span>: <span style="color: #cc66cc;">19</span>
<span style="color: #cc66cc;">20</span>: <span style="color: #cc66cc;">23</span>
<span style="color: #cc66cc;">21</span>: <span style="color: #cc66cc;">7</span>
<span style="color: #cc66cc;">22</span>: <span style="color: #cc66cc;">10</span>
<span style="color: #cc66cc;">23</span>: <span style="color: #cc66cc;">11</span>
<span style="color: #cc66cc;">24</span>: <span style="color: #cc66cc;">15</span>
<span style="color: #cc66cc;">25</span>: <span style="color: #cc66cc;">11</span>
<span style="color: #cc66cc;">26</span>:
<span style="color: #666666; font-style: italic;">#Read 25 items</span>
porcentaje.algodon = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">35</span><span style="color: #009900;">)</span>
datos.algodon = <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span>algodon<span style="color: #339933;">,</span>porcentaje.algodon<span style="color: #009900;">)</span>
datos.algodon
<span style="color: #666666; font-style: italic;"># algodon porcentaje.algodon</span>
<span style="color: #666666; font-style: italic;"># [1,] 7 15</span>
<span style="color: #666666; font-style: italic;"># [2,] 7 15</span>
<span style="color: #666666; font-style: italic;"># [3,] 15 15</span>
<span style="color: #666666; font-style: italic;"># [4,] 11 15</span>
<span style="color: #666666; font-style: italic;"># [5,] 9 15</span>
<span style="color: #666666; font-style: italic;"># [6,] 12 20</span>
<span style="color: #666666; font-style: italic;"># [7,] 17 20</span>
<span style="color: #666666; font-style: italic;"># [8,] 12 20</span>
<span style="color: #666666; font-style: italic;"># [9,] 18 20</span>
<span style="color: #666666; font-style: italic;">#[10,] 18 20</span>
<span style="color: #666666; font-style: italic;">#[11,] 14 25</span>
<span style="color: #666666; font-style: italic;">#[12,] 18 25</span>
<span style="color: #666666; font-style: italic;">#[13,] 18 25</span>
<span style="color: #666666; font-style: italic;">#[14,] 19 25</span>
<span style="color: #666666; font-style: italic;">#[15,] 19 25</span>
<span style="color: #666666; font-style: italic;">#[16,] 19 30</span>
<span style="color: #666666; font-style: italic;">#[17,] 25 30</span>
<span style="color: #666666; font-style: italic;">#[18,] 22 30</span>
<span style="color: #666666; font-style: italic;">#[19,] 19 30</span>
<span style="color: #666666; font-style: italic;">#[20,] 23 30</span>
<span style="color: #666666; font-style: italic;">#[21,] 7 35</span>
<span style="color: #666666; font-style: italic;">#[22,] 10 35</span>
<span style="color: #666666; font-style: italic;">#[23,] 11 35</span>
<span style="color: #666666; font-style: italic;">#[24,] 15 35</span>
<span style="color: #666666; font-style: italic;">#[25,] 11 35</span>
<span style="color: #666666; font-style: italic;">#Hasta aqu? tenemos los datos de resistencia de las 25 camisas, junto con la proporci?n de algod?n que hay en cada una.</span>
<span style="color: #666666; font-style: italic;">#Ahora aplicaremos el test de igualdad de medias. En la funci?n se especifica como par?metro algodon~porcentaje.algodon lo que significa que estudiaremos la variable algodon (su resistencia), dividiendola en grupos seg?n el porcentaje de algod?n presente.</span>
<a href="http://inside-r.org/r-doc/stats/oneway.test"><span style="color: #003399; font-weight: bold;">oneway.test</span></a><span style="color: #009900;">(</span>algodon~porcentaje.algodon<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=datos.algodon<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># One-way analysis of means (not assuming equal variances)</span>
<span style="color: #666666; font-style: italic;"># data: algodon and porcentaje.algodon</span>
<span style="color: #666666; font-style: italic;">#F = 12.4507, num df = 4.000, denom df = 9.916, p-value = 0.0006987</span>
<span style="color: #666666; font-style: italic;">## Como vemos, el p-valor es pr?cticamente cero. Se rechaza, pues, la hip?tesis de que todos los grupos son similares. Esto es, variar el porcentaje de algod?n tiene consecuencias en la resistencia de la camisa. De hecho, podemos hacer un diagrama de caja (un boxplot) por grupos, para ver c?mo evoluciona la resistencia en funci?n del porcentaje de algod?n:</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span>algodon~porcentaje.algodon<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#### var.test ##################################################################</span>
<span style="color: #666666; font-style: italic;"># Este es un contraste para comprobar la igualdad de varianzas entre dos grupos de datos.</span>
<span style="color: #666666; font-style: italic;"># Por ejemplo, cuando tenemos dos m?quinas que fabrian lo mismo (por ejemplo, rodamientos), y queremos saber si lo hacen con la misma variabilidad. Para poner un ejemplo, generemos datos de dos distribuciones normales con distinta varianza y veamos si el test detecta esta situaci?n:</span>
x = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> = <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a> = <span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>
y = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> = <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a> = <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/var.test"><span style="color: #003399; font-weight: bold;">var.test</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># F test to compare two variances</span>
<span style="color: #666666; font-style: italic;"># data: x and y</span>
<span style="color: #666666; font-style: italic;"># F = 5.3488, num df = 49, denom df = 29, p-value = 7.546e-06</span>
<span style="color: #666666; font-style: italic;"># alternative hypothesis: true ratio of variances is not equal to 1</span>
<span style="color: #666666; font-style: italic;"># 95 percent confidence interval:</span>
<span style="color: #666666; font-style: italic;"># 2.687373 10.063368</span>
<span style="color: #666666; font-style: italic;"># sample estimates:</span>
<span style="color: #666666; font-style: italic;"># ratio of variances</span>
<span style="color: #666666; font-style: italic;"># 5.348825</span>
<span style="color: #666666; font-style: italic;">## Como vemos, el p-valor sale pr?cticamente cero: rechazaremos la hip?tesis de que los conjuntos de datos x e y provienen de distribuciones con la misma varianza.</span>
</pre>
</div>
</div>
<br />Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-90627474003333184232012-08-21T05:50:00.000-07:002012-08-21T05:50:08.562-07:00Clase 3: Distribuciones de probabilidad en R-project<div style="overflow: auto;">
<h2 style="text-align: center;">
Distribuciones de probabilidad en R</h2>
<div class="geshifilter">
<br /></div>
<div class="geshifilter">
En las entradas anteriores (<a href="http://statisticalecology-ec.blogspot.com.es/2012/08/clase-1-manejo-de-datos-en-r-project.html">Clase 1</a>, <a href="http://statisticalecology-ec.blogspot.com.es/2012/08/clase-2-analisis-unidimensional-en-r.html">Clase 2</a>) comentamos que el análisis estadístico consta de tres partes: el análisis descriptivo, el probabilistico y el inferencial. Hemos abordado el primero en la Clase 2 y ahora nos centraremos en el análisis probabilístico.</div>
<div style="overflow: auto;">
<br /></div>
<h4>
Probabilidad y variables aleatorias.</h4>
<br />
La probabilidad refleja las expectativas de que un resultado (o suceso) determinado ocurra.<br />
Para estudiar el comoprtamiento de estas probabilidades, se trabaja con variables aleatorias que representan los posibles resultados de un experimento aún no realizado. Una variable aleatoria (v.a.) es una función que asocia a cada resultado de de un experimento (espacio muestral) un número real.<br />
<br />
<h4>
Tipos de variables aleatorias.</h4>
<div>
Podemos clasificar las v.a. en discretas y continuas según tomen valores discretos (números enteros) o continuos (números reales), respectivamente.</div>
<h4>
Función de probabilidad y función de distribución.</h4>
Las v.a. quedan caracterizadas por su función masa de probabilidad (caso discreto) o función de densidad de probabilidad (caso continuo), además de por su función de distribución, esperanza y varianza.<br />
La función de densidad nos dice cómo se distribuyen las probabilidades de un suceso o evento, por tanto, la distribución de probabilidad de una v.a. es una función que asigna a cada valor posible de dicha v.a. una probabilidad. La función de distribución se puede entender como una función de probabilidad acumulada, es decir, asocia a cada valor de la variable aleatoria la probabilidad acumulada hasta ese valor.<br /><br />
<br />
<br />
Con estos conceptos en mente, veamos ahora cómo aplicarlos en R.<br />
Saludos!<br />
<br />
<div class="geshifilter">
<br />
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># An?lisis de datos con R #</span>
<span style="color: #666666; font-style: italic;"># 2012 #</span>
<span style="color: #666666; font-style: italic;"># Msc. Rosana Ferrero #</span>
<span style="color: #666666; font-style: italic;"># http://statisticalecology.blogspot.com/ #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 3. Distribuciones de probabilidad #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">### Funciones para distribuciones de probabilidad</span>
<span style="color: #666666; font-style: italic;"># Densidad de probabilidad (d): probabilidades puntuales P(X=x)</span>
<span style="color: #666666; font-style: italic;"># Probabilidades (p): probabilidades acumuladas P(X<=x) y P(X>x) si lower.tail=F</span>
<span style="color: #666666; font-style: italic;"># Quantiles (q): cuantiles (cola inferior) o cola superior si lower.tail=F. Recordar que los cuantiles pueden ser: cuartiles, deciles, percentiles</span>
<span style="color: #666666; font-style: italic;"># N?meros aleatorios (r): Generaci?n de muestras aleatorias</span>
<span style="color: #666666; font-style: italic;">### Distribuciones disponibles:</span>
<span style="color: #666666; font-style: italic;"># binom Binomial</span>
<span style="color: #666666; font-style: italic;"># cauchy Cauchy</span>
<span style="color: #666666; font-style: italic;"># chisq Chi Cuadrado</span>
<span style="color: #666666; font-style: italic;"># beta Beta</span>
<span style="color: #666666; font-style: italic;"># exp Exponencial</span>
<span style="color: #666666; font-style: italic;"># gamma Gamma</span>
<span style="color: #666666; font-style: italic;"># geom Geom?trica</span>
<span style="color: #666666; font-style: italic;"># hyper Hipergeom?trica</span>
<span style="color: #666666; font-style: italic;"># lnorm Log-normal</span>
<span style="color: #666666; font-style: italic;"># logis Log?stica</span>
<span style="color: #666666; font-style: italic;"># nbinom Binomial negativa</span>
<span style="color: #666666; font-style: italic;"># nchisq Chi cuadrado no central</span>
<span style="color: #666666; font-style: italic;"># norm Normal</span>
<span style="color: #666666; font-style: italic;"># pois Poisson</span>
<span style="color: #666666; font-style: italic;"># signrank Distribucion del test de Wilcoxon de rangos con signo</span>
<span style="color: #666666; font-style: italic;"># t Student</span>
<span style="color: #666666; font-style: italic;"># unif Uniforme</span>
<span style="color: #666666; font-style: italic;"># weibull Weibull</span>
<span style="color: #666666; font-style: italic;"># wilcox Distribuci?n de la suma de rangos de Wilcoxon</span>
<span style="color: #666666; font-style: italic;">### Ejemplo con la distribuci?n normal:</span>
<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Generaci?n de un dato de la normal estandar</span>
<span style="color: #666666; font-style: italic;">#[1] -0.4120618</span>
<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Generaci?n de 5 datos de la normal estandar</span>
<span style="color: #666666; font-style: italic;">#[1] -0.3220499 -0.5556478 -0.1899898 -0.3450181 -2.5807986</span>
<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a>=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a>=<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Generaci?n de un dato de una normal no estandar</span>
<span style="color: #666666; font-style: italic;">#[1] -0.4035896 -0.8089832 3.2513373 4.9641722 -1.8603231</span>
<a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Evaluaci?n de la funci?n de densidad normal en el punto 0</span>
<span style="color: #666666; font-style: italic;">#[1] 0.3989423</span>
<a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 0.2419707</span>
<span style="color: #666666; font-style: italic;"># dnorm(3)</span>
<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.004431848</span>
<a href="http://inside-r.org/r-doc/stats/pnorm"><span style="color: #003399; font-weight: bold;">pnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Probabilidad acumulada bajo la normal en el punto 0</span>
<span style="color: #666666; font-style: italic;">#[1] 0.5</span>
<a href="http://inside-r.org/r-doc/stats/pnorm"><span style="color: #003399; font-weight: bold;">pnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># [1] 0.9986501</span>
<a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.5</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># El cuantil 50% de la normal es el 0</span>
<span style="color: #666666; font-style: italic;">#[1] 0</span>
<a href="http://inside-r.org/r-doc/stats/qnorm"><span style="color: #003399; font-weight: bold;">qnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.9986501</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># El cuantil 99.86501% de la normal es el 3</span>
<span style="color: #666666; font-style: italic;">#[1] 3.000000</span>
x=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span>-<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a>=<span style="color: #cc66cc;">200</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Dibujo del gr?fico de la distribuci?n normal</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/dnorm"><span style="color: #003399; font-weight: bold;">dnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>type=<span style="color: blue;">"l"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Generaci?n de 100 datos de una normal estandar, y un sumario de los mismos:</span>
x = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Min. 1st Qu. Median Mean 3rd Qu. Max.</span>
<span style="color: #666666; font-style: italic;">#-2.18000 -0.71000 -0.01041 0.05584 0.73730 2.76500</span>
<span style="color: #666666; font-style: italic;">#Generaci?n de dos muestras correladas, y c?lculo de la correlaci?n:</span>
x = <a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
y = <span style="color: #cc66cc;">2</span>*x+<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 0.9817436</span>
<span style="color: #666666; font-style: italic;">#Creaci?n de permutaciones:</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 5 6 1 4 9 2 8 10 7 3</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 9 1 3 10 7 2 5 4 8 6</span>
<span style="color: #666666; font-style: italic;">#Muestreo sin repetici?n:</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 7 8 2 4 6</span>
<span style="color: #666666; font-style: italic;">#Muestreo con repetici?n: ej. Simulaci?n de 5 tiradas de un dado equilibrado.</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a>=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 6 10 6 1 6</span>
<span style="color: #666666; font-style: italic;">#C?lculo de algunos percentiles de un conjunto de datos:</span>
x = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>probs=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0.1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0.9</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># 10% 40% 90%</span>
<span style="color: #666666; font-style: italic;">#-1.3194786 -0.3663511 1.0824184</span></pre>
</div>
</div>
<br />Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-49402576333562611822012-08-21T03:14:00.002-07:002012-08-21T16:16:20.430-07:00Clase 2: Análisis unidimensional en R-project<div style="overflow: auto;">
<div class="geshifilter">
<h2 style="text-align: center;">
<b>Análisis unidimensional de los datos</b></h2>
Luego de familiarizarnos con el manejo básico de datos en R (<a href="http://statisticalecology-ec.blogspot.com.es/2012/08/clase-1-manejo-de-datos-en-r-project.html">post anterior</a>), podemos comenzar a realizar el análisis unidimensional de los datos (de 1 dimensión o 1 variable). </div>
<div class="geshifilter">
El estudio de una variable se divide en su análisis descriptivo, el inferencial y el probabilístico. En esta entrada nos basaremos en el primero de ellos.</div>
<div class="geshifilter">
El análisis descriptivo consta de tablas de frecuencias (absolutas, relativas, acumuladas), gráficos y cálculo de estadísticos o medidas que nos permitan resumir la variable en cuestión.<br />
<b><br /></b>
<br />
<h4>
<b>Tipo de variable.</b></h4>
El tipo de variable a estudiar nos determinará el tipo de análisis a seguir. Una variable puede ser cuantitativa (o numérica) o cualitativa (o categórica). Además, una variable cuantitativa puede ser continua (si sus valores toma números reales; e.g. el peso) o discreta (números enteros; e.g. el número de hijos), mientras que una variable cualitativa puede clasificarse como nominal (categorías sin orden; e.g. el género) u ordinal (categorías con orden; e.g. estado de una enfermedad: leve, moderado, grave).<br />
<br />
<h4>
Tablas de frecuencia, gráficos, estadísticos e inferencia.</h4>
La clasificación de la variable de estudio determinará si la utilización de una tabla de frecuencias será con datos agrupados o no agrupados, el tipo de gráfico a seleccionar y qué inferencia podemos extraer de ella. Por ejemplo, las variables discretas, y cualitativas se representan mediante una tabla de frecuencias con datos no agrupados, mientras que para los datos continuos elaboramos intervalos y realizamos la tabla de frecuencia con datos agrupados en dichos intervalos. Ademas, si se trata de una variable continua elegiremos el histograma, mientras que para una variable discreta utilizaremos el diagrama de barras. Por su parte, si la variable es continua tendrá sentido calcular su media, mientras que si la variable es cualitativa este estadístico no lo podemos calcular (e.g. no se puede calcular la media del género).<br />
<br />
<div style="text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-vITMe42W-zuzEfxW6B39IDUhNNIRSUirjVOX7aGYG9ay_Oka-BIElk5LSaGTb9kyo-WgxuAPzkX3EazG9xLWEujsUNxdm_NGkicpx8oziAqTYBs8301bRtOLp6VPxMFm7shIF1R73kw/s1600/0.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="220" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-vITMe42W-zuzEfxW6B39IDUhNNIRSUirjVOX7aGYG9ay_Oka-BIElk5LSaGTb9kyo-WgxuAPzkX3EazG9xLWEujsUNxdm_NGkicpx8oziAqTYBs8301bRtOLp6VPxMFm7shIF1R73kw/s400/0.png" style="cursor: move;" width="400" /></a><br />
<br />
<div style="text-align: left;">
Si deseas conocer más sobre el diagrama de cajas o boxplot visita:<a href="http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=5049297490932214389"> http://www.blogger.com/blogger.g?blogID=6841022238556785612#editor/target=post;postID=5049297490932214389</a></div>
</div>
<h4>
Estadísticos descriptivos.</h4>
Los estadísticos se dividen principalmente en aquellos de centralidad o posición central (media, moda, mediana, etc.), en los de posición no cental (cuantiles: cuartiles, deciles y percentiles), de dispersión o variabilidad (varianza, desviación típica, error típico, coeficiente de variación, rango intercuartílico, etc.), y de forma (asimetría y curtosis).<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<br /></div>
<div class="geshifilter">
Aquí les dejo una breve introducción a las principales funciones que hay en R para desarrollar cada uno de estos análisis. </div>
<div class="geshifilter">
Por último les dejo algunas funciones para la manipulación de bases de datos.</div>
<div class="geshifilter">
<br />
Saludos!</div>
<div class="geshifilter">
<br />
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># An?lisis de datos con R #</span>
<span style="color: #666666; font-style: italic;"># 2012 #</span>
<span style="color: #666666; font-style: italic;"># Msc. Rosana Ferrero #</span>
<span style="color: #666666; font-style: italic;"># http://statisticalecology.blogspot.com/ #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span class="Apple-style-span" style="color: #666666;"><i>
</i></span></pre>
</div>
</div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Clase 2. Estad?stica descriptiva #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 0. Funciones de interés para manejar los datos #</span>
<span style="color: #666666; font-style: italic;"># 1. Estad?stica unidimensional #</span>
<span style="color: #666666; font-style: italic;"># a. Tablas de Frecuencia #</span>
<span style="color: #666666; font-style: italic;"># b. Gráficos #</span>
<span style="color: #666666; font-style: italic;"># c. Estadísticos #</span>
<span style="color: #666666; font-style: italic;"># 2. Ejemplo: datos Iris #</span>
<span style="color: #666666; font-style: italic;"># 3. Otras funciones #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 0. Funciones de inter?s para manejar los datos #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">########## Leer los datos</span>
<span style="color: #666666; font-style: italic;">### La funci?n "scan"</span>
x=<a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #cc66cc;">1</span>: <span style="color: #cc66cc;">6</span>
<span style="color: #cc66cc;">2</span>: <span style="color: #cc66cc;">7</span>
<span style="color: #cc66cc;">3</span>: <span style="color: #cc66cc;">3</span>
<span style="color: #cc66cc;">4</span>: <span style="color: #cc66cc;">4</span>
<span style="color: #cc66cc;">5</span>: <span style="color: #cc66cc;">8</span>
<span style="color: #cc66cc;">6</span>: <span style="color: #cc66cc;">5</span>
<span style="color: #cc66cc;">7</span>: <span style="color: #cc66cc;">6</span>
<span style="color: #cc66cc;">8</span>: <span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">9</span>:
<span style="color: #666666; font-style: italic;">### La funci?n "read.table". Producen un dataframe</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>regression.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
map=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>bowens.csv"</span><span style="color: #339933;">,</span>header=T<span style="color: #339933;">,</span>sep=<span style="color: blue;">","</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/file.choose"><span style="color: #003399; font-weight: bold;">file.choose</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## opci?n seq=""</span>
<span style="color: #666666; font-style: italic;">#\n newline</span>
<span style="color: #666666; font-style: italic;">#\r carriage return</span>
<span style="color: #666666; font-style: italic;">#\t tab character</span>
<span style="color: #666666; font-style: italic;">#\b backspace</span>
<span style="color: #666666; font-style: italic;">#\a bell</span>
<span style="color: #666666; font-style: italic;">#\f form feed</span>
<span style="color: #666666; font-style: italic;">#\v vertical tab</span>
<span style="color: #666666; font-style: italic;">## chequear un archivo</span>
<a href="http://inside-r.org/r-doc/base/file.exists"><span style="color: #003399; font-weight: bold;">file.exists</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>Decay.txt"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## leer datos desde archivos con formato no est?ndar. Producen un objeto lista no un dataframe</span>
murders=<a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>murders.txt"</span><span style="color: #339933;">,</span> skip=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> what=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><span style="color: blue;">""</span><span style="color: #339933;">,</span><span style="color: blue;">""</span><span style="color: #339933;">,</span><span style="color: blue;">""</span><span style="color: #339933;">,</span><span style="color: blue;">""</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
murder.frame=<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span>murders<span style="color: #009900;">)</span>
murders=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>murders.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## leer archivos con diferentes n?meros/valores por l?nea</span>
line.number=<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>rt.txt"</span><span style="color: #339933;">,</span>sep=<span style="color: blue;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">(</span>my.list=<a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span>:<span style="color: #009900;">(</span>line.number-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>rt.txt"</span><span style="color: #339933;">,</span>skip=x<span style="color: #339933;">,</span>nlines=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>quiet=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## leer l?neas. Produce un objeto de clase character.</span>
<a href="http://inside-r.org/r-doc/base/readLines"><span style="color: #003399; font-weight: bold;">readLines</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>murders.txt"</span><span style="color: #339933;">,</span>n=-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">######## Ver ?tem 4 para obtener m?s funciones de inter?s </span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 1.a. Tablas de frecuencias #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># realiza tablas de frecuencias absolutas</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>/sum<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#realiza tablas de frecuencias relativas</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#para variables num?ricas si queremos hacer una tabla de frecuencias absolutas agrupando los datos por intervalos</span>
<a href="http://inside-r.org/r-doc/base/margin.table"><span style="color: #003399; font-weight: bold;">margin.table</span></a><span style="color: #009900;">(</span>tabla<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> o <span style="color: #cc66cc;">2</span> o ...<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#realiza tablas marginales del objeto tabla</span>
<a href="http://inside-r.org/r-doc/base/prop.table"><span style="color: #003399; font-weight: bold;">prop.table</span></a><span style="color: #009900;">(</span>tabla<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> o <span style="color: #cc66cc;">2</span> o ...<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#tabla de frecuencias condicionadas por la dimensi?n indicada con 1 o 2 o... del objeto tabla</span>
<a href="http://inside-r.org/r-doc/stats/ftable"><span style="color: #003399; font-weight: bold;">ftable</span></a><span style="color: #009900;">(</span>tabla<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># tabla de frecuencias para 3 o m?s variables categ?ricas</span>
m = <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> m
<a href="http://inside-r.org/r-doc/base/margin.table"><span style="color: #003399; font-weight: bold;">margin.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># totales por filas</span>
<a href="http://inside-r.org/r-doc/base/margin.table"><span style="color: #003399; font-weight: bold;">margin.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># totales por columnas</span>
<a href="http://inside-r.org/r-doc/base/prop.table"><span style="color: #003399; font-weight: bold;">prop.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># porcentaje por filas</span>
<a href="http://inside-r.org/r-doc/base/prop.table"><span style="color: #003399; font-weight: bold;">prop.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># porcentaje por columnas</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a>
<a href="http://inside-r.org/r-doc/stats/ftable"><span style="color: #003399; font-weight: bold;">ftable</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># crea tablas de contingencia llanas; x= factores o lista o tabla</span>
<a href="http://inside-r.org/r-doc/stats/ftable"><span style="color: #003399; font-weight: bold;">ftable</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a><span style="color: #339933;">,</span> row.vars = <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> col.vars = <span style="color: blue;">"Survived"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/tabulate"><span style="color: #003399; font-weight: bold;">tabulate</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> nbins = <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># cuenta el n?mero de veces que ocurre cada valor en un vector dado </span>
<span style="color: #666666; font-style: italic;"># Tabla de frecuencias de 3 v?as (3 variables)</span>
<a href="http://inside-r.org/r-doc/stats/xtabs"><span style="color: #003399; font-weight: bold;">xtabs</span></a><span style="color: #009900;">(</span>~A+B+<a href="http://inside-r.org/r-doc/stats/C"><span style="color: #003399; font-weight: bold;">C</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># tabla de frecuencias de 3 v?as</span>
<span style="color: #666666; font-style: italic;"># Tabla de contingencia (Cross Tabulation) de 2 v?as (2 variables)</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/gmodels">gmodels</a><span style="color: #009900;">)</span>
CrossTable<span style="color: #009900;">(</span>mydata$myrowvar<span style="color: #339933;">,</span> mydata$mycolvar<span style="color: #009900;">)</span>
requires<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/with"><span style="color: #003399; font-weight: bold;">with</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/warpbreaks"><span style="color: #003399; font-weight: bold;">warpbreaks</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/boot/wool"><span style="color: #003399; font-weight: bold;">wool</span></a><span style="color: #339933;">,</span> tension<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#distribuci?n de frecuencias simples</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/state.division"><span style="color: #003399; font-weight: bold;">state.division</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/datasets/state.region"><span style="color: #003399; font-weight: bold;">state.region</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/with"><span style="color: #003399; font-weight: bold;">with</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/airquality"><span style="color: #003399; font-weight: bold;">airquality</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>Temp<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>Temp<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> Month<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#tabla de contingencia de 2 v?as</span>
<span style="color: #666666; font-style: italic;">#La tabla ignora los valores perdidos, para incluir los NA como una categor?a para contar debemos incluir la opci?n exclude=NULL si la variable es un vector. Si la variable es un factor debemos crear un nuevo factor usando: newfactor = factor(oldfactor, exclude=NULL). (Obtenido de "QuickR" -http://www.statmethods.net/stats/frequencies.html-)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/grDevices/nclass"><span style="color: #003399; font-weight: bold;">nclass</span></a>=<span style="color: #339933;">,</span> breaks=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#por ejemplo: </span>
<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>velocidades<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a>=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">'14'</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/grDevices/nclass"><span style="color: #003399; font-weight: bold;">nclass</span></a>=<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>ylim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>main= <span style="color: blue;">'Velocidad autom?oviles'</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/sub"><span style="color: #003399; font-weight: bold;">sub</span></a>=<span style="color: blue;">'Via el Volador'</span><span style="color: #339933;">,</span> ylab=<span style="color: blue;">'Frecuencias'</span><span style="color: #339933;">,</span>xlab=<span style="color: blue;">'Velocidad en Km/h'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#$breaks (l?mites de clase)</span>
<span style="color: #666666; font-style: italic;">#$counts (frecuencias absolutas)</span>
<span style="color: #666666; font-style: italic;">#$intensities (frecuencias relativas)</span>
<span style="color: #666666; font-style: italic;">#$mids (marcas de clase o puntos medios)</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 1.b. Gr?ficos #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># activa un dispositivo gr?fico </span>
<a href="http://inside-r.org/r-doc/grDevices/dev.off"><span style="color: #003399; font-weight: bold;">dev.off</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#cierra un dispositivo gr?fico</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># selecciona ciertos par?metros gr?ficos que est?n definidos por defecto y pueden modificarse</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics"><span style="color: #006600; font-weight: bold;">graphics</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># demostraci?n de gr?ficos y c?digo de R</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gr?fico de dispersi?n: x e y son vectores</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#si x es un vector num?rico realiza un gr?fico de sus elementos sobre el ?ndice, y si x es una serie temporal realiza un gr?fico de x frente al tiempo</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># si f es factor realiza un gr?fico de barras</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>f<span style="color: #339933;">,</span> x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># si f es factor y x vector, realiza un diagrama de cajas para cada nivel del factor f</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>hoja.datos<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># realiza un gr?fico de dispersi?n para cada pareja de variables</span>
<span style="color: #666666; font-style: italic;">## argumentos de las funciones gr?ficas</span>
<span style="color: #666666; font-style: italic;">#type= "n" (nada) "p" (puntos) "l" (l?neas) "b" (both: punto+l?nea) "o" "s" "S" "h"</span>
<span style="color: #666666; font-style: italic;">#xlab= ylab= (a?ade etiquetas a los ejes)</span>
<span style="color: #666666; font-style: italic;">#main= sub= (a?ade t?tulo y subt?tulo)</span>
<span style="color: #666666; font-style: italic;">#xlim=c(,) ylim=c(,) (selecciona los l?mites m?nimo y m?ximo para los ejes)</span>
<span style="color: #666666; font-style: italic;">#add= T o F (solapa un gr?fico con otro ya existente o no)</span>
<span style="color: #666666; font-style: italic;">#col= (indicar color). ver la opci?n colors()</span>
<span style="color: #666666; font-style: italic;">## otras funciones gr?ficas</span>
<a href="http://inside-r.org/r-doc/graphics/points"><span style="color: #003399; font-weight: bold;">points</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span> pch=n?<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#a?ade puntos definidos por las coordenadas contenidas en los vectores x e y el aspecto indicado en pch. Se puede utilizar type</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>h=<span style="color: #339933;">,</span> v=<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># a?ade l?neas horizontales y/o verticales</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">)</span> o <a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #339933;">,</span> lty=n?<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#a?ade una recta y=a+b*x con el trazo indicado en lty</span>
<a href="http://inside-r.org/r-doc/graphics/legend"><span style="color: #003399; font-weight: bold;">legend</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># a?ade la leyenda en el punto indicado</span>
<a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a><span style="color: #009900;">(</span>main=<span style="color: blue;">""</span> <span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/sub"><span style="color: #003399; font-weight: bold;">sub</span></a>=<span style="color: blue;">""</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#a?ade t?tulo y subt?tulo</span>
<a href="http://inside-r.org/r-doc/graphics/axis"><span style="color: #003399; font-weight: bold;">axis</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># modifica elementos referentes a los ejes como color, tickmarks, fuentes, etc</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>etiquetas<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#a?ade etiquetas en las posiciones marcadas por x e y</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#gr?ficos simples</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/grDevices/nclass"><span style="color: #003399; font-weight: bold;">nclass</span></a><span style="color: #339933;">,</span> breaks<span style="color: #339933;">,</span> probability<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#histograma</span>
mid.age=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">13</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">17.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">22.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">44.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">70.5</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span>
acc.count=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">28</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">46</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">58</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">31</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">149</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">316</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">103</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span>
age.acc=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>mid.age<span style="color: #339933;">,</span> acc.count<span style="color: #009900;">)</span> <span style="color: #339933;">;</span> age.acc <span style="color: #339933;">;</span>
brk=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">17</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>age.acc<span style="color: #339933;">,</span> breaks=brk<span style="color: #009900;">)</span>
A=<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span>A$breaks<span style="color: #009900;">)</span><span style="color: #339933;">,</span> A$mids<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>A$breaks<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> A$counts<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> type=<span style="color: blue;">"l"</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"red"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#histograma con pol?gono de fercuencias</span>
x=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">145</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> n=<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#funci?nd e distribuci?n emp?rica</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sort"><span style="color: #003399; font-weight: bold;">sort</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>: n<span style="color: #009900;">)</span>/n <span style="color: #339933;">,</span> type=?<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a>?<span style="color: #339933;">,</span> ylim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/qqnorm"><span style="color: #003399; font-weight: bold;">qqnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gr?fico de probabilidad normal, en abscisas est?n los valores esperados de los cuantiles de la normal y en las ordenadas los valores de x</span>
<a href="http://inside-r.org/r-doc/stats/qqplot"><span style="color: #003399; font-weight: bold;">qqplot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gr?fico cuantil-cuantil, en abscisas est?n lso cuantiles del vector x y en ordenadas los cuantiles del vector y</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span>vector1<span style="color: #339933;">,</span> vector2<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a>=F o T<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de cajas como vectores</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/subset"><span style="color: #003399; font-weight: bold;">subset</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a>=F o T<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># diagrama de cajas m?ltiple</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de cajas para cada nivel del factor f</span>
<a href="http://inside-r.org/r-doc/graphics/stripchart"><span style="color: #003399; font-weight: bold;">stripchart</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a> o x<span style="color: #339933;">,</span> method=<span style="color: blue;">"overplot"</span> o <span style="color: blue;">"jitter"</span> o <span style="color: blue;">"stack"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gr?fico de puntos</span>
<a href="http://inside-r.org/r-doc/graphics/stem"><span style="color: #003399; font-weight: bold;">stem</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de tallo-hojas</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de barras para una variable num?rica discreta</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de barras para una variable categ?rica</span>
<a href="http://inside-r.org/r-doc/graphics/barplot"><span style="color: #003399; font-weight: bold;">barplot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a> o tabla<span style="color: #339933;">,</span> names.args=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#cada valor representa la altura de una barra que podemos etiquetar opcionalmente con names.args</span>
<a href="http://inside-r.org/r-doc/graphics/barplot"><span style="color: #003399; font-weight: bold;">barplot</span></a><span style="color: #009900;">(</span>matriz o tabla<span style="color: #339933;">,</span> beside=T <span style="color: #009900;">(</span>adyacentes<span style="color: #009900;">)</span> o F <span style="color: #009900;">(</span>apiladas<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de barras m?ltiples apiladas o no.</span>
<a href="http://inside-r.org/r-doc/graphics/pie"><span style="color: #003399; font-weight: bold;">pie</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de sectores</span>
<span style="color: #666666; font-style: italic;">#gr?ficos m?ltiples por ventana</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span>mfrow=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>filas<span style="color: #339933;">,</span> columnas<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#divide la pantalla gr?fica en tantas filas y columnas como se indique</span>
<a href="http://inside-r.org/r-doc/graphics/split.screen"><span style="color: #003399; font-weight: bold;">split.screen</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#datos multivariantes</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#realiza un gr?fico de dispersi?n de todos los pares de variables</span>
<a href="http://inside-r.org/r-doc/graphics/pairs"><span style="color: #003399; font-weight: bold;">pairs</span></a><span style="color: #009900;">(</span>A<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># matriz de gr?ficos planos, un gr?fico de dispersi?n para cada pareja de columnas de A</span>
<a href="http://inside-r.org/r-doc/graphics/coplot"><span style="color: #003399; font-weight: bold;">coplot</span></a><span style="color: #009900;">(</span>a~b|c<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gr?ficos condicionales</span>
<a href="http://inside-r.org/r-doc/graphics/persp"><span style="color: #003399; font-weight: bold;">persp</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa gr?ficamente funciones de 2 variables</span>
<a href="http://inside-r.org/r-doc/graphics/contour"><span style="color: #003399; font-weight: bold;">contour</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa curvas de nivel de una funci?n de 3 variables</span>
<a href="http://inside-r.org/r-doc/graphics/image"><span style="color: #003399; font-weight: bold;">image</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa un mapa de 2 variables</span>
<a href="http://inside-r.org/r-doc/graphics/matplot"><span style="color: #003399; font-weight: bold;">matplot</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa una matriz frente a otra por columnas</span>
<a href="http://inside-r.org/r-doc/graphics/matpoints"><span style="color: #003399; font-weight: bold;">matpoints</span></a> o <a href="http://inside-r.org/r-doc/graphics/matlines"><span style="color: #003399; font-weight: bold;">matlines</span></a> <span style="color: #666666; font-style: italic;"># a?ade puntos o l?neas a un gr?fico (matrices)</span>
<span style="color: #666666; font-style: italic;">#otros</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/lattice">lattice</a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/lattice/xyplot"><span style="color: #003399; font-weight: bold;">xyplot</span></a><span style="color: #009900;">(</span>circumference ~ age<span style="color: #339933;">,</span> groups=<a href="http://inside-r.org/packages/cran/tree">Tree</a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<a href="http://inside-r.org/r-doc/datasets/Orange"><span style="color: #003399; font-weight: bold;">Orange</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># gr?ficos con barra de error: "The R Book" (Crawley, 2009) </span>
<span style="color: #666666; font-style: italic;"># en diagrama de barras</span>
error.bars=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>yv<span style="color: #339933;">,</span>z<span style="color: #339933;">,</span>nn<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
xv=
<a href="http://inside-r.org/r-doc/graphics/barplot"><span style="color: #003399; font-weight: bold;">barplot</span></a><span style="color: #009900;">(</span>yv<span style="color: #339933;">,</span>ylim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>yv<span style="color: #009900;">)</span>+<a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a>=nn<span style="color: #339933;">,</span>ylab=<a href="http://inside-r.org/r-doc/base/deparse"><span style="color: #003399; font-weight: bold;">deparse</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/substitute"><span style="color: #003399; font-weight: bold;">substitute</span></a><span style="color: #009900;">(</span>yv<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
g=<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>xv<span style="color: #009900;">)</span>-<a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span>xv<span style="color: #009900;">)</span><span style="color: #009900;">)</span>/<span style="color: #cc66cc;">50</span>
<span style="color: black; font-weight: bold;">for</span> <span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>xv<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>xv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #339933;">,</span>xv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>yv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>+z<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #339933;">,</span>yv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>-z<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>xv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>-g<span style="color: #339933;">,</span>xv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>+g<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>yv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>+z<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #339933;">,</span> yv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>+z<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>xv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>-g<span style="color: #339933;">,</span>xv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>+g<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>yv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>-z<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #339933;">,</span> yv<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span>-z<span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
comp=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>competition.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span>comp<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>comp<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#[1] "biomass" "clipping"</span>
se=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">28.75</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/as.character"><span style="color: #003399; font-weight: bold;">as.character</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/levels"><span style="color: #003399; font-weight: bold;">levels</span></a><span style="color: #009900;">(</span>clipping<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
ybar=<a href="http://inside-r.org/r-doc/base/as.vector"><span style="color: #003399; font-weight: bold;">as.vector</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>biomass<span style="color: #339933;">,</span>clipping<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
error.bars<span style="color: #009900;">(</span>ybar<span style="color: #339933;">,</span>se<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># en diagrama de puntos</span>
xy.error.bars=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a> <span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>xbar<span style="color: #339933;">,</span>ybar<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> pch=<span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> ylim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span>y-ybar<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>y+ybar<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>xlim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span>x-xbar<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>x+xbar<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/arrows"><span style="color: #003399; font-weight: bold;">arrows</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> y-ybar<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y+ybar<span style="color: #339933;">,</span> code=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> angle=<span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a>=<span style="color: #cc66cc;">0.1</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/arrows"><span style="color: #003399; font-weight: bold;">arrows</span></a><span style="color: #009900;">(</span>x-xbar<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> x+xbar<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> code=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> angle=<span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a>=<span style="color: #cc66cc;">0.1</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
x = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
y = <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span>
xb = <a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>*<span style="color: #cc66cc;">5</span>
yb = <a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>*<span style="color: #cc66cc;">20</span>
xy.error.bars<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>xb<span style="color: #339933;">,</span>yb<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#guardar gr?ficos</span>
<a href="http://inside-r.org/r-doc/grDevices/pdf"><span style="color: #003399; font-weight: bold;">pdf</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/file"><span style="color: #003399; font-weight: bold;">file</span></a>=<span style="color: blue;">"f1,pdf"</span><span style="color: #339933;">,</span> width=<span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> heigth=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/dev.off"><span style="color: #003399; font-weight: bold;">dev.off</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#otra forma</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/dev.copy2eps"><span style="color: #003399; font-weight: bold;">dev.copy2eps</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#el nombre del archivo por defecto es Rplot.eps y el formato es postcript</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<span style="color: #666666; font-style: italic;"># 1.c. Estad?sticos simples #</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<span style="color: #666666; font-style: italic;">#mean Media aritm?tica</span>
<span style="color: #666666; font-style: italic;">#mean(datos, trim=.05) Media truncada o recortada al 5%</span>
<span style="color: #666666; font-style: italic;">#median El percentil 0.5: la mediana</span>
<span style="color: #666666; font-style: italic;">#min El m?nimo de una serie de n?meros</span>
<span style="color: #666666; font-style: italic;">#max El m?ximo de una serie de n?meros</span>
<span style="color: #666666; font-style: italic;">#quantile Los percentiles de una distribuci?n</span>
<span style="color: #666666; font-style: italic;">#range M?nimo y m?ximo de un vector</span>
<span style="color: #666666; font-style: italic;">#sum Suma aritm?tica</span>
<span style="color: #666666; font-style: italic;">#var Cuasi-Varianza y cuasi-covarianza</span>
<span style="color: #666666; font-style: italic;">#var(datos)*(length(datos)-1)/length(datos) Varianza</span>
<span style="color: #666666; font-style: italic;">#sd Cuasi-desviaci?n t?pica</span>
<span style="color: #666666; font-style: italic;">#sqrt(var(datos))*(length(datos)-1)/length(datos) Desviaci?n t?pica</span>
<span style="color: #666666; font-style: italic;">#sd(datos)/abs(mean(datos)) Coeficiente de Variaci?n</span>
<span style="color: #666666; font-style: italic;">#mad Desviaci?n media de la mediana</span>
<span style="color: #666666; font-style: italic;">#skewness Asimetr?a</span>
<span style="color: #666666; font-style: italic;">#kurtosis Curtosis o apuntalamiento</span>
<span style="color: #666666; font-style: italic;">#summary Resumen de estad?sticas de una serie de datos</span>
<span style="color: #666666; font-style: italic;">#fivenum Retorna los 5 n?meros de Tukey (de cajas de Tukey): minimum, lower-hinge, median, upper-hinge, maximum.</span>
<span style="color: #666666; font-style: italic;">#cor Correlaci?n (admite uno o dos argumentos)</span>
<span style="color: #666666; font-style: italic;">#cumsum Suma cumulativa de un vector</span>
<span style="color: #666666; font-style: italic;">#prod El producto de los elementos de un vector</span>
<span style="color: #666666; font-style: italic;">#sample Muestreo aleatorio (y permutaciones)</span>
<span style="color: #666666; font-style: italic;">### Veamos algunos ejemplos:</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#media aritm?tica</span>
<a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/abs"><span style="color: #003399; font-weight: bold;">abs</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#media geom?trica</span>
<span style="color: #cc66cc;">1</span>/mean<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#media arm?nica</span>
<a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#mediana </span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#cuartiles</span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> probs=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#deciles</span>
weigthed.mean<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
IRQ<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/range"><span style="color: #003399; font-weight: bold;">range</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>f<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#para obtener res?menes por grupos</span>
<span style="color: #cc66cc;">100</span>*<a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>/mean<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># CV coeficiente de variaci?n</span>
<span style="color: #666666; font-style: italic;">#Estad?sticas simples:</span>
x = <a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
x
<span style="color: #666666; font-style: italic;"># [1] 1 2 3 4 5 6 7 8 9 10</span>
<a href="http://inside-r.org/r-doc/base/cumsum"><span style="color: #003399; font-weight: bold;">cumsum</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># [1] 1 3 6 10 15 21 28 36 45 55</span>
<a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 5.5</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<span style="color: #666666; font-style: italic;"># 2. Ejemplo: datos Iris</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/head"><span style="color: #003399; font-weight: bold;">head</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>Species<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># resumen estad?stico de 1 variable</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#resumen estad?stico de varias variables</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Species<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#matriz de correlaciones</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gr?fico de un dataframe</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gr?fico de 1 variables </span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gr?fico de 2 variables; ?dem plot(Petal.Length~Sepal.Length)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Species<span style="color: #009900;">[</span>Sepal.Length><span style="color: #cc66cc;">5.1</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gr?fico de 1 factor</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Species<span style="color: #339933;">,</span> Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gr?fico de 1 factor (diagrama de cajas)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#histograma</span>
<a href="http://inside-r.org/r-doc/graphics/pie"><span style="color: #003399; font-weight: bold;">pie</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>Species<span style="color: #009900;">[</span>Sepal.Length><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/qqline"><span style="color: #003399; font-weight: bold;">qqline</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># comparaci?n de un vector con los valores esperados normales</span>
<a href="http://inside-r.org/r-doc/stats/qqplot"><span style="color: #003399; font-weight: bold;">qqplot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># comparaci?n de las distribuciones de dos vectores o variables </span>
cols = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"red"</span><span style="color: #339933;">,</span><span style="color: blue;">"blue"</span><span style="color: #339933;">,</span><span style="color: blue;">"green"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> each=<span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>
pich = <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">21</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">23</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>each=<span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span>Petal.Length<span style="color: #339933;">,</span> pch=pich<span style="color: #339933;">,</span> bg=colores<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span>ctest<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/shapiro.test"><span style="color: #003399; font-weight: bold;">shapiro.test</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># contraste de normalidad</span>
mu=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a>=<a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/ks.test"><span style="color: #003399; font-weight: bold;">ks.test</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> <span style="color: blue;">"pnorm"</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a>=mu<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a>=se<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># contraste de normalidad</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/t.test"><span style="color: #003399; font-weight: bold;">t.test</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># igualdad de medias para dos muestras independientes sin hip?tesis sobre la varianza</span>
<a href="http://inside-r.org/r-doc/stats/var.test"><span style="color: #003399; font-weight: bold;">var.test</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># igualdad de varianzas</span>
<a href="http://inside-r.org/r-doc/stats/wilcox.test"><span style="color: #003399; font-weight: bold;">wilcox.test</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># contraste para muestras no normales de Wilcoxon (o Mann-Whitney)</span>
<a href="http://inside-r.org/r-doc/stats/ks.test"><span style="color: #003399; font-weight: bold;">ks.test</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># Kolmogorov-Smirnov para comparar dos distribuciones continuas </span>
<span style="color: #666666; font-style: italic;">################################################################################ </span>
<span style="color: #666666; font-style: italic;"># 3. Otras funciones # </span>
<span style="color: #666666; font-style: italic;">################################################################################ </span>
<span style="color: #666666; font-style: italic;">######### Primer visi?n de los datos</span>
worms=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/worms.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#las funciones by y aggregate permiten resumir un dataframe seg?n los niveles del factor</span>
<a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a><span style="color: #009900;">(</span>worms<span style="color: #339933;">,</span> Vegetation<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## Sub?ndice e ?ndices</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>:<span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Seleccionar filas aleatoriamente de un Dataframe</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Ordenando los Dataframes</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Slope<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/rev"><span style="color: #003399; font-weight: bold;">rev</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Slope<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #339933;">,</span> Worm.density<span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Usar condiciones l?gicas para seleccionar filas de un Dataframe</span>
worms<span style="color: #009900;">[</span>Damp == T<span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span>Worm.density > <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span>Worm.density<span style="color: #009900;">)</span> & Soil.pH < <span style="color: #cc66cc;">5.2</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>worms<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.numeric"><span style="color: #003399; font-weight: bold;">is.numeric</span></a><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>worms<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.factor"><span style="color: #003399; font-weight: bold;">is.factor</span></a><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Drop filas</span>
worms<span style="color: #009900;">[</span>-<span style="color: #009900;">(</span><span style="color: #cc66cc;">6</span>:<span style="color: #cc66cc;">15</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span>!<span style="color: #009900;">(</span>Vegetation==<span style="color: blue;">"Grassland"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span>-<a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a><span style="color: #009900;">(</span>Damp==F<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Omitir filas que contengan valores perdidos</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>worms.missing.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>
<a href="http://inside-r.org/r-doc/stats/na.omit"><span style="color: #003399; font-weight: bold;">na.omit</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span>
new.frame=<a href="http://inside-r.org/r-doc/stats/na.exclude"><span style="color: #003399; font-weight: bold;">na.exclude</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/stats/complete.cases"><span style="color: #003399; font-weight: bold;">complete.cases</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#### Usar order y unique para eliminar la pseudoreplicaci?n</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/rev"><span style="color: #003399; font-weight: bold;">rev</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Worm.density<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/unique"><span style="color: #003399; font-weight: bold;">unique</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">#### Ordenar de manera compleja con direcciones mixtas </span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #339933;">,</span>-Worm.density<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># solo podemos utilizar el signo de menos cuando ordenamos variables num?ricas, si no debemos utilizar la funci?n rank para hacer num?ricos los niveles de los factores.</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>-<a href="http://inside-r.org/r-doc/base/rank"><span style="color: #003399; font-weight: bold;">rank</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #009900;">)</span><span style="color: #339933;">,</span>-Worm.density<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Un dataframe con nombres en las filas en lugar de n?meros</span>
<a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
worms=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/worms.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#worms=read.table("c:\\temp\\worms.txt",header=T,row.names=1)</span>
worms
<span style="color: #666666; font-style: italic;">##### Crear un dataframe</span>
x=<a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
y=<span style="color: black; font-weight: bold;">letters</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">]</span>
z=<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>T<span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>F<span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/methods/new"><span style="color: #003399; font-weight: bold;">new</span></a>=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span>z<span style="color: #339933;">,</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/methods/new"><span style="color: #003399; font-weight: bold;">new</span></a>
y=<a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1500</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1.5</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
short.frame=<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># expandir un dataframe</span>
long=<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>short.frame<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> short.frame$Freq<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
long<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">##### Eliminar filas duplicadas en un Dataframe</span>
dups=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>dups.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
dups
<a href="http://inside-r.org/r-doc/base/unique"><span style="color: #003399; font-weight: bold;">unique</span></a><span style="color: #009900;">(</span>dups<span style="color: #009900;">)</span>
dups<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/duplicated"><span style="color: #003399; font-weight: bold;">duplicated</span></a><span style="color: #009900;">(</span>dups<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">##### Juntar dos Dataframes: Merge</span>
lifeform=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/lifeforms.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span><span style="color: #339933;">;</span> lifeform
flowering=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/fltimes.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span><span style="color: #339933;">;</span> flowering
<a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>flowering<span style="color: #339933;">,</span>lifeforms<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#contiene solo las filas que tienen entradas completas para ambos dataframes</span>
<span style="color: #009900;">(</span>both=<a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>flowering<span style="color: #339933;">,</span>lifeforms<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/all"><span style="color: #003399; font-weight: bold;">all</span></a>=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># si queremos incluir todas las especies poniendo NA donde no hay coincidencias</span>
<span style="color: #666666; font-style: italic;"># cuando tenemos diferentes nombres en 2 dataframes que queremos unir:</span>
<span style="color: #009900;">(</span>seeds=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>seedwts.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>both<span style="color: #339933;">,</span>seeds<span style="color: #339933;">,</span>by.x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Genus"</span><span style="color: #339933;">,</span><span style="color: blue;">"species"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>by.y=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"name1"</span><span style="color: #339933;">,</span><span style="color: blue;">"name2"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">##### Juntar dos Dataframes: Match</span>
herb=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/herbicides.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
herb
worms$hb=herb$Herbicide<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/match"><span style="color: #003399; font-weight: bold;">match</span></a><span style="color: #009900;">(</span>worms$Vegetation<span style="color: #339933;">,</span>herb$Type<span style="color: #009900;">)</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;">## agregar un herbicida recomendado </span>
<a href="http://inside-r.org/r-doc/base/match"><span style="color: #003399; font-weight: bold;">match</span></a><span style="color: #009900;">(</span>worms$Vegetation<span style="color: #339933;">,</span>herb$Type<span style="color: #009900;">)</span>
worms
<span style="color: #666666; font-style: italic;">##### Resumir los contenidos de un dataframe: </span>
<span style="color: #666666; font-style: italic;">#summary: resume los contenidos de todas las variables </span>
<span style="color: #666666; font-style: italic;">#aggregate: crea una tabla del tipo tapply</span>
<span style="color: #666666; font-style: italic;">#by: crea funciones para cada nivel del factor especificado</span>
<a href="http://inside-r.org/r-doc/stats/aggregate"><span style="color: #003399; font-weight: bold;">aggregate</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>veg=Vegetation<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/aggregate"><span style="color: #003399; font-weight: bold;">aggregate</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>veg=Vegetation<span style="color: #339933;">,</span>d=Damp<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
x1=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> x2=<span style="color: #cc66cc;">11</span>:<span style="color: #cc66cc;">18</span><span style="color: #339933;">;</span> x3=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"i"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>each=<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x4=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"m"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>y=x1<span style="color: #339933;">,</span>z=x2<span style="color: #339933;">,</span>age=x3<span style="color: #339933;">,</span>sex=x4<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$sex<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># returns frequencies</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## apply (to a matrix)</span>
x=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x <span style="color: #666666; font-style: italic;"># 2 rows, 3 cols</span>
<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 21</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 9 12 sum rows(1)</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 3, 7 11 sum cols(2)</span>
<span style="color: #666666; font-style: italic;">## tapply (table apply)</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># tapply(vector,factor,function) for table apply</span>
<a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>=<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$sex<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>
<a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a><span style="color: #009900;">[</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"m"</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># 3 you can reference individual elements</span>
<span style="color: #666666; font-style: italic;"># Unir y cortar vectores de un Dataframe</span>
<a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/PlantGrowth"><span style="color: #003399; font-weight: bold;">PlantGrowth</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># check the default formula</span>
pg = <a href="http://inside-r.org/r-doc/utils/unstack"><span style="color: #003399; font-weight: bold;">unstack</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/PlantGrowth"><span style="color: #003399; font-weight: bold;">PlantGrowth</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># unstack according to this formula</span>
pg
<a href="http://inside-r.org/r-doc/utils/stack"><span style="color: #003399; font-weight: bold;">stack</span></a><span style="color: #009900;">(</span>pg<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># now put it back together</span>
<a href="http://inside-r.org/r-doc/utils/stack"><span style="color: #003399; font-weight: bold;">stack</span></a><span style="color: #009900;">(</span>pg<span style="color: #339933;">,</span> select = -ctrl<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># omitting one vector</span>
<a href="http://inside-r.org/r-doc/base/subset"><span style="color: #003399; font-weight: bold;">subset</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/packages/cran/cond">cond</a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#devuelve una selecci?n de x que cumple unas condiciones</span>
<a href="http://inside-r.org/r-doc/base/split"><span style="color: #003399; font-weight: bold;">split</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> f<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#divide el vector o la hora de datos x en grupos definidos por los valores de f</span>
<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> breaks<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=T o F<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># divide el rango del vector x en intervalos y codifica los elementos de x de acuerdo con el intervalo en el que caigan</span></pre>
</div>
</div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-37555054266949974892012-08-21T02:59:00.001-07:002012-08-21T16:11:13.325-07:00Clase 1: Manejo de datos en R-project.<h2 style="text-align: center;">
Curso de bioestadística aplicada en R</h2>
<br />
Esta es la primera entrada que forma parte de un curso que he diseñado sobre el análisis de datos biológico en R-project. El curso es mas extenso y completo de lo que detallo en las entradas, pero me parecía interesante reunir en este blog una especie de formulario con los principales tipos de análisis de cualquier curso básico de estadística. Iré creando entradas con los códigos (scripts) de cada clase, y cierta información básica de cada tema. Si alguien desea profundizar puede solicitarme información más detallada o realizar el curso online.<br />
Saludos y espero que lo disfruten. <br />
<br /><a href="http://www.scribd.com/doc/103515053/CursoR-Clase1" style="-x-system-font: none; display: block; font-family: Helvetica,Arial,Sans-serif; font-size-adjust: none; font-size: 14px; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal; margin: 12px auto 6px auto; text-decoration: underline;" title="View CursoR Clase1 on Scribd">CursoR Clase1</a><iframe class="scribd_iframe_embed" data-aspect-ratio="0.706697459584296" data-auto-height="true" frameborder="0" height="600" id="doc_35513" scrolling="no" src="http://www.scribd.com/embeds/103515053/content?start_page=1&view_mode=scroll&access_key=key-1bjncxuxjx6i0c0qnf8u" width="100%"></iframe>
<br />
<div style="overflow: auto;">
<div class="geshifilter">
<br />
<br />
<br />
Aquí les dejo los códigos que corresponden a una primera clase de introducción a R-project.<br />
<br /></div>
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###################################################</span>
<span style="color: #666666; font-style: italic;"># Análisis de datos estadísticos con R #</span>
<span style="color: #666666; font-style: italic;"># MANEJO DE DATOS #</span>
<span style="color: #666666; font-style: italic;"># 2012 #</span>
<span style="color: #666666; font-style: italic;"># </span><span class="Apple-style-span" style="color: #666666; font-style: italic;">Rosana Ferrero #</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span class="Apple-style-span" style="color: #666666; font-style: italic;"># Ph.D. student #</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;"># # </span>
<span style="color: #666666; font-style: italic;">###################################################</span>
<span class="Apple-style-span" style="color: #666666;"><i>
</i></span></pre>
</div>
</div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 1. R como calculadora: Aritm?tica y Funciones b?sicas #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># operaciones aritm?ticas b?sicas</span>
<span style="color: #cc66cc;">2</span> + <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># suma</span>
<span style="color: #cc66cc;">2</span> - <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># resta</span>
<span style="color: #cc66cc;">2</span>*<span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># multiplicaci?n</span>
<span style="color: #cc66cc;">2</span>/<span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># divisi?n</span>
<span style="color: #cc66cc;">2</span>^<span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># potenciaci?n</span>
<a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#exponencial</span>
<a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># ra?z cuadrada</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># logaritmo</span>
<a href="http://inside-r.org/r-doc/base/sin"><span style="color: #003399; font-weight: bold;">sin</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/cos"><span style="color: #003399; font-weight: bold;">cos</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/tan"><span style="color: #003399; font-weight: bold;">tan</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># trigonom?tricas</span>
max<span style="color: #339933;">;</span> min<span style="color: #339933;">;</span> range<span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/diff"><span style="color: #003399; font-weight: bold;">diff</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/cumsum"><span style="color: #003399; font-weight: bold;">cumsum</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a> <span style="color: #666666; font-style: italic;"># otras</span>
<span style="color: #666666; font-style: italic;">#operadores l?gicos</span>
<<span style="color: #339933;">,</span> ><span style="color: #339933;">,</span> <=<span style="color: #339933;">,</span> =><span style="color: #339933;">,</span> ==<span style="color: #339933;">,</span> !=
!<span style="color: #339933;">,</span> &<span style="color: #339933;">,</span> |<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/xor"><span style="color: #003399; font-weight: bold;">xor</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
&&<span style="color: #339933;">,</span> ||
<span style="color: black; font-weight: bold;">if</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># precedencia de operaciones</span>
<span style="color: #cc66cc;">4</span>^<span style="color: #cc66cc;">2</span> - <span style="color: #cc66cc;">3</span>*<span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">1</span> - <span style="color: #cc66cc;">6</span> + <span style="color: #cc66cc;">4</span>
<span style="color: #cc66cc;">2</span>^-<span style="color: #cc66cc;">3</span>
<span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span>^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> - <span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span>*<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># usar par?ntisis para agrupar, clarifica</span>
<span style="color: #666666; font-style: italic;"># Funciones, argumentos en las funciones</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base"><span style="color: #006600; font-weight: bold;">base</span></a>=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/log10"><span style="color: #003399; font-weight: bold;">log10</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># equivalente</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> b=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># argumentos abreviados</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># argumentos por posici?n</span>
x=<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># generaci?n de objetos</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 2. R como lenguaje: objetos en R #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Creaci?n de objetos y funciones b?sicas</span>
<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x=<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x <span style="color: #666666; font-style: italic;"># equivalente a print(x)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Ayuda detro del programa</span>
?<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a>
<a href="http://inside-r.org/r-doc/utils/help"><span style="color: #003399; font-weight: bold;">help</span></a><span style="color: #009900;">(</span><span style="color: blue;">"rnorm"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/help.start"><span style="color: #003399; font-weight: bold;">help.start</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
?<a href="http://inside-r.org/r-doc/utils/help.search"><span style="color: #003399; font-weight: bold;">help.search</span></a>
<a href="http://inside-r.org/r-doc/utils/help.search"><span style="color: #003399; font-weight: bold;">help.search</span></a><span style="color: #009900;">(</span><span style="color: blue;">"normal"</span><span style="color: #009900;">)</span>
?<a href="http://inside-r.org/r-doc/utils/apropos"><span style="color: #003399; font-weight: bold;">apropos</span></a>
<a href="http://inside-r.org/r-doc/utils/apropos"><span style="color: #003399; font-weight: bold;">apropos</span></a><span style="color: #009900;">(</span><span style="color: blue;">"normal"</span><span style="color: #009900;">)</span>
?<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics"><span style="color: #006600; font-weight: bold;">graphics</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics/persp"><span style="color: #003399; font-weight: bold;">persp</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span>lm.glm<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Listado y remoci?n de objetos en memoria</span>
<a href="http://inside-r.org/r-doc/base/ls"><span style="color: #003399; font-weight: bold;">ls</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para saber los objetos que tenemos en el espacio de trabajo</span>
x <span style="color: #666666; font-style: italic;"># escribir el nombre de un objeto muestra su contenido</span>
<a href="http://inside-r.org/r-doc/base/save"><span style="color: #003399; font-weight: bold;">save</span></a><span style="color: #009900;">(</span><objetos><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/file"><span style="color: #003399; font-weight: bold;">file</span></a>=<span style="color: blue;">"nombre.RData"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para guardar el contenido del espacio de trabajo. Tambi?n se puede usar: save.image() , si queremos que guarde todo.</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span><span style="color: blue;">"misdatos.RData"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para acceder a los objetos de la carpeta de trabajo</span>
<a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span><span style="color: blue;">"misdatos.RData"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para deshacer la selecci?n</span>
<a href="http://inside-r.org/r-doc/base/rm"><span style="color: #003399; font-weight: bold;">rm</span></a><span style="color: #009900;">(</span><span style="color: blue;">"x"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># borra el objeto "x", y rm() todo el escritorio de trabajo</span>
<span style="color: #666666; font-style: italic;"># Tipos de objetos:</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/utils/str"><span style="color: #003399; font-weight: bold;">str</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Vector: colecci?n ordenada de elementos del mismo tipo</span>
x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> y=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span> <span style="color: blue;">"b"</span><span style="color: #339933;">,</span> <span style="color: blue;">"c"</span><span style="color: #009900;">)</span>
z=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/is.vector"><span style="color: #003399; font-weight: bold;">is.vector</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.vector"><span style="color: #003399; font-weight: bold;">is.vector</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.vector"><span style="color: #003399; font-weight: bold;">is.vector</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #339933;">;</span> y<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> z<span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;">#c?mo acceder a los elementos del vector</span>
<span style="color: #666666; font-style: italic;"># Matrices y Array: generalizaci?n multidimensional del vector. Elementos del mismo tipo. Varias columnas (o filas) de vectores</span>
<a href="http://inside-r.org/r-doc/base/is.array"><span style="color: #003399; font-weight: bold;">is.array</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
a=<a href="http://inside-r.org/r-doc/base/array"><span style="color: #003399; font-weight: bold;">array</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> a<span style="color: #339933;">;</span> a<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> a<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,,</span><a href="http://inside-r.org/r-doc/base/drop"><span style="color: #003399; font-weight: bold;">drop</span></a>=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">]</span>
b=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> b<span style="color: #339933;">;</span> b<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Data frame: similar al array, pero con columnas de diferentes tipos.</span>
dates=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>ID=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"gen0"</span><span style="color: #339933;">,</span><span style="color: blue;">"genB"</span><span style="color: #339933;">,</span><span style="color: blue;">"genZ"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>subj1=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">33</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>subj2=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">34</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>oncogen=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>loc=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">125</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Factor: tipo de vector para datos cualitativos (o categ?ricos)</span>
x=<a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># List: vector generalizado. Sus componentes pueden ser tambi?n listas, o pueden ser de distinto tipo. No tienen estructura.</span>
una.lista=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>un.vector=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>una.palabra=<span style="color: blue;">"hola"</span><span style="color: #339933;">,</span>una.matriz=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>lista2=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>=<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>b=<a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"b"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Funciones: c?digo</span>
f1=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #009900;">{</span><a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*x<span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.function"><span style="color: #003399; font-weight: bold;">is.function</span></a><span style="color: #009900;">(</span>f1<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Atributos de los objetos</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">15</span><span style="color: #339933;">;</span> y=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> z=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
w=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>a=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>b=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>w<span style="color: #009900;">)</span>
f1=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #009900;">{</span><a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*x<span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>f1<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.function"><span style="color: #003399; font-weight: bold;">is.function</span></a><span style="color: #009900;">(</span>f1<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Modo (mode): l?gico, entero, real, car?cter, etc.</span>
<a href="http://inside-r.org/r-doc/base/mode"><span style="color: #003399; font-weight: bold;">mode</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/mode"><span style="color: #003399; font-weight: bold;">mode</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/mode"><span style="color: #003399; font-weight: bold;">mode</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Tipo (typeof): double, character...</span>
x1=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> x2=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x3=<span style="color: blue;">"patata"</span>
<a href="http://inside-r.org/r-doc/base/typeof"><span style="color: #003399; font-weight: bold;">typeof</span></a><span style="color: #009900;">(</span>x1<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/typeof"><span style="color: #003399; font-weight: bold;">typeof</span></a><span style="color: #009900;">(</span>x2<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/typeof"><span style="color: #003399; font-weight: bold;">typeof</span></a><span style="color: #009900;">(</span>x3<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Nombres (names): etiquetas.</span>
<span style="color: #666666; font-style: italic;">#Poner un nombre en min?scula no es lo mismo que ponerlo en may?scula (R es "case-sensitive": x!=X)</span>
<span style="color: #666666; font-style: italic;">#Hay nombres reservados, por lo que hay que evitarlos. Ej: "function", "if"</span>
z = <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>a=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> b=<span style="color: blue;">"c"</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a>=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Dimensiones (dim): n?mero de filas y columnas (puede ser cero)</span>
<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Nombres de las dimensiones de los arrays (dimnames)</span>
y=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"8am"</span><span style="color: #339933;">,</span><span style="color: blue;">"9am"</span><span style="color: #339933;">,</span><span style="color: blue;">"10am"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># [[1]] para filas y [[2]] para columnas</span>
<a href="http://inside-r.org/r-doc/base/colnames"><span style="color: #003399; font-weight: bold;">colnames</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"lunes"</span><span style="color: #339933;">,</span> <span style="color: blue;">"martes"</span><span style="color: #339933;">,</span> <span style="color: blue;">"mi?rcoles"</span><span style="color: #339933;">,</span> <span style="color: blue;">"jueves"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># alternativa</span>
<span style="color: #666666; font-style: italic;"># Clase (class): lista de las clases del objeto (vector alfa-num?rico)</span>
x = <span style="color: #cc66cc;">10</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># "numeric"</span>
<span style="color: #666666; font-style: italic;"># Otros: por ejemplo, para series temporales.</span>
<span style="color: #666666; font-style: italic;">### Generaci?n de secuencias</span>
<span style="color: #666666; font-style: italic;"># Secuencias aleatorias</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>sample<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span>
probs=x/sum<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span>sample<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/packages/cran/prob">prob</a>=probs<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Secuencias discretas y continuas</span>
x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span>
x=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span>from=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>to=<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Secuencias con repeticiones</span>
<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/gl"><span style="color: #003399; font-weight: bold;">gl</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#como rep(1:3, rep(5,3))</span>
<a href="http://inside-r.org/r-doc/base/expand.grid"><span style="color: #003399; font-weight: bold;">expand.grid</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span>:<span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Indexaci?n de vectores</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> x<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
x<span style="color: #009900;">[</span>x><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>
y=x><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> x<span style="color: #009900;">[</span>y<span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"b"</span><span style="color: #339933;">,</span><span style="color: blue;">"c"</span><span style="color: #339933;">,</span><span style="color: blue;">"d"</span><span style="color: #339933;">,</span><span style="color: blue;">"e"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x<span style="color: #009900;">[</span><span style="color: blue;">"c"</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Valores ausentes: NA (not available); NaN (not a number); Inf (infinito)</span>
x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">]</span>=<span style="color: #cc66cc;">0</span> <span style="color: #666666; font-style: italic;">#sustituir NA por 0</span>
<a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span>/<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/is.nan"><span style="color: #003399; font-weight: bold;">is.nan</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span>/<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/is.infinite"><span style="color: #003399; font-weight: bold;">is.infinite</span></a><span style="color: #009900;">(</span>-<span style="color: #cc66cc;">5</span>/<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#quitar NA al evaluar la media</span>
x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> y=<a href="http://inside-r.org/r-doc/stats/na.omit"><span style="color: #003399; font-weight: bold;">na.omit</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> y <span style="color: #666666; font-style: italic;">#para funciones de modelado como lm es mejor usar "na.omit" o "na.exclude", que eliminan los NA</span>
<span style="color: #666666; font-style: italic;">### Orden de vectores</span>
x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>decreasing=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/sort"><span style="color: #003399; font-weight: bold;">sort</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/rank"><span style="color: #003399; font-weight: bold;">rank</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">]</span>
x<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>decreasing=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Vector de caracteres</span>
pegar=<a href="http://inside-r.org/r-doc/base/paste"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"A"</span><span style="color: #339933;">,</span><span style="color: blue;">"B"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>sep=<span style="color: blue;">""</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> pegar
<a href="http://inside-r.org/r-doc/base/substr"><span style="color: #003399; font-weight: bold;">substr</span></a><span style="color: #009900;">(</span><span style="color: blue;">"abcdef"</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
y=<a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> y
x = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> = <span style="color: blue;">"asfef"</span><span style="color: #339933;">,</span> qu = <span style="color: blue;">"qwerty"</span><span style="color: #339933;">,</span> <span style="color: blue;">"yuiop["</span><span style="color: #339933;">,</span> <span style="color: blue;">"b"</span><span style="color: #339933;">,</span> <span style="color: blue;">"stuff.blah.yech"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># split x on the letter e</span>
<a href="http://inside-r.org/r-doc/base/strsplit"><span style="color: #003399; font-weight: bold;">strsplit</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <span style="color: blue;">"e"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Discretizar</span>
v=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">9</span>
cut1=<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> cut1
cut2=<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>include.lowest=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> cut2
cut3=<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>include.lowest=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> cut3
<span style="color: #666666; font-style: italic;">### Combinar vectores o matrices</span>
<a href="http://inside-r.org/r-doc/base/rbind"><span style="color: #003399; font-weight: bold;">rbind</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
a=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
mediai=media=NULL<span style="color: #339933;">;</span>for<span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>mediai=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>a<span style="color: #009900;">[</span>i<span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> media=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>media<span style="color: #339933;">,</span>mediai<span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #666666; font-style: italic;"># loop</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># aplica una funci?n a filas, columnas o ambas. Hay que especificar el "margin"</span>
<a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># simplifica el resultado a un vector o una matriz</span>
<a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># devuelve una lista</span>
<a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span><span style="color: #339933;">;</span> groups = <a href="http://inside-r.org/r-doc/base/as.factor"><span style="color: #003399; font-weight: bold;">as.factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rbinom"><span style="color: #003399; font-weight: bold;">rbinom</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">32</span><span style="color: #339933;">,</span> n = <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/packages/cran/prob">prob</a> = <span style="color: #cc66cc;">0.4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>groups<span style="color: #339933;">,</span> groups<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/aggregate"><span style="color: #003399; font-weight: bold;">aggregate</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/state.x77"><span style="color: #003399; font-weight: bold;">state.x77</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>Region = <a href="http://inside-r.org/r-doc/datasets/state.region"><span style="color: #003399; font-weight: bold;">state.region</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Programaci?n en R</span>
<span style="color: #666666; font-style: italic;">#Definici?n de funciones</span>
funcion1=<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>A<span style="color: #339933;">,</span>B<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/C"><span style="color: #003399; font-weight: bold;">C</span></a><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>x1=A*B/C<span style="color: #339933;">;</span> x1<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#Argumentos</span>
<a href="http://inside-r.org/r-doc/base/args"><span style="color: #003399; font-weight: bold;">args</span></a><span style="color: #009900;">(</span>funcion1<span style="color: #009900;">)</span>
funcion1<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Control de ejecuci?n: condicionales, loops</span>
f = <a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">letters</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span> i <span style="color: black; font-weight: bold;">in</span> <a href="http://inside-r.org/r-doc/base/unique"><span style="color: #003399; font-weight: bold;">unique</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">)</span> <span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/print"><span style="color: #003399; font-weight: bold;">print</span></a><span style="color: #009900;">(</span>i<span style="color: #009900;">)</span>
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: black; font-weight: bold;">if</span><span style="color: #009900;">(</span>x==<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>x=x+<span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: black; font-weight: bold;">else</span> <span style="color: #009900;">{</span>x=x+<span style="color: #cc66cc;">2</span><span style="color: #009900;">}</span><span style="color: #339933;">;</span> x
x=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/ifelse"><span style="color: #003399; font-weight: bold;">ifelse</span></a><span style="color: #009900;">(</span>x %% <span style="color: #cc66cc;">2</span> == <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: blue;">"Odd"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Even"</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">while</span><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span>
centre = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> type<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span>type<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a> = <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> trimmed = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> trim = <span style="color: #cc66cc;">.1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
x = <a href="http://inside-r.org/r-doc/stats/rcauchy"><span style="color: #003399; font-weight: bold;">rcauchy</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> centre<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <span style="color: blue;">"mean"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Control de errores: traceback, browser, debug</span>
<a href="http://inside-r.org/r-doc/base/debug"><span style="color: #003399; font-weight: bold;">debug</span></a><span style="color: #009900;">(</span>centre<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/undebug"><span style="color: #003399; font-weight: bold;">undebug</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Control de la duraci?n de la operaci?n:</span>
<a href="http://inside-r.org/r-doc/base/unix.time"><span style="color: #003399; font-weight: bold;">unix.time</span></a><span style="color: #009900;">(</span>centre<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: blue;">"mean"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#unix.time, Rprof</span>
<a href="http://inside-r.org/r-doc/base/traceback"><span style="color: #003399; font-weight: bold;">traceback</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
</pre>
</div>
</div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com1tag:blogger.com,1999:blog-6841022238556785612.post-24693858089460906222012-08-18T04:46:00.001-07:002012-08-18T04:46:08.830-07:00Regresión: selección de variables. Stepwise, Forward, Backward<div style="text-align: center;">
<b><span class="Apple-style-span" style="font-size: large;">¿Cómo realizar una selección de variables predictoras para un modelo de regresión lineal múltiple?</span></b></div>
<div style="text-align: center;">
<b><span class="Apple-style-span" style="font-size: large;"><br /></span></b></div>
<div style="text-align: center;">
<b><span class="Apple-style-span" style="font-size: large;"><br /></span></b></div>
A menudo nos encontramos estudiando un gran número de variables explicativas (regresoras, predictoras, causales o independientes) y nos preguntamos cuál de ellas incluir en un modelo de regresión múltiple tanto por problemas de multicolinealidad como por su significación en la regresión.<br />
<br />
<b><br /></b>
<b>Estrategias de regresión</b><br />
<br />
Los métodos más comunes de selección de variables son: el de pasos sucesivos (stepwise), el de introducción progresiva (forward) y el de eliminación progresiva (backward).<br />
<br />
<div class="column">
<ul style="list-style-type: none;">
<li>
<span style="color: #3333b3; font-family: CMSS8;">Eliminación hacia atrás (Backward Stepwise Regression). </span><span style="font-family: CMSS8;">Se
introducen todas las variables en la ecuación y después se van
excluyendo una tras otra. En cada etapa se elimina la variable
menos influyente según el contraste individual (de la </span><span style="font-family: CMSSI8;">t </span><span style="font-family: CMSS8;">o de la </span><span style="font-family: CMSSI8;">F).
</span><br />
</li>
<li>
<span style="color: #3333b3; font-family: MSAM5; vertical-align: 1pt;"> </span><span style="color: #3333b3; font-family: CMSS8;">Selección hacia adelante (Fordward Stepwise Regression). </span><span style="font-family: CMSS8;">Las
variables se introducen secuencialmente en el modelo. La primera
variable que se introduce es la de mayor correlación (+ o -) con la
variable dependiente. Dicha variable se introducirá en la ecuación
solo si cumple el criterio de entrada. A continuación se<br />
considerará la variable independiente cuya correlación parcial sea la
mayor y que no esté en la ecuación. El procedimiento termina
cuando ya no quedan variables que cumplan el criterio de entrada.
</span><br />
</li>
<li>
<span style="color: #3333b3; font-family: MSAM5; vertical-align: 1pt;"> </span><span style="color: #3333b3; font-family: CMSS8;">Pasos sucesivos (Stepwise Regression). </span><span style="font-family: CMSS8;">Este método es una
combinación de los procedimientos anteriores. En cada paso se
introduce la variable independiente que no se encuentre ya en la
ecuación y que tenga la probabilidad para </span><span style="font-family: CMSSI8;">F </span><span style="font-family: CMSS8;">más pequeña. Las
variables ya introducidas en la ecuación de regresión pueden ser
eliminadas del modelo. El método termina cuando ya no hay más
variables candidatas a ser incluidas o eliminadas. </span><br />
<span style="font-family: CMSS8;">Fuente: <a href="http://eio.usc.es/eipc1/BASE/BASEMASTER/FORMULARIOS-PHP-DPTO/MATERIALES/Mat_50140129_RegresionMultiple.pdf">http://eio.usc.es/eipc1/BASE/BASEMASTER/FORMULARIOS-PHP-DPTO/MATERIALES/Mat_50140129_RegresionMultiple.pdf</a></span><br />
</li>
</ul>
</div>
<br />
<br />
<b>Criterios de elección de variables</b><br />
<br />
Cuando se dispone de muchas variables explicativas potenciales, las estrategias de regresión anteriores definen normalmente un subconjunto posible de modelos y el problema es seleccionar entre ellos. Para ellos se puede utilizar distintos estadísticos, como el criterio de información de Akaike AIC, el citerior de información Bayesiana BIC, el coeficiente de determinación R2, el coeficiente de determinación corregido R2aj, la varianza residual o el estadístico Cp de Mallows.<br />
<br />
<br />
<b>Implementación en R-project</b><br />
<br />
El programa R-project, al igual que otros programas (SPSS, SAS, StatSoft Statistica, etc.), cuenta con librerías específicas para la selección de variables explicativas. Entre ellas encontramos la función<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> stepAIC</span> del paquete <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">MASS</span>, que permite seleccionar el método mediante el argumento<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> <span class="Apple-style-span" style="font-size: 13px; white-space: pre;">direction = c("both", "backward", "forward").</span></span><br />
<span class="Apple-style-span" style="font-family: monospace; font-size: 13px; white-space: pre;"><br /></span>
También está la función <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">leaps</span> del paquete<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> leaps</span>, que permite realizar todas las regresiones con subconjuntos de variables (<a href="http://cran.r-project.org/web/packages/leaps/leaps.pdf">package</a>).<br />
<br />
La función <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">eleaps </span>del paquete <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">subselect</span> permite realizar la selección de variables mediante ocho criterios diferentes (<a href="http://cran.r-project.org/web/packages/subselect/vignettes/subselect.pdf">vignettes</a>).<br />
<br />
Sin embargo, algunos investigadores plantean que la selección del modelo mediante un criterio automático no es recomendable (e.g. Peña 1998). La práctica, dicen, muestra que los buenos modelos suelen coincidir con cualquier criterio razonable y la selección final del modelo es mejor hacerla en base a su adecuación lógica a la realidad que describe. Si esto no es suficiente, recomiendan utilizar criterios de validez externa.<br />
Por ello, para explicar el funcionamiento de los métodos de selección de variables predictoras, en esta entrada no utilizaré paquetes sino los pasos específicos para los tres métodos que he comentado.<br />
<br />
<br />
<b>Funciones de correlación parcial</b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"><br /></span></b>
<b><span class="Apple-style-span" style="font-weight: normal;">Primero, es necesario especificar las funciones de correlación parcial tanto de primer orden como de segundo y tercer orden. (NOTA: podemos encontrar librerías específicas para estas funciones en R-project).</span></b><br />
<br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#correlación parcial de primer orden</span>
pcor = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v2<span style="color: #339933;">,</span> v3<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
c12 = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v2<span style="color: #009900;">)</span>
c23 = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>v2<span style="color: #339933;">,</span> v3<span style="color: #009900;">)</span>
c13 = <a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v3<span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/pARtial">partial</a> = <span style="color: #009900;">(</span>c12-<span style="color: #009900;">(</span>c13*c23<span style="color: #009900;">)</span><span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-<span style="color: #009900;">(</span>c13^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> * <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-<span style="color: #009900;">(</span>c23^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/pARtial">partial</a><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#correlación parcial de segundo orden</span>
pcor2 = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v2<span style="color: #339933;">,</span> v3<span style="color: #339933;">,</span> v4<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
c123 = pcor<span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v2<span style="color: #339933;">,</span> v3<span style="color: #009900;">)</span>
c143 = pcor<span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v4<span style="color: #339933;">,</span> v3<span style="color: #009900;">)</span>
c243 = pcor<span style="color: #009900;">(</span>v2<span style="color: #339933;">,</span> v4<span style="color: #339933;">,</span> v3<span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/pARtial">partial</a> = <span style="color: #009900;">(</span>c123-<span style="color: #009900;">(</span>c143*c243<span style="color: #009900;">)</span><span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-<span style="color: #009900;">(</span>c143^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> * <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-<span style="color: #009900;">(</span>c243^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/pARtial">partial</a><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#correlación parcial de tercer orden</span>
pcor3 = <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v2<span style="color: #339933;">,</span> v3<span style="color: #339933;">,</span> v4<span style="color: #339933;">,</span> v5<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#r12|345=(r12|34-r15|34*r25|34)/sqrt((1-r15|34^2)*(1-r25|34^2))</span>
<span style="color: #009900;">{</span>
c1234 = pcor2<span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v2<span style="color: #339933;">,</span> v3<span style="color: #339933;">,</span> v4<span style="color: #009900;">)</span>
c1534 = pcor2<span style="color: #009900;">(</span>v1<span style="color: #339933;">,</span> v5<span style="color: #339933;">,</span> v3<span style="color: #339933;">,</span> v4<span style="color: #009900;">)</span>
c2534 = pcor2<span style="color: #009900;">(</span>v2<span style="color: #339933;">,</span> v5<span style="color: #339933;">,</span> v3<span style="color: #339933;">,</span> v4<span style="color: #009900;">)</span>
<a href="http://inside-r.org/packages/cran/pARtial">partial</a> = <span style="color: #009900;">(</span>c1234-<span style="color: #009900;">(</span>c1534*c2534<span style="color: #009900;">)</span><span style="color: #009900;">)</span>/<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-<span style="color: #009900;">(</span>c1534^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> * <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>-<span style="color: #009900;">(</span>c2534^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/pARtial">partial</a><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;">
</pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><b>
</b></span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><b>Algoritmos de selección de variables en modelos de regresión.</b></span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><b>
</b></span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">(próximamente)</span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><b>
</b></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;">
</pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="white-space: normal;"><b>Ejemplo en R.</b></span></span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="white-space: normal;">
</span></span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="white-space: normal;">Segundo, utilizaremos un conjunto de datos bien conocido para estos procedimientos. Se trata</span></span><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"> de un estudio que analiza la relación entre la composición de un cemento tipo Portland y el calor desprendido durante la fase de fraguado. La muestra está formada por 13 cementos.</span> <span class="Apple-style-span" style="font-family: Times; white-space: normal;">Y es la cantidad de calor desprendido (cals/gr), mientras que las</span><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"> variables X1 , X2 , X3 y X4 representan el contenido ( %) de cuatro </span><span class="Apple-style-span" style="font-family: Times; white-space: normal;">ingredientes activos.</span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times;"><span class="Apple-style-span" style="white-space: normal;">
</span></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;"><a href="http://inside-r.org/r-doc/base/library">library</a></span><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/MASS">MASS</a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/MASS/cement"><span style="color: #003399; font-weight: bold;">cement</span></a><span style="color: #009900;">)</span>
datos=<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/MASS/cement"><span style="color: #003399; font-weight: bold;">cement</span></a><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/str"><span style="color: #003399; font-weight: bold;">str</span></a><span style="color: #009900;">(</span>datos<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span>datos<span style="color: #009900;">)</span>
</pre>
<pre class="r geshifilter-R"><span style="font-family: Times; white-space: normal;">Utilizaremos los tres métodos más comunes de selección de variables predictivas para determinar qué variables serán incluidas en el modelo final.</span></pre>
<pre class="r geshifilter-R"><span style="color: #666666; font-family: monospace; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R"><span style="color: #666666; font-family: monospace; font-style: italic;">
</span></pre>
<pre class="r geshifilter-R"><span style="color: #666666; font-family: monospace; font-style: italic;">#### FORWARD</span>
N=<span style="color: #cc66cc; font-family: monospace;">13</span>
p=<span style="color: #cc66cc; font-family: monospace;">1</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#el parámetro del intercepto</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#mayor correlación: x4</span>
<a href="http://inside-r.org/r-doc/stats/cor" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/MASS/cement" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cement</span></a><span style="color: #009900; font-family: monospace;">)</span>
summary.aov<span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/stats/lm" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900; font-family: monospace;">(</span>y<span style="font-family: monospace;">~</span>x4<span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#Fexp=22.8</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/stats/cor" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span><a href="http://inside-r.org/r-doc/stats/cor" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 24.87111</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.747225</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo, entra x4</span>
p=p<span style="font-family: monospace;">+</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#mayor correlación parcial de primer orden: x1</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 108.2239</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.964603</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo, entra x1</span>
p=p<span style="font-family: monospace;">+</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#mayor correlación parcial de segundo orden: x2</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 0.5986053</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#-0.5657105</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.025865</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.117355</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#Como Fexp<Fteo, no entra x2</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#FIN: el modelo final incluye a x1 y x4</span></pre>
<pre class="r geshifilter-R">
</pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">El modelo final incluirá las variables X1 y X4.</span></pre>
<pre class="r geshifilter-R">
</pre>
<pre class="r geshifilter-R">
</pre>
<pre class="r geshifilter-R">
</pre>
<pre class="r geshifilter-R">
<span style="color: #666666; font-family: monospace; font-style: italic;">#### BACKWARD</span>
N=<span style="color: #cc66cc; font-family: monospace;">13</span>
p=<span style="color: #cc66cc; font-family: monospace;">4</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#todos los parámetros</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#menor correlación parcial de tercer orden: x3</span>
pcor3<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x3<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.5929326</span>
pcor3<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x3<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.2418094</span>
pcor3<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.04768649</span>
pcor3<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x3<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#-0.07164829</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor3<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor3<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 0.01823347</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.317655</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp<Fteo, sale x3</span>
p=p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#menor correlación parcial de segundo orden: x4 (menor r)</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.972002</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.5986053</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#-0.4141492</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 1.863262</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.117355</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp<Fteo, sale x4</span>
p=p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#menor correlación parcial de primer orden: x1 (menor r)</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.9675285</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#0.9768575</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 146.5227</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.964603</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo,no sale x1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#FIN: el modelo final incluye a x1 y x2</span>
</pre>
<pre class="r geshifilter-R"><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">El modelo final incluirá las variables X1 y X2.</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
</span></pre>
<pre class="r geshifilter-R"><span style="color: #666666; font-family: monospace; font-style: italic;">#### STEPWISE</span>
N=<span style="color: #cc66cc; font-family: monospace;">13</span>
p=<span style="color: #cc66cc; font-family: monospace;">0</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#sin el parámetro del intercepto</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#mayor correlación: x4</span>
<a href="http://inside-r.org/r-doc/stats/cor" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/MASS/cement" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cement</span></a><span style="color: #009900; font-family: monospace;">)</span>
summary.aov<span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/stats/lm" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900; font-family: monospace;">(</span>y<span style="font-family: monospace;">~</span>x4<span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#Fexp=22.8</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/stats/cor" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span><a href="http://inside-r.org/r-doc/stats/cor" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#22.79852</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#4.844336</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 24.87111</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.747225</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo, entra x4</span>
p=p<span style="font-family: monospace;">+</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#mayor correlación parcial de primer orden: x1</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span>
pcor<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#108.2239</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 4.964603</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 119.0463</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.844336</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo, entra x1</span>
p=p<span style="font-family: monospace;">+</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#pruebo la significación de las variables introducidas en pasos anteriores: x4</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#159.2952</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 4.964603</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 159.2952</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.964603</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo, sigo sin eliminar x4</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#mayor correlación parcial de segundo orden: x2</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 0.5986053</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#-0.5657105</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x2<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#5.584294</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 4.964603</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.584294</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 4.964603</span>
<span style="color: #666666; font-family: monospace; font-style: italic;"># como Fexp>Fteo entra x2</span>
p=p<span style="font-family: monospace;">+</span><span style="color: #cc66cc; font-family: monospace;">1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#pruebo la significación de las variables introducidas en pasos anteriores: x4 y x1</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x4<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x1<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#2.070292</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 4.964603</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 1.863262</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.117355</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp<Fteo, debo eliminar x4</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span> x2<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x1<span style="color: #339933; font-family: monospace;">,</span> y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x4<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;">#171.1196</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 4.964603</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 154.0076</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.117355</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp>Fteo, se queda x1</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#solo queda x3, correlación parcial de segundo orden: x3</span>
pcor2<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span> <span style="color: #666666; font-family: monospace; font-style: italic;"># 0.4112643</span>
Fexp=<span style="color: #009900; font-family: monospace;">(</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="font-family: monospace;">*</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">/</span><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="font-family: monospace;">-</span>pcor2<span style="color: #009900; font-family: monospace;">(</span>x3<span style="color: #339933; font-family: monospace;">,</span>y<span style="color: #339933; font-family: monospace;">,</span>x2<span style="color: #339933; font-family: monospace;">,</span>x1<span style="color: #009900; font-family: monospace;">)</span><span style="font-family: monospace;">^</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">)</span>
Fteo=<a href="http://inside-r.org/r-doc/stats/qf" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">qf</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">.95</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #009900; font-family: monospace;">(</span>N<span style="font-family: monospace;">-</span>p<span style="font-family: monospace;">-</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
Fexp <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 1.832128</span>
Fteo <span style="color: #666666; font-family: monospace; font-style: italic;">#[1] 5.117355</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#como Fexp<Fteo, no entra x3.</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#FIN: modelo final con x1 y x2</span></pre>
</div>
</div>
<br />
<br />
<br />
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">El modelo final incluirá las variables X1 y X2.</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">(NOTA: También hay investigadores que fijan de antemano el criterio de inclusión o exclusión de antemano, el Fteórico).</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Espero que este material les sea de ayuda. Saludos.</span></pre>
<pre class="r geshifilter-R" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">
</span></pre>
Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com1tag:blogger.com,1999:blog-6841022238556785612.post-90584366727368509112012-05-14T10:45:00.001-07:002012-07-21T03:02:32.253-07:00Bondad de ajuste en modelos no lineales<br />
<div class="gmail_quote">
<h1 itemprop="name" style="border-bottom-width: 0px; border-collapse: separate; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; font-weight: bold; line-height: 12px; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 9px; padding-left: 9px; padding-right: 9px; padding-top: 9px; text-align: center; vertical-align: baseline;">
<span style="font-family: CMR10; font-size: 11pt;"><i>“All models are wrong, but some are useful”</i> - George Box. </span></h1>
<h1 itemprop="name" style="border-bottom-width: 0px; border-collapse: separate; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; font-weight: bold; line-height: 12px; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 9px; padding-left: 9px; padding-right: 9px; padding-top: 9px; vertical-align: baseline;">
</h1>
Partiendo de la idea anterior, en esta entrada del blog intentaremos dar algunas ideas sobre cómo seleccionar el modelo "más" útil, o el menos inútil. Para ello debemos describir qué aspectos de los datos son capturados correctamente por el modelo y cuáles no. Compararemos el "ajuste" de varios modelos, considerando el concepto de parsimonia. Es decir, no elegiremos el modelo basados solamente en que reproduzca mejor los datos observados, sino que el modelo seleccionado deberá ser el más simple (el más parsimonioso). Existen numerosos índices de ajuste para evaluar el grado de congruencia entre el modelo y los datos, y se recomienda presentar varios de ellos para complementar el análisis.</div>
<div class="gmail_quote">
Las medidas que presentaré se pueden utilizar para cualquier tipo de modelo de regresión, pero es especialmente útil para modelos no-lineales, donde no podemos usar el famoso R2.</div>
<div class="gmail_quote">
<br />
<h1 itemprop="name" style="border-bottom-width: 0px; border-collapse: separate; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 18px; font-weight: bold; line-height: 12px; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 9px; padding-left: 9px; padding-right: 9px; padding-top: 9px; vertical-align: baseline;">
¿CÓMO EVALUAR LA BONDAD DE AJUSTE EN UN MODELO NO LINEAL?</h1>
<h1 itemprop="name" style="border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 9px; padding-left: 9px; padding-right: 9px; padding-top: 9px; vertical-align: baseline;">
<ul>
<li><span class="Apple-style-span" style="color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium;"><span class="Apple-style-span" style="line-height: 12px;">ESTADÍSTICOS DE BONDAD DE AJUSTE</span></span></li>
<li><span class="Apple-style-span" style="color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium;"><span class="Apple-style-span" style="line-height: 12px;">ANÁLISIS DE LOS RESIDUOS</span></span></li>
<li><span class="Apple-style-span" style="color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium; line-height: 12px;">INTERVALOS DE CONFIANZA Y PREDICCIÓN</span></li>
</ul>
<div>
<span class="Apple-style-span" style="color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium;"><span class="Apple-style-span" style="line-height: 12px;"><br /></span></span></div>
<ul>
<li><span class="Apple-style-span" style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px; font-weight: normal;"><span class="Apple-style-span" style="border-collapse: separate; color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium; font-weight: bold; line-height: 12px;">ESTADÍSTICOS DE BONDAD DE AJUSTE o BONDAD DE PREDICCIÓN</span></span></li>
</ul>
<span class="Apple-style-span" style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px; font-weight: normal;">Este es un resumen de los principales estadísticos/medidas que se pueden calcular mediante R.</span>
</h1>
</div>
<div class="gmail_quote">
<div class="gmail_quote" style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<div>
<ul>
<li style="margin-left: 15px;"><b>deviance. </b>Función del paquete <b><a href="http://stat.ethz.ch/R-manual/R-patched/library/stats/html/stats-package.html">stat</a></b>. La "Devianza" del modelo es una medida del grado de diferencia entre las frecuencias observadas y predichas por el modelo de la variable dependiente, de forma que a mayor devianza, peor es el modelo. Su cálculo es -2 veces el logaritmo neperiano de la verosimilitud del modelo. La devianza nos puede orientar durante la etapa de selección del modelo final. Idealmente el modelo final, el mejor modelo, debería tener la menor devianza de los modelos analizados.</li>
</ul>
<ul>
<li style="margin-left: 15px;"><b>PseudoR2</b>=1-(deviance(fit)/sum(<wbr></wbr>(Y-mean(Y))^2))</li>
</ul>
</div>
</div>
<div class="im" style="border-collapse: collapse; color: #500050; font-family: arial, sans-serif; font-size: 13px;">
<blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="gmail_quote">
<div>
<span style="background-color: #ffff66;">Cameron, A.C., Windmeijer, F.A.G., 1996.</span> R^2 measures for count data regression models with applications to health-care utilization. J. Bus. Econom. Statist. 14, 209–220 </div>
</div>
</blockquote>
</div>
<div class="gmail_quote" style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<div>
<ul>
<li style="margin-left: 15px;">Medidas de la función <b>pcrGOF</b> del paquete <a href="http://www.google.es/url?sa=t&rct=j&q=qpcr%20r-project&source=web&cd=1&ved=0CHcQFjAA&url=http%3A%2F%2Fcran.r-project.org%2Fpackage%3DqpcR&ei=YzmxT8T3ItSyhAe0g5n7CA&usg=AFQjCNGSbDcQyH5frjFpGTYb-tScLjf1rw&sig2=PVCFwYvyE93mR5PHAWkPWQ"><b>qpcR</b></a>, para el modelo ajustado nls.</li>
</ul>
</div>
</div>
<blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="gmail_quote">
<div style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<span style="font-family: sans-serif; font-size: 13px;"><code><b>Rsq=1 - (rss/tss)</b>, </code></span>R-square value of a fitted model</div>
<div style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<span style="font-family: sans-serif; font-size: 13px;"><code><b>Rsq.ad=1 - (n - 1)/(n - p) * (1 - rsq)</b>, adjusted </code></span>R-square value of a fitted model</div>
<div style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<span style="font-family: sans-serif; font-size: 13px;"><code><b>AIC</b>, </code></span>Akaike's An Information Criterion</div>
<div style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<span style="font-family: sans-serif; font-size: 13px;"><code><b>AICc</b>, </code></span>Akaike's second-order corrected Information Criterion</div>
<div>
<span style="border-collapse: collapse; color: #222222; font-family: sans-serif; font-size: 13px;"><code><b>BIC</b>,</code></span><span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif; font-size: x-small;"><span class="Apple-style-span" style="border-collapse: collapse;"> Bayesian information criterion or </span></span>Schwarz Criterion</div>
<div style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<span style="font-family: sans-serif; font-size: 13px;"><code><b>HQIC</b>, </code></span>Hannan-Quinn Information Criterion (es una variante del BIC con una pequeña penalización de la magnitud del tamaño muestral).</div>
</div>
</blockquote>
<blockquote style="border-bottom-style: none; border-collapse: collapse; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; color: #222222; font-family: arial, sans-serif; font-size: 13px; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="gmail_quote">
<div>
<span style="font-family: sans-serif; font-size: 13px;"></span><br />
<div align="center">
<span style="font-family: sans-serif; font-size: 13px;"><i>HQIC = -2 \cdot log(\mathcal{L}_{max}) + 2 \cdot k \cdot log(log(n))</i></span></div>
<span style="font-family: sans-serif; font-size: 13px;">
with <i>\mathcal{L}_{max}</i> = maximum likelihood, <i>k</i> = number of parameters and <i>n</i> = number of observations.</span>For n > \sim 20 or so BIC is the strictest in penalizing loss of degree of freedom by having more parameters in the fitted model. For n > \sim 40 AIC is the least strict of the three and HQIC holds the middle ground, or is the least penalizing for n < \sim 20.</div>
<div>
<br /></div>
</div>
</blockquote>
</blockquote>
<blockquote style="border-bottom-style: none; border-collapse: collapse; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; color: #222222; font-family: arial, sans-serif; font-size: 13px; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="gmail_quote">
<div>
<b>resVar</b>, residual variance, </div>
</div>
<div class="gmail_quote">
<b>RMSE</b>, root-mean-squared-error, <span style="font-family: sans-serif; font-size: 13px;"><i>RMSE = √{\overline{(y_i-\hat{y_i})^2}<wbr></wbr>}</i></span></div>
</blockquote>
<blockquote style="border-bottom-style: none; border-collapse: collapse; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; color: #222222; font-family: arial, sans-serif; font-size: 13px; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div class="gmail_quote">
<b>P.square</b>, the <code><a href="http://../qpcR/help/PRESS" style="color: #1155cc;" target="_blank">PRESS</a></code> <i>P^2</i> value (if <code>PRESS = TRUE</code>).</div>
<div class="gmail_quote">
<div class="gmail_quote">
<br /></div>
</div>
</blockquote>
<ul style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<li style="margin-left: 15px;">Medidas de la función<b> gof</b> del paquete <b><a href="http://www.google.es/url?sa=t&rct=j&q=hydrogof%20r-project&source=web&cd=1&ved=0CG4QFjAA&url=http%3A%2F%2Fcran.r-project.org%2Fpackage%3DhydroGOF&ei=iDmxT5iiMNS0hAeQubj2CA&usg=AFQjCNHQn-xqstSBT5c7dFyaeoRORPSAaA&sig2=xj1jcKpZDPXK65hEy5bllA">hydroGOF</a></b>, para la comparación entre las observaciones y las predicciones realizadas por el modelo ajustado nls.</li>
</ul>
<div style="border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">
<table style="font-family: sans-serif; font-size: 13px;" summary="R valueblock"><tbody>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>me</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Mean Error</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>mae</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Mean Absolute Error</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>mse</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Mean Squared Error</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>rmse</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Root Mean Square Error</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>nrmse</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Normalized Root Mean Square Error ( -100% <= nrms <= 100% )</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>PBIAS</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Percent Bias</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>pbiasfdc</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
PBIAS in the slope of the midsegment of the Flow Duration Curve</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>RSR</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Ratio of RMSE to the Standard Deviation of the Observations, RSR = rms / sd(obs). ( 0 <= RSR <= +Inf )</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>rSD</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Ratio of Standard Deviations, rSD = sd(sim) / sd(obs)</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>NSeff</b></code></blockquote>
</td><td style="font-family: arial, sans-serif; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Nash-Sutcliffe Efficiency ( -Inf <= NSeff <= 1 ). </blockquote>
<br />
<blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Una métrica para medir la capacidad de un modelo para predecir los valores observados. Este índice produce resultados menores o iguales a 1, si el resultado es 1 el ajuste es perfecto, si es cero el error es del mismo orden de magnitud que la varianza de los datos observados por lo que la media de los datos observados tendrá una capacidad predictora similar al modelo. Valores inferiores a cero implican que la media tiene una capacidad predictora más alta que el modelo (lo que implica desde luego que el modelo es muy malo). Este índice no es sensible al efecto de los valores proporcionales pero sigue siendo sensible a los valores extremos.<br />
<br />
<span style="font-family: sans-serif; font-size: 13px;"><i>NSE = 1 - ( sum( (obs - sim)^2 ) / sum( (obs - mean(obs))^2 )</i></span><span style="font-family: sans-serif; font-size: 13px;">The Nash-Sutcliffe efficiency (NSE) is a normalized statistic that determines the relative magnitude of the residual variance ("noise") compared to the measured data variance ("information") (<span style="background-color: #ffff66;">Nash and Sutcliffe, 1970</span>). </span> </blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>mNSeff</b></code></blockquote>
</td><td style="font-family: arial, sans-serif; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div style="font-family: sans-serif; font-size: 10pt;">
Modified Nash-Sutcliffe Efficiency. </div>
mNSE = 1 - ( sum( abs(obs - sim)^j ) / sum( abs(obs - mean(obs))^j ).</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>rNSeff</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Relative Nash-Sutcliffe Efficiency.<br />
<i>rNSE = 1 - ( sum( ( (obs - sim)/ mean(obs) )^2 ) / sum( abs( (obs - mean(obs)) / mean(obs) )^2 )</i><br />
<i><br /></i></blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>d</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Index of Agreement ( 0 <= d <= 1 ).<br />
<br />
<div align="center">
<i>d = 1 - [ ( sum( (obs - sim)^2 ) ] / sum( ( abs(sim - mean(obs)) + abs(obs - mean(obs)) )^2 )</i></div>
The Index of Agreement (d) developed by <span style="background-color: #ffff66;">Willmott (1981) </span>as a standardized measure of the degree of model prediction error and varies between 0 and 1.<br />
A value of 1 indicates a perfect match, and 0 indicates no agreement at all (Willmott, 1981).<br />
The index of agreement can detect additive and proportional differences in the observed and simulated means and variances; however, it is overly sensitive to extreme values due to the squared differences (<span style="background-color: #ffff66;">Legates and McCabe, 1999</span>).<br />
<br /></blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>d1</b></code></blockquote>
</td><td style="font-family: arial, sans-serif; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div style="font-family: sans-serif; font-size: 10pt;">
Modified Index of Agreement</div>
<i>mNSeff = 1 - ( sum( abs(obs - sim)^j ) / sum( abs(obs - mean(obs))^j )</i><br />
<br />
When j=1, the modified NSeff is not inflated by the squared values of the differences, because the squares are replaced by absolute values.</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>rd</b></code></blockquote>
</td><td style="font-family: arial, sans-serif; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<div style="font-family: sans-serif; font-size: 10pt;">
Relative Index of Agreement</div>
<i>rd = 1 - [ sum( ( (obs - sim) / obs )^2 ] / sum( ( (abs(sim - mean(obs) ) + abs(obs - mean(obs) ) ) / mean(obs) )^2 )</i><br />
It varies between 0 and 1. A value of 1 indicates a perfect match, and 0 indicates no agreement at all.<br />
<div style="font-family: sans-serif; font-size: 10pt;">
<br /></div>
</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>cp</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Persistence Index ( 0 <= PI <= 1 )<br />
<br />
<div align="center">
<i>cp = 1 - [ sum( (obs[2:n] - sim[2:n])^2 ] / sum( ( obs[2:n] - obs[1:(n-1)] )^2 )</i></div>
Coefficient of persistence (<span style="background-color: #ffff66;">Kitadinis and Bras, 1980; Corradini et al., 1986</span>) is used to compare the model performance against a simple model using the observed value of the previous day as the prediction for the current day.<br />
The coefficient of persistence compare the predictions of the model with the predictions obtained by assuming that the process is a Wiener process (variance increasing linearly with time), in which case, the best estimate for the future is given by the latest measurement (<span style="background-color: #ffff66;">Kitadinis and Bras, 1980</span>).<br />
Persistence model efficiency is a normalized model evaluation statistic that quantifies the relative magnitude of the residual variance (noise) to the variance of the errors obtained by the use of a simple persistence model (<span style="background-color: #ffff66;">Moriasi et al., 2007</span>).<br />
CP ranges from 0 to 1, with CP = 1 being the optimal value and it should be larger than 0.0 to indicate a minimally acceptable model performance.<br />
<br /></blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>r</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Pearson Correlation coefficient ( -1 <= r <= 1 )</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>r.Spearman</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Spearman Correlation coefficient ( -1 <= r.Spearman <= 1 )</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>R2</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Coefficient of Determination ( 0 <= R2 <= 1 ). Gives the proportion of the variance of one variable that is predictable from the other variable</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>bR2</b></code></blockquote>
</td><td style="font-family: sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
R2 multiplied by the coefficient of the regression line between <code>sim</code> and <code>obs</code> ( 0 <= bR2 <= 1 )</blockquote>
</td></tr>
<tr valign="top"><td style="font-family: arial, sans-serif; font-size: 10pt; font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<code><b>KGE</b></code></blockquote>
</td><td style="font: normal normal normal small/normal arial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><blockquote style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: sans-serif; font-size: 10pt; margin-bottom: 0px; margin-left: 40px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
Kling-Gupta efficiency between <code>sim</code> and <code>obs</code> ( 0<=KGE<= 1)<br />
<br />
This goodness-of-fit measure was developed by <span style="background-color: #ffff66;">Gupta et al. (2009)</span> to provide a diagnostically interesting decomposition of the Nash-Sutcliffe efficiency (and hence MSE), which facilitates the analysis of the relative importance of its different components in the context of hydrological modelling".<br />
In the computation of this index, there are three main components involved:<br />
-) <code>r</code> : the Pearson product-moment correlation coefficient. Ideal value is r=1.<br />
-) <code>Alpha</code>: the ratio between the standard deviation of the simulated values and the standard deviation of the observed ones. Ideal value is Alpha=1.<br />
-) <code>Beta</code> : the ratio between the mean of the simulated values and the mean of the observed ones. Ideal value is Beta=1.<br />
<br />
<div align="center">
<i>KGE = 1 - ED</i></div>
<div align="center">
<i>ED = √{ (s[1]*(r-1))^2 +(s[2]*(α-1))^2 + (s[3]*(β-1))^2 }</i></div>
<div align="center">
<i>α=σ_s/σ_o</i></div>
<div align="center">
<i>KGE = 1 - sqrt[ (s[1]*(r-1))^2 + (s[2]*(Alpha-1))^2 + (s[3]*(Beta-1))^2]</i></div>
Kling-Gupta efficiencies range from -Inf to 1. Essentially, the closer to 1, the more accurate the model is. </blockquote>
<div style="font-family: sans-serif; font-size: 10pt;">
<br /></div>
<br />
<br />
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;">Más información: </span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;"><a href="http://www.sciencedirect.com/science/article/pii/S0304407696018180" style="color: #1155cc;" target="_blank">link1</a></span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;"> y </span><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: collapse; color: #222222; font-family: arial, sans-serif; font-size: 13px;"><a href="http://www.jstor.org/discover/10.2307/1392433?uid=3737952&uid=2134&uid=2&uid=70&uid=4&sid=21100796561351" style="color: #1155cc;" target="_blank">link2</a></span><span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif;"><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: collapse;">.</span></span><br />
<br />
<span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif;"><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: collapse;"><br /></span></span></td></tr>
</tbody></table>
</div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R"><h1 itemprop="name" style="border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 9px; padding-left: 9px; padding-right: 9px; padding-top: 9px; vertical-align: baseline;">
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: small; font-weight: normal;">Algunas órdenes para generar gráficos con los valores observados (obs) y predichos (sim) por el modelo ajustado. Se agregan además los valores de las medidas de ajuste detalladas anteriormente.</span></h1>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">
</span></div>
<span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif; font-size: x-small;"></span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">#gráfico de valores observados y valores predichos (o simulados) y medidas de bondad de ajuste</span>
<span style="color: #666666; font-style: italic;">#sea fit.nlsveert el modelo ajustado mediante nls</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span>hydroGOF<span style="color: #009900;">)</span>
sim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/predict"><span style="color: #003399; font-weight: bold;">predict</span></a><span style="color: #009900;">(</span>fit.nlsvert<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
obs=Rt
ggof<span style="color: #009900;">(</span>sim<span style="color: #339933;">,</span>obs<span style="color: #339933;">,</span> digits=<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2jKz861pDQwziln5zkVwSsMhDvvJQNF5wM3xbTO4koCjtyqzNRGk8SNLwnHYXdzlipwnZHGrkeTKyU-Prwlb0q7S3WvbiumyOErlATutNpBzGtdmh05lOm3nS5XuRnO-B9x4wELN4DWU/s1600/Rplot.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="326" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2jKz861pDQwziln5zkVwSsMhDvvJQNF5wM3xbTO4koCjtyqzNRGk8SNLwnHYXdzlipwnZHGrkeTKyU-Prwlb0q7S3WvbiumyOErlATutNpBzGtdmh05lOm3nS5XuRnO-B9x4wELN4DWU/s400/Rplot.jpeg" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<ul>
<li style="text-align: left;"><span class="Apple-style-span" style="color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium; font-weight: bold; line-height: 12px;">ANÁLISIS DE LOS RESIDUOS</span></li>
</ul>
<div style="text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
<span class="Apple-style-span" style="color: #666666; font-family: monospace; white-space: pre;"># ejemplo del paquete </span><span class="Apple-style-span" style="font-family: monospace; white-space: pre;">hydroTSM, para series temporales.</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span class="Apple-style-span" style="font-family: monospace; white-space: pre;">library(hydroTSM)</span></div>
<div class="separator" style="clear: both; text-align: left;">
<span class="Apple-style-span" style="font-family: monospace; white-space: pre;"><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399;">data</span></a></span><span class="Apple-style-span" style="font-family: monospace; white-space: pre;"><span style="color: #009900;">(</span></span><span class="Apple-style-span" style="font-family: monospace; white-space: pre;">OcaEnOnaQts</span><span class="Apple-style-span" style="font-family: monospace; white-space: pre;"><span style="color: #009900;">)</span></span><span class="Apple-style-span" style="font-family: monospace; white-space: pre;"> </span></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="text-align: left;">
<span class="Apple-style-span" style="color: #666666; font-family: monospace; white-space: pre;">## 3 ts, 3 boxplots and 3 histograms</span></div>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; text-align: left;"> hydroplot<span style="color: #009900;">(</span>OcaEnOnaQts<span style="color: #339933;">,</span> FUN=<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399;">mean</span></a><span style="color: #339933;">,</span> ylab= <span style="color: blue;">"Q"</span><span style="color: #339933;">,</span> var.unit = <span style="color: blue;">"m3/s"</span><span style="color: #009900;">)</span></pre>
<br />
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXng4g0qgYpZXgVc2Xt2XqH7RrPc4sPsfFTN6S4-HuCLnRVrt1metbbBN2clmb9XC61HUGxOZswCwmMWQ5o_sOotCwlaOCgWwhfFt3rHyrMGHPUMqFNCmAB5dwOe2DGB1qpqeCZyW8W6I/s1600/Rplot02.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="370" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXng4g0qgYpZXgVc2Xt2XqH7RrPc4sPsfFTN6S4-HuCLnRVrt1metbbBN2clmb9XC61HUGxOZswCwmMWQ5o_sOotCwlaOCgWwhfFt3rHyrMGHPUMqFNCmAB5dwOe2DGB1qpqeCZyW8W6I/s400/Rplot02.jpeg" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R"><ul style="font-family: monospace;">
<li><span class="Apple-style-span" style="color: #444444; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: medium; font-weight: bold; line-height: 12px; white-space: normal;">INTERVALOS DE CONFIANZA Y PREDICCIÓN</span></li>
</ul>
<a href="http://inside-r.org/r-doc/base/library" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/packages/cran/nls2" style="font-family: monospace;">nls2</a><span style="color: #009900; font-family: monospace;">)</span>
fm <- fit.nlsvert
ic=<a href="http://inside-r.org/r-doc/stats/predict" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">predict</span></a><span style="color: #009900; font-family: monospace;">(</span>as.lm<span style="color: #009900; font-family: monospace;">(</span>fm<span style="color: #009900; font-family: monospace;">)</span><span style="color: #339933; font-family: monospace;">,</span> <a href="http://inside-r.org/packages/cran/interval" style="font-family: monospace;">interval</a> = <span style="color: blue; font-family: monospace;">"confidence"</span><span style="color: #009900; font-family: monospace;">)</span>
pr=<a href="http://inside-r.org/r-doc/stats/predict" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">predict</span></a><span style="color: #009900; font-family: monospace;">(</span>as.lm<span style="color: #009900; font-family: monospace;">(</span>fm<span style="color: #009900; font-family: monospace;">)</span><span style="color: #339933; font-family: monospace;">,</span> <a href="http://inside-r.org/packages/cran/interval" style="font-family: monospace;">interval</a> = <span style="color: blue; font-family: monospace;">"prediction"</span><span style="color: #009900; font-family: monospace;">)</span>
<a href="http://inside-r.org/r-doc/graphics/par" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900; font-family: monospace;">(</span>mfrow=<a href="http://inside-r.org/r-doc/base/c" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900; font-family: monospace;">(</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">)</span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#P-factor is the percent of observations that are within the given uncertainty bounds. </span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#R-factor represents the average width of the given uncertainty bounds divided by the standard deviation of the observations. </span>
<span style="color: #666666; font-family: monospace; font-style: italic;">#dates, date.fmt="%Y-%m-%d"</span>
<a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a>=NULL<span style="color: #339933; font-family: monospace;">;</span>for<span style="color: #009900; font-family: monospace;">(</span>i <span style="color: black; font-family: monospace; font-weight: bold;">in</span> <span style="color: #cc66cc; font-family: monospace;">1960</span>:<span style="color: #cc66cc; font-family: monospace;">2009</span><span style="color: #009900; font-family: monospace;">)</span>
<span style="color: #009900; font-family: monospace;">{</span>
datei=<a href="http://inside-r.org/r-doc/base/paste" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900; font-family: monospace;">(</span>i<span style="color: #339933; font-family: monospace;">,</span><span style="color: blue; font-family: monospace;">"-01"</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: blue; font-family: monospace;">"-01"</span><span style="color: #339933; font-family: monospace;">,</span>sep=<span style="color: blue; font-family: monospace;">""</span><span style="color: #009900; font-family: monospace;">)</span>
<a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a>=<a href="http://inside-r.org/r-doc/base/c" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a><span style="color: #339933; font-family: monospace;">,</span>datei<span style="color: #009900; font-family: monospace;">)</span>
<span style="color: #009900; font-family: monospace;">}</span>
plotbands<span style="color: #009900; font-family: monospace;">(</span>x=Rt<span style="color: #009900; font-family: monospace;">[</span>-<span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>lband=ic<span style="color: #009900; font-family: monospace;">[</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>uband=ic<span style="color: #009900; font-family: monospace;">[</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">3</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>sim=sim<span style="color: #009900; font-family: monospace;">[</span>-<span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>main=<span style="color: blue; font-family: monospace;">"Confidence Bounds for x"</span><span style="color: #339933; font-family: monospace;">,</span>dates=<a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a><span style="color: #009900; font-family: monospace;">[</span>-<a href="http://inside-r.org/r-doc/base/length" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #009900; font-family: monospace;">)</span>
plotbands<span style="color: #009900; font-family: monospace;">(</span>x=Rt<span style="color: #009900; font-family: monospace;">[</span>-<span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>lband=pr<span style="color: #009900; font-family: monospace;">[</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">2</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>uband=pr<span style="color: #009900; font-family: monospace;">[</span><span style="color: #339933; font-family: monospace;">,</span><span style="color: #cc66cc; font-family: monospace;">3</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>sim=sim<span style="color: #009900; font-family: monospace;">[</span>-<span style="color: #cc66cc; font-family: monospace;">1</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #339933; font-family: monospace;">,</span>main=<span style="color: blue; font-family: monospace;">"Confidence Bounds for predictions"</span><span style="color: #339933; font-family: monospace;">,</span>dates=<a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a><span style="color: #009900; font-family: monospace;">[</span>-<a href="http://inside-r.org/r-doc/base/length" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900; font-family: monospace;">(</span><a href="http://inside-r.org/r-doc/base/date" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;">date</span></a><span style="color: #009900; font-family: monospace;">)</span><span style="color: #009900; font-family: monospace;">]</span><span style="color: #009900; font-family: monospace;">)</span></pre>
<pre class="r geshifilter-R"><span style="color: #009900; font-family: monospace;">
</span></pre>
<pre class="r geshifilter-R" style="text-align: center;"><span style="color: #009900; font-family: monospace;"><span class="Apple-style-span" style="color: black; font-family: Times; white-space: normal;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEw1vo5ib3XDV-5Gmgo9vnZl3nEDdcf3inCfYnTMEBItyjbDhDcStGbNS5V2MrCKWWQEDPI6LUxDKPcDdnbQrUL7AhP3dxMufuUIhvcXK8WIvtFk7wk_vxkLMJ9LePqHUe6fuPu4IH_z8/s1600/Rplot01.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEw1vo5ib3XDV-5Gmgo9vnZl3nEDdcf3inCfYnTMEBItyjbDhDcStGbNS5V2MrCKWWQEDPI6LUxDKPcDdnbQrUL7AhP3dxMufuUIhvcXK8WIvtFk7wk_vxkLMJ9LePqHUe6fuPu4IH_z8/s640/Rplot01.jpeg" width="412" /></a></span></span></pre>
<pre class="r geshifilter-R" style="text-align: center;"><span style="color: #009900; font-family: monospace;">
</span></pre>
<pre class="r geshifilter-R" style="text-align: center;"><span style="color: #009900; font-family: monospace;">
</span></pre>
<pre class="r geshifilter-R" style="text-align: center;"><span style="color: #009900; font-family: monospace;">
</span></pre>
<pre class="r geshifilter-R" style="text-align: center;"><span style="color: #009900; font-family: monospace;"><span class="Apple-style-span" style="color: black; font-family: 'Times New Roman', serif; white-space: normal;"><h3 style="color: red; font-family: 'Times New Roman', serif; text-align: left;">
In: <a href="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/psuedo_rsquareds.htm">http://www.ats.ucla.edu/stat/mult_pkg/faq/general/psuedo_rsquareds.htm</a></h3>
<h3 style="color: red; font-family: 'Times New Roman', serif; text-align: left;">
FAQ: What are pseudo R-squareds?</h3>
<div style="text-align: left;">
As a starting point, recall that a non-pseudo R-squared is a statistic generated in ordinary least squares (OLS) regression that is often used as a goodness-of-fit measure. In OLS,</div>
<div style="text-align: center;">
<img border="0" height="72" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/Psuedo3.gif" width="177" /></div>
<div style="text-align: left;">
where <i>N</i> is the number of observations in the model, <i>y</i> is the dependent variable, <i>y</i>-bar is the mean of the <i>y</i> values, and <i>y</i>-hat is the value predicted by the model. The numerator of the ratio is the sum of the squared differences between the actual <i>y</i> values and the predicted <i>y</i> values. The denominator of the ratio is the sum of squared differences between the actual <i>y</i> values and their mean. </div>
<div style="text-align: left;">
There are several approaches to thinking about R-squared in OLS. These different approaches lead to various calculations of pseudo R-squareds with regressions of categorical outcome variables. </div>
<ol>
<li><div style="text-align: left;">
<b>R-squared as explained variability </b>- The denominator of the ratio can be thought of as the total variability in the dependent variable, or how much <i>y</i> varies from its mean. The numerator of the ratio can be thought of as the variability in the dependent variable that is not predicted by the model. Thus, this ratio is the proportion of the total variability unexplained by the model. Subtracting this ratio from one results in the proportion of the total variability explained by the model. The more variability explained, the better the model.</div>
</li>
<li><div style="text-align: left;">
<b>R-squared as improvement from null model to fitted model </b>- The denominator of the ratio can be thought of as the sum of squared errors from the null model--a model predicting the dependent variable without any independent variables. In the null model, each <i>y</i> value is predicted to be the mean of the <i>y</i> values. Consider being asked to predict a <i>y</i> value without having any additional information about what you are predicting. The mean of the <i>y</i> values would be your best guess if your aim is to minimize the squared difference between your prediction and the actual <i>y </i>value. The numerator of the ratio would then be the sum of squared errors of the fitted model. The ratio is indicative of the degree to which the model parameters improve upon the prediction of the null model. The smaller this ratio, the greater the improvement and the higher the R-squared.</div>
</li>
<li><div style="text-align: left;">
<b>R-squared as the square of the correlation </b>- The term "R-squared" is derived from this definition. R-squared is the square of the correlation between the model's predicted values and the actual values. This correlation can range from -1 to 1, and so the square of the correlation then ranges from 0 to 1. The greater the magnitude of the correlation between the predicted values and the actual values, the greater the R-squared, regardless of whether the correlation is positive or negative. </div>
</li>
</ol>
<div style="text-align: left;">
When analyzing data with a logistic regression, an equivalent statistic to R-squared does not exist. The model estimates from a logistic regression are maximum likelihood estimates arrived at through an iterative process. They are not calculated to minimize variance, so the OLS approach to goodness-of-fit does not apply. However, to evaluate the goodness-of-fit of logistic models, several pseudo R-squareds have been developed. These are "pseudo" R-squareds because they look like R-squared in the sense that they are on a similar scale, ranging from 0 to 1 (though some pseudo R-squareds never achieve 0 or 1) with higher values indicating better model fit, but they cannot be interpreted as one would interpret an OLS R-squared and different pseudo R-squareds can arrive at very different values. Note that most software packages report the natural logarithm of the likelihood due to floating point precision problems that more commonly arise with raw likelihoods.</div>
<h4 style="color: red; font-family: 'Times New Roman', serif; text-align: left;">
Commonly Encountered Pseudo R-Squareds</h4>
<table border="1" id="table1"><tbody>
<tr><td width="103">Pseudo R-Squared</td><td width="237">Formula</td><td>Description</td></tr>
<tr><td width="103">Efron's</td><td width="237"><div align="center">
<img border="0" height="64" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/Psuedo4.gif" width="187" /></div>
<img border="0" height="22" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/pihat.jpg" width="226" /></td><td><div align="left">
Efron's mirrors approaches 1 and 3 from the list above--the model residuals are squared, summed, and divided by the total variability in the dependent variable, and this R-squared is also equal to the squared correlation between the predicted values and actual values. </div>
<div align="left">
When considering Efron's, remember that model residuals from a logistic regression are not comparable to those in OLS. The dependent variable in a logistic regression is not continuous and the predicted value (a probability) is. In OLS, the predicted values and the actual values are both continuous and on the same scale, so their differences are easily interpreted. </div>
</td></tr>
<tr><td height="96" width="103">McFadden's</td><td height="96" width="237"><div align="center">
<img border="0" height="81" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/McFadden.jpg" width="197" /></div>
<div align="center">
<i>M</i><sub><i>full</i> </sub>= Model with predictors</div>
<div align="center">
<i>M<sub>intercept </sub></i>= Model without predictors</div>
<div align="center">
<img border="0" height="24" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/lhat.jpg" width="171" /></div>
</td><td height="96">McFadden's mirrors approaches 1 and 2 from the list above. The log likelihood of the intercept model is treated as a total sum of squares, and the log likelihood of the full model is treated as the sum of squared errors (like in approach 1). The ratio of the likelihoods suggests the level of improvement over the intercept model offered by the full model (like in approach 2).<br />
A likelihood falls between 0 and 1, so the log of a likelihood is less than or equal to zero. If a model has a very low likelihood, then the log of the likelihood will have a larger magnitude than the log of a more likely model. Thus, a small ratio of log likelihoods indicates that the full model is a far better fit than the intercept model.<br />
If comparing two models on the same data, McFadden's would be higher for the model with the greater likelihood. </td></tr>
<tr><td width="103">McFadden's (adjusted)</td><td width="237"><div align="center">
<img border="0" height="65" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/McFaddenAdj.jpg" width="225" /></div>
<div align="center">
<img border="0" height="24" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/lhat.jpg" width="171" /></div>
</td><td>McFadden's adjusted mirrors the adjusted R-squared in OLS by penalizing a model for including too many predictors. If the predictors in the model are effective, then the penalty will be small relative to the added information of the predictors. However, if a model contains predictors that do not add sufficiently to the model, then the penalty becomes noticeable and the adjusted R-squared can <i>decrease</i> with the addition of a predictor, even if the R-squared increases slightly.<br />
Note that negative McFadden's adjusted R-squared are possible.</td></tr>
<tr><td width="103"><span style="font-family: 'Times New Roman'; font-size: 12pt;">Cox & Snell</span></td><td width="237"><div align="center">
<img border="0" height="77" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/CoxSnell.jpg" width="223" /></div>
</td><td>Cox & Snell's mirrors approach 2 from the list above. The ratio of the likelihoods reflects the improvement of the full model over the intercept model (the smaller the ratio, the greater the improvement).Consider the definition of <i>L(M). L(M)</i> is the conditional probability of the dependent variable given the independent variables. If there are N observations in the dataset, then <i>L(M)</i> is the product of N such probabilities. Thus, taking the n<sup>th</sup>root of the product <i>L(M) </i>provides an estimate of the likelihood of each Y value. Cox & Snell's presents the R-squared as a transformation of the -<i>2ln[L(M<sub>Intercept</sub>)/L(M<sub>Full</sub>)] </i>statistic that is used to determine the convergence of a logistic regression.<br />
Note that Cox & Snell's pseudo R-squared has a maximum value that is not 1: if the full model predicts the outcome perfectly and has a likelihood of 1, Cox & Snell's is then 1-<i>L(M<sub>Intercept</sub>)<sup>2/N</sup>, </i>which is less than one. </td></tr>
<tr><td width="103">Nagelkerke / Cragg & Uhler's</td><td width="237"><div align="center">
<img border="0" height="85" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/CraggUhler.jpg" width="237" /></div>
</td><td>Nagelkerke/Cragg & Uhler's mirrors approach 2 from the list above. It adjusts Cox & Snell's so that the range of possible values extends to 1.To achieve this, the Cox & Snell R-squared is divided by its maximum possible value, 1-<i>L(M<sub>Intercept</sub>)<sup>2/N</sup>. </i>Then, if the full model perfectly predicts the outcome and has a likelihood of 1, Nagelkerke/Cragg & Uhler's R-squared = 1.<br />
When <i>L(M<sub>full</sub>) = 1</i>, then <i>R<sup>2</sup> = 1</i>;
When <i>L(M<sub>full</sub>) = L(M<sub>intercept</sub>)</i>, then <i>R<sup>2</sup> = 0</i>.</td></tr>
<tr><td width="103"><div class="MsoNormal">
McKelvey & Zavoina</div>
</td><td width="237"><div align="center">
<img border="0" height="78" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/McKandZ.jpg" width="207" /></div>
</td><td>McKelvey & Zavoina's mirrors approach 1 from the list above, but its calculations are based on predicting a continuous latent variable underlying the observed 0-1 outcomes in the data. The model predictions of the latent variable can be calculated using the model coefficients (NOT the log-odds) and the predictor variables.McKelvey & Zavoina's also mirrors approach 3. Because of the parallel structure between McKelvey & Zavoina's and OLS R-squareds, we can examine the square root of McKelvey & Zavoina's to arrive at the correlation between the latent continuous variable and the predicted probabilities.<br />
Note that, because <i>y*</i> is not observed, we cannot calculate the variance of the error (the second term in the denominator). It is assumed to be π<sup>2</sup>/3 in logistic models.</td></tr>
<tr><td width="103">Count</td><td width="237"><div align="center">
<img border="0" height="70" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/Count.jpg" width="175" /></div>
</td><td>Count R-Squared does not approach goodness of fit in a way comparable to any OLS approach. It transforms the continuous predicted probabilities into a binary variable on the same scale as the outcome variable (0-1) and then assesses the predictions as correct or incorrect.Count R-Square treats any record with a predicted probability of .5 or greater as having a predicted outcome of 1 and any record with a predicted probability less than .5 as having a predicted outcome of 0. Then, the predicted 1s that match actual 1s and predicted 0s that match actual 0s are tallied. This is the number of records correctly predicted, given this cutoff point of .5. The R-square is this correct count divided by the total count. </td></tr>
<tr><td width="103">Adjusted Count</td><td width="237"><div align="center">
<img border="0" height="50" src="http://www.ats.ucla.edu/stat/mult_pkg/faq/general/CountAdj.jpg" width="132" /></div>
<div align="center">
<i>n</i> = Count of most frequent outcome</div>
</td><td>The Adjusted Count R-Square mirrors approach 2 from the list above. This adjustment is unrelated to the number of predictors and is not comparable to the adjustment to OLS or McFadden's R-Squareds.Consider this scenario: If you are asked to predict who in a list of 100 random people is left-handed or right-handed, you could guess that <i>everyone</i> in the list is right handed and you would be correct for the majority of the list. Your guess could be thought of as a null model.<br />
The Adjusted Count R-Squared controls for such a null model. Without knowing anything about the predictors, one could always predict the more common outcome and be right the majority of the time. An effective model should improve on this null model, and so this null model is the baseline for which the Count R-Square is adjusted. The Adjusted Count R-squared then measures the proportion of correct predictions beyond this baseline. </td></tr>
</tbody></table>
</span></span></pre>
<pre class="r geshifilter-R" style="text-align: center;"><span style="color: #009900; font-family: monospace;">
</span></pre>
<pre class="r geshifilter-R" style="text-align: left;"><span style="color: #009900; font-family: monospace;">Espero que les haya interesado, iré agregando más material próximamente!.</span></pre>
</div>
</div>
</div>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com2tag:blogger.com,1999:blog-6841022238556785612.post-17425869699827994232012-05-14T02:50:00.002-07:002012-05-14T15:14:20.628-07:00Tablas exportables a Latex o Lyx desde R<br />
Mediante el paquete "xtable" podemos convertir algunas salidas de R a formato Latex (aplicable a Lyx), y así facilitarnos la redacción de nuestros trabajos.<br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #003399; font-weight: bold;"><a href="http://inside-r.org/r-doc/utils/install.packages">install.packages</a></span><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#instalamos el paquete</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><span style="color: blue;">"stable"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#lo activates o cargamos</span></pre>
</div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"> <a href="http://inside-r.org/r-doc/utils/methods"><span style="color: #003399; font-weight: bold;">methods</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/xtable">xtable</a><span style="color: #009900;">)</span></pre>
</div>
</div>
<br /></div>
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> [1] xtable.anova* xtable.aov* xtable.aovlist* xtable.coxph* <br /> [5] xtable.data.frame* xtable.glm* xtable.lm* xtable.matrix* <br /> [9] xtable.prcomp* xtable.summary.aov* xtable.summary.aovlist* xtable.summary.glm* <br />[13] xtable.summary.lm* xtable.summary.prcomp* xtable.table* xtable.ts* <br />[17] xtable.zoo* <br /><br /> Non-visible functions are asterisked</span><span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif; font-size: x-small;"><span class="Apple-style-span" style="line-height: 16px;"><br /></span></span><br />
<span class="Apple-style-span" style="color: #222222; font-family: arial, sans-serif; font-size: x-small;"><span class="Apple-style-span" style="line-height: 16px;"><br /></span></span><br />
Si queremos utilizar el paquete con objetos de nls, basta con aplicar el siguiente script.<br />
<br />
<b>## Script para crear tablas a partir de objetos nls</b><br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;">xtable.nls= <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.summary.nls<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.summary.nls= <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
x = <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>x$coef<span style="color: #339933;">,</span>check.names=F<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #339933;">,</span><span style="color: blue;">"data.frame"</span><span style="color: #009900;">)</span>
caption<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> = caption
label<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> = label
align<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> = <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>align<span style="color: #009900;">)</span><span style="color: #339933;">,</span>align<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
digits<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> = <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>digits<span style="color: #009900;">)</span><span style="color: #339933;">,</span>digits<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
display<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> = <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>display<span style="color: #009900;">)</span><span style="color: #339933;">,</span>display<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"s"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<br />
<br />
<b># ejemplo de aplicación</b><br />
<span class="Apple-style-span" style="font-family: sans-serif; font-size: 13px;"></span><br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics"><span style="color: #006600; font-weight: bold;">graphics</span></a><span style="color: #009900;">)</span>
DNase1=<a href="http://inside-r.org/r-doc/base/subset"><span style="color: #003399; font-weight: bold;">subset</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/DNase"><span style="color: #003399; font-weight: bold;">DNase</span></a><span style="color: #339933;">,</span> Run == <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
fm3DNase1=<a href="http://inside-r.org/r-doc/stats/nls"><span style="color: #003399; font-weight: bold;">nls</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/density"><span style="color: #003399; font-weight: bold;">density</span></a> ~ Asym/<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span> + <a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>xmid - <a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span>conc<span style="color: #009900;">)</span><span style="color: #009900;">)</span>/scal<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a> = DNase1<span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/stats/start"><span style="color: #003399; font-weight: bold;">start</span></a> = <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>Asym = <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> xmid = <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> scal = <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>fm3DNase1<span style="color: #009900;">)</span></pre>
</div>
</div>
<br />
<pre>Formula: density ~ Asym/(1 + exp((xmid - log(conc))/scal))
Parameters:
Estimate Std. Error t value Pr(>|t|)
Asym 2.34518 0.07815 30.01 2.17e-13 ***
xmid 1.48309 0.08135 18.23 1.22e-10 ***
scal 1.04145 0.03227 32.27 8.51e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.01919 on 13 degrees of freedom
Number of iterations to convergence: 6
Achieved convergence tolerance: 2.026e-06
</pre>
<pre></pre>
<pre><span class="Apple-style-span" style="font-family: Times; white-space: normal;"><pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><a href="http://inside-r.org/packages/cran/xtable">xtable</a><span style="color: #009900;">(</span>fm3DNase1<span style="color: #009900;">)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span style="color: #009900;">
</span></pre>
</span></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre>% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Mon May 14 11:21:04 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
Asym & 2.3452 & 0.0782 & 30.01 & 0.0000 \\
xmid & 1.4831 & 0.0814 & 18.23 & 0.0000 \\
scal & 1.0415 & 0.0323 & 32.27 & 0.0000 \\
\hline
\end{tabular}
\end{center}</pre>
<pre>\end{table}</pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
Yo uso <a href="http://www.lyx.org/">Lyx</a>. En este programa basta con abrir un archivo nuevo, seleccionar "Insertar" en la barra de herramientas, luego "Código TEX" y pegamos la salida de R.<br />
El resultado es el siguiente:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAVVNT6hX2NZaq3XGz-hEO-CSytRWezSi6HZxVWaeBUevrCJle2-SQN_E6_kPwAMUzqMVkLFQZehYg3Fs3GW1NYV99oTBNyy3fSTXOoc31zTOg9zyLGFaw_yGMBD-0oiSw9U1yMX-hO_Y/s1600/Sin+ti%CC%81tulo.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="112" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAVVNT6hX2NZaq3XGz-hEO-CSytRWezSi6HZxVWaeBUevrCJle2-SQN_E6_kPwAMUzqMVkLFQZehYg3Fs3GW1NYV99oTBNyy3fSTXOoc31zTOg9zyLGFaw_yGMBD-0oiSw9U1yMX-hO_Y/s400/Sin+ti%CC%81tulo.jpg" width="400" /></a></div>
<pre style="text-align: center;">
</pre>
<br />
<br />
También existen otros paquetes similares que te pueden interesar:<br />
<br />
<ul>
<li><span class="Apple-style-span" style="font-family: Arial;"><span style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; border-collapse: separate; color: black; font-family: Times; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; text-align: left;"><span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: small; line-height: normal;"><b><a href="http://journal.r-project.org/2009-1/RJournal_2009-1_Pau+Huber.pdf">hwriter</a></b></span><span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: small; line-height: normal;"> </span><span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: small; line-height: normal;">(Gregoire Pau, 2010</span><span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: small; line-height: normal;">)</span></span></span></span></li>
<li><span class="Apple-style-span" style="font-family: Arial;"><span style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; border-collapse: separate; color: black; font-family: Times; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; text-align: left;"><span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: Times; font-size: small; line-height: normal;"><b>R2HTML</b> (Eric Lecoutre, 2010)</span></span></span></span></li>
<li><span style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="text-align: left;"><a href="http://cran.r-project.org/web/packages/memisc/index.html" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; cursor: pointer; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;"><span class="Apple-style-span" style="color: #0077cc; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 14px; line-height: 18px;">memisc</span></span></a><span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span class="Apple-style-span" style="border-collapse: collapse; font-size: 14px; line-height: 18px;"> : tool for management of survey data, contains some tools for latex tables of (basic) regression model estimates.</span></span></span></span><span class="Apple-style-span" style="font-family: Arial;"><br /></span></li>
<li><span style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; border-collapse: separate; color: black; font-family: Times; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; text-align: left;"><a href="http://cran.r-project.org/web/packages/Hmisc/index.html" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">Hmisc</a> contains a function <code style="background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">latex()</code> that creates a tex file containing the object of choice. It is pretty flexible, and can also output <code style="background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">longtable</code> latex tables. There's a lot of info in the help file <code style="background-attachment: initial; background-clip: initial; background-color: #eeeeee; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">?latex</code></span></span></li>
<li><span style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; border-collapse: separate; color: black; font-family: Times; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px; text-align: left;"><a href="http://cran.r-project.org/web/packages/miscFuncs/index.html" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">miscFuncs</a> has a neat function 'latextable' that converts matrix data with mixed alphabetic and numeric entries into a LaTeX table and prints them to the console, so they can be copied and pasted into a LaTeX document.</span></span></li>
<li><span style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; border-collapse: separate; color: black; font-family: Times; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><a href="http://cran.r-project.org/package=apsrtable" rel="nofollow" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">apsrtable</a> which <em style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; font-style: italic; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">formats latex tables from one or more model objects</em></span></li>
<li><a href="http://cran.r-project.org/package=r2lh" rel="nofollow" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">p2lh</a> which exports <em style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; font-style: italic; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">R to LaTeX and HTML</em></li>
<li><a href="http://cran.r-project.org/package=RcmdrPlugin.Export" rel="nofollow" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">RcmdrPlugin.Export</a> which <em style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; font-style: italic; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">graphically exports output to LaTeX or HTML</em></li>
<li><a href="http://cran.r-project.org/package=reporttools" rel="nofollow" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">reporttools</a> which <em style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; font-style: italic; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">generates LaTeX tables of descriptive statistics</em></li>
<li><span class="Apple-style-span" style="border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 18px;"><a href="http://cran.r-project.org/web/packages/xtable/index.html" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">xtable</a> : for standard tables of most simple objects. A nice gallery with examples can be found <a href="http://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf" style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; color: #0077cc; cursor: pointer; font-size: 14px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-decoration: none; vertical-align: baseline;">here</a>.</span></li>
</ul>
<br />
<br />
<br />
Para ver más de generación de informes con R y Latex, puedes mirarte estos archivos:<br />
<br />
<ul>
<li>How to use xtable() with a data.frame which contains special LATEX characters J.R. Lobry. 2007. <a href="http://pbil.univ-lyon1.fr/R_old/querep/qrn.pdf">http://pbil.univ-lyon1.fr/R_old/querep/qrn.pdf</a></li>
<li>Generación automática de informes con Sweave y LATEX. Francesc Carmona Departamento de Estadística Universidad de Barcelona. 2012. <a href="http://www.ub.edu/stat/docencia/EADB/Manual_Sweave.pdf">http://www.ub.edu/stat/docencia/EADB/Manual_Sweave.pdf</a></li>
<li>An Sweave Demo Charles J. Geyer. 2010. <a href="http://users.stat.umn.edu/~geyer/Sweave/foo.pdf">http://users.stat.umn.edu/~geyer/Sweave/foo.pdf</a></li>
</ul>
<div>
<br /></div>
<div>
Para los curiosos. Para todo aquel que quiera manipular las funciones del paquete <b>xtable</b>. Aquí van los scripts (<a href="https://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/R/xtable.R?root=xtable">link</a>).<br />
<br /></div>
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">### xtable package</span>
<span style="color: #666666; font-style: italic;">###</span>
<span style="color: #666666; font-style: italic;">### Produce LaTeX and HTML tables from R objects.</span>
<span style="color: #666666; font-style: italic;">###</span>
<span style="color: #666666; font-style: italic;">### Copyright 2000-2007 David B. Dahl <dahl@stat.tamu.edu></span>
<span style="color: #666666; font-style: italic;">###</span>
<span style="color: #666666; font-style: italic;">### This file is part of the `xtable' library for R and related languages.</span>
<span style="color: #666666; font-style: italic;">### It is made available under the terms of the GNU General Public</span>
<span style="color: #666666; font-style: italic;">### License, version 2, or at your option, any later version,</span>
<span style="color: #666666; font-style: italic;">### incorporated herein by reference.</span>
<span style="color: #666666; font-style: italic;">###</span>
<span style="color: #666666; font-style: italic;">### This program is distributed in the hope that it will be</span>
<span style="color: #666666; font-style: italic;">### useful, but WITHOUT ANY WARRANTY; without even the implied</span>
<span style="color: #666666; font-style: italic;">### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR</span>
<span style="color: #666666; font-style: italic;">### PURPOSE. See the GNU General Public License for more</span>
<span style="color: #666666; font-style: italic;">### details.</span>
<span style="color: #666666; font-style: italic;">###</span>
<span style="color: #666666; font-style: italic;">### You should have received a copy of the GNU General Public</span>
<span style="color: #666666; font-style: italic;">### License along with this program; if not, write to the Free</span>
<span style="color: #666666; font-style: italic;">### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,</span>
<span style="color: #666666; font-style: italic;">### MA 02111-1307, USA</span>
<a href="http://inside-r.org/packages/cran/xtable">xtable</a> <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/UseMethod"><span style="color: #003399; font-weight: bold;">UseMethod</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## data.frame and matrix objects</span>
xtable.data.frame <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
logicals <- <a href="http://inside-r.org/r-doc/base/unlist"><span style="color: #003399; font-weight: bold;">unlist</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.logical"><span style="color: #003399; font-weight: bold;">is.logical</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><span style="color: #339933;">,</span>logicals<span style="color: #009900;">]</span> <- <a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">[</span><span style="color: #339933;">,</span>logicals<span style="color: #009900;">]</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/as.character"><span style="color: #003399; font-weight: bold;">as.character</span></a><span style="color: #009900;">)</span>
characters <- <a href="http://inside-r.org/r-doc/base/unlist"><span style="color: #003399; font-weight: bold;">unlist</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.character"><span style="color: #003399; font-weight: bold;">is.character</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
factors <- <a href="http://inside-r.org/r-doc/base/unlist"><span style="color: #003399; font-weight: bold;">unlist</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.factor"><span style="color: #003399; font-weight: bold;">is.factor</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
ints <- <a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/is.integer"><span style="color: #003399; font-weight: bold;">is.integer</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #339933;">,</span><span style="color: blue;">"data.frame"</span><span style="color: #009900;">)</span>
caption<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- caption
label<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- label
align<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>align<span style="color: #009900;">)</span><span style="color: #339933;">,</span> align<span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"l"</span><span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #009900;">(</span>characters|factors<span style="color: #009900;">)</span>+<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
digits<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>digits<span style="color: #009900;">)</span><span style="color: #339933;">,</span>digits<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Patch from Seth Falcon <sfalcon@fhcrc.org>, 18-May-2007</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>display<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
display <- <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
display<span style="color: #009900;">[</span>ints<span style="color: #009900;">]</span> <- <span style="color: blue;">"d"</span>
display<span style="color: #009900;">[</span>characters | factors<span style="color: #009900;">]</span> <- <span style="color: blue;">"s"</span>
display <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"s"</span><span style="color: #339933;">,</span> display<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
display<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- display
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.matrix <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.data.frame<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>check.names=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>align=align<span style="color: #339933;">,</span>
digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## table objects (of 1 or 2 dimensions) by Guido Gay, 9 Feb 2007</span>
<span style="color: #666666; font-style: italic;">## Fixed to pass R checks by DBD, 9 May 2007</span>
xtable.table<-<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>==<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.matrix<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a>=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rownames"><span style="color: #003399; font-weight: bold;">rownames</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>align=align<span style="color: #339933;">,</span>digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span> <span style="color: black; font-weight: bold;">else</span> <span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>==<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.matrix<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a>=<a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a>=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rownames"><span style="color: #003399; font-weight: bold;">rownames</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/colnames"><span style="color: #003399; font-weight: bold;">colnames</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>align=align<span style="color: #339933;">,</span>digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span> <span style="color: black; font-weight: bold;">else</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/stop"><span style="color: #003399; font-weight: bold;">stop</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable.table is not implemented for tables of > 2 dimensions"</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## anova objects</span>
xtable.anova <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
suggested.digits <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
suggested.digits<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/grep"><span style="color: #003399; font-weight: bold;">grep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Pr<span style="color: #000099; font-weight: bold;">\\</span>(>"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>+<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <- <span style="color: #cc66cc;">4</span>
suggested.digits<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/grep"><span style="color: #003399; font-weight: bold;">grep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"P<span style="color: #000099; font-weight: bold;">\\</span>(>"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>+<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <- <span style="color: #cc66cc;">4</span>
suggested.digits<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/grep"><span style="color: #003399; font-weight: bold;">grep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Df"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>+<span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <- <span style="color: #cc66cc;">0</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #339933;">,</span><span style="color: blue;">"data.frame"</span><span style="color: #009900;">)</span>
caption<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- caption
label<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- label
align<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>align<span style="color: #009900;">)</span><span style="color: #339933;">,</span>align<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"l"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
digits<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>digits<span style="color: #009900;">)</span><span style="color: #339933;">,</span>digits<span style="color: #339933;">,</span>suggested.digits<span style="color: #009900;">)</span>
display<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>display<span style="color: #009900;">)</span><span style="color: #339933;">,</span>display<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"s"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## aov objects</span>
xtable.aov <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.anova<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/anova"><span style="color: #003399; font-weight: bold;">anova</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>...<span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.summary.aov <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.anova<span style="color: #009900;">(</span>x<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.summary.aovlist <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span>i==<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> result <- xtable.summary.aov<span style="color: #009900;">(</span>x<span style="color: #009900;">[</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">else</span> result <- <a href="http://inside-r.org/r-doc/base/rbind"><span style="color: #003399; font-weight: bold;">rbind</span></a><span style="color: #009900;">(</span>result<span style="color: #339933;">,</span>xtable.anova<span style="color: #009900;">(</span>x<span style="color: #009900;">[</span><span style="color: #009900;">[</span>i<span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>
label=label<span style="color: #339933;">,</span> align=align<span style="color: #339933;">,</span>
digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>result<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.aovlist <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.summary.aovlist<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## lm objects</span>
xtable.lm <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.summary.lm<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.summary.lm <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
x <- <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>x$coef<span style="color: #339933;">,</span>check.names=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #339933;">,</span><span style="color: blue;">"data.frame"</span><span style="color: #009900;">)</span>
caption<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- caption
label<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- label
align<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>align<span style="color: #009900;">)</span><span style="color: #339933;">,</span>align<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><span style="color: blue;">"r"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
digits<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>digits<span style="color: #009900;">)</span><span style="color: #339933;">,</span>digits<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
display<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>display<span style="color: #009900;">)</span><span style="color: #339933;">,</span>display<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"s"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"f"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## glm objects</span>
xtable.glm <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.summary.glm<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>align=align<span style="color: #339933;">,</span>
digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.summary.glm <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>xtable.summary.lm<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=caption<span style="color: #339933;">,</span>label=label<span style="color: #339933;">,</span>
align=align<span style="color: #339933;">,</span> digits=digits<span style="color: #339933;">,</span>display=display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">## prcomp objects</span>
xtable.prcomp <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
x <- <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>x$rotation<span style="color: #339933;">,</span>check.names=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #339933;">,</span><span style="color: blue;">"data.frame"</span><span style="color: #009900;">)</span>
caption<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- caption
label<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- label
align<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>align<span style="color: #009900;">)</span><span style="color: #339933;">,</span>align<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
digits<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>digits<span style="color: #009900;">)</span><span style="color: #339933;">,</span>digits<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
display<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>display<span style="color: #009900;">)</span><span style="color: #339933;">,</span>display<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"s"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
xtable.summary.prcomp <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
x <- <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>x$importance<span style="color: #339933;">,</span>check.names=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"xtable"</span><span style="color: #339933;">,</span><span style="color: blue;">"data.frame"</span><span style="color: #009900;">)</span>
caption<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- caption
label<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- label
align<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>align<span style="color: #009900;">)</span><span style="color: #339933;">,</span>align<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"r"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
digits<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>digits<span style="color: #009900;">)</span><span style="color: #339933;">,</span>digits<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
display<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>+<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>display<span style="color: #009900;">)</span><span style="color: #339933;">,</span>display<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"s"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># Slightly modified version of xtable.coxph contributed on r-help by</span>
<span style="color: #666666; font-style: italic;"># Date: Wed, 2 Oct 2002 17:47:56 -0500 (CDT)</span>
<span style="color: #666666; font-style: italic;"># From: Jun Yan <jyan@stat.wisc.edu></span>
<span style="color: #666666; font-style: italic;"># Subject: Re: [R] xtable for Cox model output</span>
xtable.coxph <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a> <span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
cox <- x
<a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a> <- cox$coef
se <- <a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/diag"><span style="color: #003399; font-weight: bold;">diag</span></a><span style="color: #009900;">(</span>cox$var<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span>cox$naive.var<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
tmp <- <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span> se<span style="color: #339933;">,</span> beta/se<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> - <a href="http://inside-r.org/r-doc/stats/pchisq"><span style="color: #003399; font-weight: bold;">pchisq</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>beta/se<span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a><span style="color: #009900;">(</span>tmp<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"coef"</span><span style="color: #339933;">,</span> <span style="color: blue;">"exp(coef)"</span><span style="color: #339933;">,</span> <span style="color: blue;">"se(coef)"</span><span style="color: #339933;">,</span> <span style="color: blue;">"z"</span><span style="color: #339933;">,</span> <span style="color: blue;">"p"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: black; font-weight: bold;">else</span> <span style="color: #009900;">{</span>
tmp <- <a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span> <a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span> se<span style="color: #339933;">,</span> beta/se<span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/signif"><span style="color: #003399; font-weight: bold;">signif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span> - <a href="http://inside-r.org/r-doc/stats/pchisq"><span style="color: #003399; font-weight: bold;">pchisq</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>beta/se<span style="color: #009900;">)</span>^<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> digits - <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a><span style="color: #009900;">(</span>tmp<span style="color: #009900;">)</span> <- <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/beta"><span style="color: #003399; font-weight: bold;">beta</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"coef"</span><span style="color: #339933;">,</span> <span style="color: blue;">"exp(coef)"</span><span style="color: #339933;">,</span> <span style="color: blue;">"robust se"</span><span style="color: #339933;">,</span> <span style="color: blue;">"z"</span><span style="color: #339933;">,</span> <span style="color: blue;">"p"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/xtable">xtable</a><span style="color: #009900;">(</span>tmp<span style="color: #339933;">,</span> caption = caption<span style="color: #339933;">,</span> label = label<span style="color: #339933;">,</span> align = align<span style="color: #339933;">,</span>
digits = digits<span style="color: #339933;">,</span> display = display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># Additional method: xtable.ts</span>
<span style="color: #666666; font-style: italic;"># Contributed by David Mitchell (davidm@netspeed.com.au)</span>
<span style="color: #666666; font-style: italic;"># Date: July 2003</span>
xtable.ts <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>caption=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>label=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>align=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
digits=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>display=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/inherits"><span style="color: #003399; font-weight: bold;">inherits</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <span style="color: blue;">"ts"</span><span style="color: #009900;">)</span> && !<a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<span style="color: #666666; font-style: italic;"># COLNAMES <- paste(colnames(x));</span>
tp.1 <- <a href="http://inside-r.org/r-doc/base/trunc"><span style="color: #003399; font-weight: bold;">trunc</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/time"><span style="color: #003399; font-weight: bold;">time</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
tp.2 <- <a href="http://inside-r.org/r-doc/base/trunc"><span style="color: #003399; font-weight: bold;">trunc</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/cycle"><span style="color: #003399; font-weight: bold;">cycle</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
day.abb <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Sun"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Mon"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Tue"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Wed"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Thu"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Fri"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Sat"</span><span style="color: #009900;">)</span>
ROWNAMES <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/frequency"><span style="color: #003399; font-weight: bold;">frequency</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>
tp.1<span style="color: #339933;">,</span>
<span style="color: blue;">"Arg2"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg3"</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">## Dummy arguments</span>
<a href="http://inside-r.org/r-doc/base/paste"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900;">(</span>tp.1<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Q1"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Q2"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Q3"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Q4"</span><span style="color: #009900;">)</span><span style="color: #009900;">[</span>tp.2<span style="color: #009900;">]</span><span style="color: #339933;">,</span> sep=<span style="color: blue;">" "</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<span style="color: blue;">"Arg5"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg6"</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/paste"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Wk."</span><span style="color: #339933;">,</span> tp.1<span style="color: #339933;">,</span> <span style="color: blue;">" "</span><span style="color: #339933;">,</span> day.abb<span style="color: #009900;">[</span>tp.2<span style="color: #009900;">]</span><span style="color: #339933;">,</span> sep=<span style="color: blue;">""</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<span style="color: blue;">"Arg8"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg9"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg10"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg11"</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/paste"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900;">(</span>tp.1<span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">month.abb</span><span style="color: #009900;">[</span>tp.2<span style="color: #009900;">]</span><span style="color: #339933;">,</span> sep=<span style="color: blue;">" "</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
tmp <- <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a>=ROWNAMES<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: black; font-weight: bold;">else</span> <span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/inherits"><span style="color: #003399; font-weight: bold;">inherits</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <span style="color: blue;">"ts"</span><span style="color: #009900;">)</span> && <a href="http://inside-r.org/r-doc/base/is.null"><span style="color: #003399; font-weight: bold;">is.null</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
COLNAMES <- <a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/frequency"><span style="color: #003399; font-weight: bold;">frequency</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<span style="color: blue;">"Value"</span><span style="color: #339933;">,</span>
<span style="color: blue;">"Arg2"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg3"</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">## Dummy arguments</span>
<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Q1"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Q2"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Q3"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Q4"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<span style="color: blue;">"Arg5"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg6"</span><span style="color: #339933;">,</span>
day.abb<span style="color: #339933;">,</span>
<span style="color: blue;">"Arg8"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg9"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg10"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Arg11"</span><span style="color: #339933;">,</span>
<span style="color: black; font-weight: bold;">month.abb</span><span style="color: #009900;">)</span>
ROWNAMES <- <a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span>from=<a href="http://inside-r.org/r-doc/stats/start"><span style="color: #003399; font-weight: bold;">start</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> to=<a href="http://inside-r.org/r-doc/stats/end"><span style="color: #003399; font-weight: bold;">end</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
tmp <- <a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/start"><span style="color: #003399; font-weight: bold;">start</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> - <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> x<span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/frequency"><span style="color: #003399; font-weight: bold;">frequency</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> - <a href="http://inside-r.org/r-doc/stats/end"><span style="color: #003399; font-weight: bold;">end</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<a href="http://inside-r.org/r-doc/stats/frequency"><span style="color: #003399; font-weight: bold;">frequency</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> byrow=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/row.names"><span style="color: #003399; font-weight: bold;">row.names</span></a>=ROWNAMES<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>tmp<span style="color: #009900;">)</span> <- COLNAMES
<span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/xtable">xtable</a><span style="color: #009900;">(</span>tmp<span style="color: #339933;">,</span> caption = caption<span style="color: #339933;">,</span> label = label<span style="color: #339933;">,</span> align = align<span style="color: #339933;">,</span>
digits = digits<span style="color: #339933;">,</span> display = display<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;"># Suggested by Ajay Narottam Shah <ajayshah@mayin.org> in e-mail 2006/07/22</span>
xtable.zoo <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>...<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/xtable">xtable</a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/as.ts"><span style="color: #003399; font-weight: bold;">as.ts</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span>...<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a><br />
<div>
<br /></div>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-24614813515547634772011-09-29T07:12:00.000-07:002011-09-29T07:42:45.208-07:00Estadística descriptiva (Clase 2)<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Análisis de datos con R #</span>
<span style="color: #666666; font-style: italic;"># 2010/2011 #</span>
<span style="color: #666666; font-style: italic;"># Msc. Rosana Ferrero #</span>
<span style="color: #666666; font-style: italic;"># http://statisticalecology.blogspot.com/ #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Clase 2. Estadística descriptiva</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 0. Funciones de interés para manejar los datos #</span>
<span style="color: #666666; font-style: italic;"># 1. Estadística descriptiva #</span>
<span style="color: #666666; font-style: italic;"># a. Tablas de Frecuencia #</span>
<span style="color: #666666; font-style: italic;"># b. Gráficos #</span>
<span style="color: #666666; font-style: italic;"># c. Estadísticos, medidas o estimadores #</span>
<span style="color: #666666; font-style: italic;"># 2. Ejemplo: datos Iris #</span>
<span style="color: #666666; font-style: italic;"># 3. Otras funciones para el manejo de datos #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 0. Funciones de interés para manejar los datos #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">########## Leer los datos</span>
<span style="color: #666666; font-style: italic;">### La función "scan"</span>
x<-<a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #cc66cc;">1</span>: <span style="color: #cc66cc;">6</span>
<span style="color: #cc66cc;">2</span>: <span style="color: #cc66cc;">7</span>
<span style="color: #cc66cc;">3</span>: <span style="color: #cc66cc;">3</span>
<span style="color: #cc66cc;">4</span>: <span style="color: #cc66cc;">4</span>
<span style="color: #cc66cc;">5</span>: <span style="color: #cc66cc;">8</span>
<span style="color: #cc66cc;">6</span>: <span style="color: #cc66cc;">5</span>
<span style="color: #cc66cc;">7</span>: <span style="color: #cc66cc;">6</span>
<span style="color: #cc66cc;">8</span>: <span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">9</span>:
<span style="color: #666666; font-style: italic;">### La función "read.table". Producen un dataframe</span>
data<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>regression.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
map<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>bowens.csv"</span><span style="color: #339933;">,</span>header=T<span style="color: #339933;">,</span>sep=<span style="color: blue;">","</span><span style="color: #009900;">)</span>
data<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/file.choose"><span style="color: #003399; font-weight: bold;">file.choose</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## opción seq=""</span>
<span style="color: #666666; font-style: italic;">#\n newline</span>
<span style="color: #666666; font-style: italic;">#\r carriage return</span>
<span style="color: #666666; font-style: italic;">#\t tab character</span>
<span style="color: #666666; font-style: italic;">#\b backspace</span>
<span style="color: #666666; font-style: italic;">#\a bell</span>
<span style="color: #666666; font-style: italic;">#\f form feed</span>
<span style="color: #666666; font-style: italic;">#\v vertical tab</span>
<span style="color: #666666; font-style: italic;">## chequear un archivo</span>
<a href="http://inside-r.org/r-doc/base/file.exists"><span style="color: #003399; font-weight: bold;">file.exists</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>Decay.txt"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## leer datos desde archivos con formato no estándar. Producen un objeto lista no un dataframe</span>
murders<-<a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>murders.txt"</span><span style="color: #339933;">,</span> skip=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> what=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><span style="color: blue;">""</span><span style="color: #339933;">,</span><span style="color: blue;">""</span><span style="color: #339933;">,</span><span style="color: blue;">""</span><span style="color: #339933;">,</span><span style="color: blue;">""</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
murder.frame<-<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span>murders<span style="color: #009900;">)</span>
murders<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>murders.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## leer archivos con diferentes números/valores por línea</span>
line.number<-<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>rt.txt"</span><span style="color: #339933;">,</span>sep=<span style="color: blue;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">(</span>my.list<-<a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span>:<span style="color: #009900;">(</span>line.number-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/scan"><span style="color: #003399; font-weight: bold;">scan</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>rt.txt"</span><span style="color: #339933;">,</span>skip=x<span style="color: #339933;">,</span>nlines=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>quiet=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## leer líneas. Produce un objeto de clase character.</span>
<a href="http://inside-r.org/r-doc/base/readLines"><span style="color: #003399; font-weight: bold;">readLines</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>murders.txt"</span><span style="color: #339933;">,</span>n=-<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">######## Ver ítem 4 para obtener más funciones de interés </span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 1.a. Tablas de frecuencias #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># realiza tablas de frecuencias absolutas</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>/sum<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#realiza tablas de frecuencias relativas</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#para variables numéricas si queremos hacer una tabla de frecuencias absolutas agrupando los datos por intervalos</span>
<a href="http://inside-r.org/r-doc/base/margin.table"><span style="color: #003399; font-weight: bold;">margin.table</span></a><span style="color: #009900;">(</span>tabla<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> o <span style="color: #cc66cc;">2</span> o ...<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#realiza tablas marginales del objeto tabla</span>
<a href="http://inside-r.org/r-doc/base/prop.table"><span style="color: #003399; font-weight: bold;">prop.table</span></a><span style="color: #009900;">(</span>tabla<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> o <span style="color: #cc66cc;">2</span> o ...<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#tabla de frecuencias condicionadas por la dimensión indicada con 1 o 2 o... del objeto tabla</span>
<a href="http://inside-r.org/r-doc/stats/ftable"><span style="color: #003399; font-weight: bold;">ftable</span></a><span style="color: #009900;">(</span>tabla<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># tabla de frecuencias para 3 o más variables categóricas</span>
m <- <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> m
<a href="http://inside-r.org/r-doc/base/margin.table"><span style="color: #003399; font-weight: bold;">margin.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># totales por filas</span>
<a href="http://inside-r.org/r-doc/base/margin.table"><span style="color: #003399; font-weight: bold;">margin.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># totales por columnas</span>
<a href="http://inside-r.org/r-doc/base/prop.table"><span style="color: #003399; font-weight: bold;">prop.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># porcentaje por filas</span>
<a href="http://inside-r.org/r-doc/base/prop.table"><span style="color: #003399; font-weight: bold;">prop.table</span></a><span style="color: #009900;">(</span>m<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># porcentaje por columnas</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a>
<a href="http://inside-r.org/r-doc/stats/ftable"><span style="color: #003399; font-weight: bold;">ftable</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># crea tablas de contingencia llanas; x= factores o lista o tabla</span>
<a href="http://inside-r.org/r-doc/stats/ftable"><span style="color: #003399; font-weight: bold;">ftable</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/Titanic"><span style="color: #003399; font-weight: bold;">Titanic</span></a><span style="color: #339933;">,</span> row.vars = <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> col.vars = <span style="color: blue;">"Survived"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/tabulate"><span style="color: #003399; font-weight: bold;">tabulate</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> nbins = <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># cuenta el número de veces que ocurre cada valor en un vector dado </span>
<span style="color: #666666; font-style: italic;"># Tabla de frecuencias de 3 vías (3 variables)</span>
<a href="http://inside-r.org/r-doc/stats/xtabs"><span style="color: #003399; font-weight: bold;">xtabs</span></a><span style="color: #009900;">(</span>~A+B+<a href="http://inside-r.org/r-doc/stats/C"><span style="color: #003399; font-weight: bold;">C</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># tabla de frecuencias de 3 vías</span>
<span style="color: #666666; font-style: italic;"># Tabla de contingencia (Cross Tabulation) de 2 vías (2 variables)</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/gmodels">gmodels</a><span style="color: #009900;">)</span>
CrossTable<span style="color: #009900;">(</span>mydata$myrowvar<span style="color: #339933;">,</span> mydata$mycolvar<span style="color: #009900;">)</span>
requires<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/with"><span style="color: #003399; font-weight: bold;">with</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/warpbreaks"><span style="color: #003399; font-weight: bold;">warpbreaks</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/boot/wool"><span style="color: #003399; font-weight: bold;">wool</span></a><span style="color: #339933;">,</span> tension<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#distribución de frecuencias simples</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/state.division"><span style="color: #003399; font-weight: bold;">state.division</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/datasets/state.region"><span style="color: #003399; font-weight: bold;">state.region</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/with"><span style="color: #003399; font-weight: bold;">with</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/airquality"><span style="color: #003399; font-weight: bold;">airquality</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>Temp<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>Temp<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> Month<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#tabla de contingencia de 2 vías</span>
<span style="color: #666666; font-style: italic;">#La tabla ignora los valores perdidos, para incluir los NA como una categoría para contar debemos incluir la opción exclude=NULL si la variable es un vector. Si la variable es un factor debemos crear un nuevo factor usando: newfactor <- factor(oldfactor, exclude=NULL). (Obtenido de "QuickR" -http://www.statmethods.net/stats/frequencies.html-)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/grDevices/nclass"><span style="color: #003399; font-weight: bold;">nclass</span></a>=<span style="color: #339933;">,</span> breaks=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#por ejemplo: </span>
<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>velocidades<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a>=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">'14'</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/grDevices/nclass"><span style="color: #003399; font-weight: bold;">nclass</span></a>=<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>ylim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>main= <span style="color: blue;">'Velocidad autom¶oviles'</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/sub"><span style="color: #003399; font-weight: bold;">sub</span></a>=<span style="color: blue;">'Via el Volador'</span><span style="color: #339933;">,</span> ylab=<span style="color: blue;">'Frecuencias'</span><span style="color: #339933;">,</span>xlab=<span style="color: blue;">'Velocidad en Km/h'</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#$breaks (límites de clase)</span>
<span style="color: #666666; font-style: italic;">#$counts (frecuencias absolutas)</span>
<span style="color: #666666; font-style: italic;">#$intensities (frecuencias relativas)</span>
<span style="color: #666666; font-style: italic;">#$mids (marcas de clase o puntos medios)</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 1.b. Gráficos #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<a href="http://inside-r.org/r-doc/grDevices/x11"><span style="color: #003399; font-weight: bold;">x11</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># activa un dispositivo gráfico </span>
<a href="http://inside-r.org/r-doc/grDevices/dev.off"><span style="color: #003399; font-weight: bold;">dev.off</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#cierra un dispositivo gráfico</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># selecciona ciertos parámetros gráficos que están definidos por defecto y pueden modificarse</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics"><span style="color: #006600; font-weight: bold;">graphics</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># demostración de gráficos y código de R</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gráfico de dispersión: x e y son vectores</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#si x es un vector numérico realiza un gráfico de sus elementos sobre el índice, y si x es una serie temporal realiza un gráfico de x frente al tiempo</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># si f es factor realiza un gráfico de barras</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>f<span style="color: #339933;">,</span> x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># si f es factor y x vector, realiza un diagrama de cajas para cada nivel del factor f</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>hoja.datos<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># realiza un gráfico de dispersión para cada pareja de variables</span>
<span style="color: #666666; font-style: italic;">## argumentos de las funciones gráficas</span>
<span style="color: #666666; font-style: italic;">#type= "n" (nada) "p" (puntos) "l" (líneas) "b" (both: punto+línea) "o" "s" "S" "h"</span>
<span style="color: #666666; font-style: italic;">#xlab= ylab= (añade etiquetas a los ejes)</span>
<span style="color: #666666; font-style: italic;">#main= sub= (añade título y subtítulo)</span>
<span style="color: #666666; font-style: italic;">#xlim=c(,) ylim=c(,) (selecciona los límites mínimo y máximo para los ejes)</span>
<span style="color: #666666; font-style: italic;">#add= T o F (solapa un gráfico con otro ya existente o no)</span>
<span style="color: #666666; font-style: italic;">#col= (indicar color). ver la opción colors()</span>
<span style="color: #666666; font-style: italic;">## otras funciones gráficas</span>
<a href="http://inside-r.org/r-doc/graphics/points"><span style="color: #003399; font-weight: bold;">points</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span> pch=nº<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#añade puntos definidos por las coordenadas contenidas en los vectores x e y el aspecto indicado en pch. Se puede utilizar type</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>h=<span style="color: #339933;">,</span> v=<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># añade líneas horizontales y/o verticales</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/lm"><span style="color: #003399; font-weight: bold;">lm</span></a><span style="color: #009900;">)</span> o <a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span>b<span style="color: #339933;">,</span> lty=nº<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#añade una recta y=a+b*x con el trazo indicado en lty</span>
<a href="http://inside-r.org/r-doc/graphics/legend"><span style="color: #003399; font-weight: bold;">legend</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># añade la leyenda en el punto indicado</span>
<a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a><span style="color: #009900;">(</span>main=<span style="color: blue;">""</span> <span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/sub"><span style="color: #003399; font-weight: bold;">sub</span></a>=<span style="color: blue;">""</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#añade título y subtítulo</span>
<a href="http://inside-r.org/r-doc/graphics/axis"><span style="color: #003399; font-weight: bold;">axis</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># modifica elementos referentes a los ejes como color, tickmarks, fuentes, etc</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span>etiquetas<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#añade etiquetas en las posiciones marcadas por x e y</span>
<a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/curve"><span style="color: #003399; font-weight: bold;">curve</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#gráficos simples</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/grDevices/nclass"><span style="color: #003399; font-weight: bold;">nclass</span></a><span style="color: #339933;">,</span> breaks<span style="color: #339933;">,</span> probability<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#histograma</span>
mid.age<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">13</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">17.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">22.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">44.5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">70.5</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span>
acc.count<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">28</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">46</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">58</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">31</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">64</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">149</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">316</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">103</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span>
age.acc<-<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>mid.age<span style="color: #339933;">,</span> acc.count<span style="color: #009900;">)</span> <span style="color: #339933;">;</span> age.acc <span style="color: #339933;">;</span>
brk<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">17</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>age.acc<span style="color: #339933;">,</span> breaks=brk<span style="color: #009900;">)</span>
A<-<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/graphics/lines"><span style="color: #003399; font-weight: bold;">lines</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/min"><span style="color: #003399; font-weight: bold;">min</span></a><span style="color: #009900;">(</span>A$breaks<span style="color: #009900;">)</span><span style="color: #339933;">,</span> A$mids<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/max"><span style="color: #003399; font-weight: bold;">max</span></a><span style="color: #009900;">(</span>A$breaks<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> A$counts<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> type=<span style="color: blue;">"l"</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"red"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#histograma con polígono de fercuencias</span>
x<-<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">145</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> n<-<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#funciónd e distribución empírica</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sort"><span style="color: #003399; font-weight: bold;">sort</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>: n<span style="color: #009900;">)</span>/n <span style="color: #339933;">,</span> type=”<a href="http://inside-r.org/r-doc/mgcv/s"><span style="color: #003399; font-weight: bold;">s</span></a>”<span style="color: #339933;">,</span> ylim=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/qqnorm"><span style="color: #003399; font-weight: bold;">qqnorm</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gráfico de probabilidad normal, en abscisas están los valores esperados de los cuantiles de la normal y en las ordenadas los valores de x</span>
<a href="http://inside-r.org/r-doc/stats/qqplot"><span style="color: #003399; font-weight: bold;">qqplot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gráfico cuantil-cuantil, en abscisas están lso cuantiles del vector x y en ordenadas los cuantiles del vector y</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span>vector1<span style="color: #339933;">,</span> vector2<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a>=F o T<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de cajas como vectores</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/subset"><span style="color: #003399; font-weight: bold;">subset</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a>=F o T<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># diagrama de cajas múltiple</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de cajas para cada nivel del factor f</span>
<a href="http://inside-r.org/r-doc/graphics/stripchart"><span style="color: #003399; font-weight: bold;">stripchart</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a> o x<span style="color: #339933;">,</span> method=<span style="color: blue;">"overplot"</span> o <span style="color: blue;">"jitter"</span> o <span style="color: blue;">"stack"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gráfico de puntos</span>
<a href="http://inside-r.org/r-doc/graphics/stem"><span style="color: #003399; font-weight: bold;">stem</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de tallo-hojas</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de barras para una variable numérica discreta</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de barras para una variable categórica</span>
<a href="http://inside-r.org/r-doc/graphics/barplot"><span style="color: #003399; font-weight: bold;">barplot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/vector"><span style="color: #003399; font-weight: bold;">vector</span></a> o tabla<span style="color: #339933;">,</span> names.args=<span style="color: black; font-weight: bold;">NULL</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#cada valor representa la altura de una barra que podemos etiquetar opcionalmente con names.args</span>
<a href="http://inside-r.org/r-doc/graphics/barplot"><span style="color: #003399; font-weight: bold;">barplot</span></a><span style="color: #009900;">(</span>matriz o tabla<span style="color: #339933;">,</span> beside=T <span style="color: #009900;">(</span>adyacentes<span style="color: #009900;">)</span> o F <span style="color: #009900;">(</span>apiladas<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de barras múltiples apiladas o no.</span>
<a href="http://inside-r.org/r-doc/graphics/pie"><span style="color: #003399; font-weight: bold;">pie</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#diagrama de sectores</span>
<span style="color: #666666; font-style: italic;">#gráficos múltiples por ventana</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span>mfrow=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>filas<span style="color: #339933;">,</span> columnas<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#divide la pantalla gráfica en tantas filas y columnas como se indique</span>
<a href="http://inside-r.org/r-doc/graphics/split.screen"><span style="color: #003399; font-weight: bold;">split.screen</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#datos multivariantes</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#realiza un gráfico de dispersión de todos los pares de variables</span>
<a href="http://inside-r.org/r-doc/graphics/pairs"><span style="color: #003399; font-weight: bold;">pairs</span></a><span style="color: #009900;">(</span>A<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># matriz de gráficos planos, un gráfico de dispersión para cada pareja de columnas de A</span>
<a href="http://inside-r.org/r-doc/graphics/coplot"><span style="color: #003399; font-weight: bold;">coplot</span></a><span style="color: #009900;">(</span>a~b|c<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gráficos condicionales</span>
<a href="http://inside-r.org/r-doc/graphics/persp"><span style="color: #003399; font-weight: bold;">persp</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa gráficamente funciones de 2 variables</span>
<a href="http://inside-r.org/r-doc/graphics/contour"><span style="color: #003399; font-weight: bold;">contour</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa curvas de nivel de una función de 3 variables</span>
<a href="http://inside-r.org/r-doc/graphics/image"><span style="color: #003399; font-weight: bold;">image</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa un mapa de 2 variables</span>
<a href="http://inside-r.org/r-doc/graphics/matplot"><span style="color: #003399; font-weight: bold;">matplot</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#representa una matriz frente a otra por columnas</span>
<a href="http://inside-r.org/r-doc/graphics/matpoints"><span style="color: #003399; font-weight: bold;">matpoints</span></a> o <a href="http://inside-r.org/r-doc/graphics/matlines"><span style="color: #003399; font-weight: bold;">matlines</span></a> <span style="color: #666666; font-style: italic;"># añade puntos o líneas a un gráfico (matrices)</span>
<span style="color: #666666; font-style: italic;">#otros</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/lattice">lattice</a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/lattice/xyplot"><span style="color: #003399; font-weight: bold;">xyplot</span></a><span style="color: #009900;">(</span>circumference ~ age<span style="color: #339933;">,</span> groups=<a href="http://inside-r.org/packages/cran/tree">Tree</a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>=<a href="http://inside-r.org/r-doc/datasets/Orange"><span style="color: #003399; font-weight: bold;">Orange</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#guardar gráficos</span>
<a href="http://inside-r.org/r-doc/grDevices/pdf"><span style="color: #003399; font-weight: bold;">pdf</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/file"><span style="color: #003399; font-weight: bold;">file</span></a>=<span style="color: blue;">"f1,pdf"</span><span style="color: #339933;">,</span> width=<span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> heigth=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/dev.off"><span style="color: #003399; font-weight: bold;">dev.off</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#otra forma</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/grDevices/dev.copy2eps"><span style="color: #003399; font-weight: bold;">dev.copy2eps</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#el nombre del archivo por defecto es Rplot.eps y el formato es postcript</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<span style="color: #666666; font-style: italic;"># 1.c. Estadísticos simples #</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<span style="color: #666666; font-style: italic;">#mean Media aritmética</span>
<span style="color: #666666; font-style: italic;">#mean(datos, trim=.05) Media truncada o recortada al 5%</span>
<span style="color: #666666; font-style: italic;">#median El percentil 0.5: la mediana</span>
<span style="color: #666666; font-style: italic;">#min El mínimo de una serie de números</span>
<span style="color: #666666; font-style: italic;">#max El máximo de una serie de números</span>
<span style="color: #666666; font-style: italic;">#quantile Los percentiles de una distribución</span>
<span style="color: #666666; font-style: italic;">#range Mínimo y máximo de un vector</span>
<span style="color: #666666; font-style: italic;">#sum Suma aritmética</span>
<span style="color: #666666; font-style: italic;">#var Cuasi-Varianza y cuasi-covarianza</span>
<span style="color: #666666; font-style: italic;">#var(datos)*(length(datos)-1)/length(datos) Varianza</span>
<span style="color: #666666; font-style: italic;">#sd Cuasi-desviación típica</span>
<span style="color: #666666; font-style: italic;">#sqrt(var(datos))*(length(datos)-1)/length(datos) Desviación típica</span>
<span style="color: #666666; font-style: italic;">#sd(datos)/abs(mean(datos)) Coeficiente de Variación</span>
<span style="color: #666666; font-style: italic;">#mad Desviación media de la mediana</span>
<span style="color: #666666; font-style: italic;">#skewness Asimetría</span>
<span style="color: #666666; font-style: italic;">#kurtosis Curtosis o apuntalamiento</span>
<span style="color: #666666; font-style: italic;">#summary Resumen de estadísticas de una serie de datos</span>
<span style="color: #666666; font-style: italic;">#fivenum Retorna los 5 números de Tukey (de cajas de Tukey): minimum, lower-hinge, median, upper-hinge, maximum.</span>
<span style="color: #666666; font-style: italic;">#cor Correlación (admite uno o dos argumentos)</span>
<span style="color: #666666; font-style: italic;">#cumsum Suma cumulativa de un vector</span>
<span style="color: #666666; font-style: italic;">#prod El producto de los elementos de un vector</span>
<span style="color: #666666; font-style: italic;">#sample Muestreo aleatorio (y permutaciones)</span>
<span style="color: #666666; font-style: italic;">### Veamos algunos ejemplos:</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#media aritmética</span>
<a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/abs"><span style="color: #003399; font-weight: bold;">abs</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#media geométrica</span>
<span style="color: #cc66cc;">1</span>/mean<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#media armónica</span>
<a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#mediana </span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#cuartiles</span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> probs=<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#deciles</span>
weigthed.mean<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
IRQ<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/range"><span style="color: #003399; font-weight: bold;">range</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>f<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#para obtener resúmenes por grupos</span>
<span style="color: #cc66cc;">100</span>*<a href="http://inside-r.org/r-doc/stats/sd"><span style="color: #003399; font-weight: bold;">sd</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>/mean<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># CV coeficiente de variación</span>
<span style="color: #666666; font-style: italic;">#Estadísticas simples:</span>
x = <a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
x
<span style="color: #666666; font-style: italic;"># [1] 1 2 3 4 5 6 7 8 9 10</span>
<a href="http://inside-r.org/r-doc/base/cumsum"><span style="color: #003399; font-weight: bold;">cumsum</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># [1] 1 3 6 10 15 21 28 36 45 55</span>
<a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#[1] 5.5</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<span style="color: #666666; font-style: italic;"># 2. Ejemplo: datos Iris</span>
<span style="color: #666666; font-style: italic;">########################################################################### </span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/head"><span style="color: #003399; font-weight: bold;">head</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>Species<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># resumen estadístico de 1 variable</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#resumen estadístico de varias variables</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Species<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/var"><span style="color: #003399; font-weight: bold;">var</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/cor"><span style="color: #003399; font-weight: bold;">cor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#matriz de correlaciones</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#gráfico de un dataframe</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gráfico de 1 variables </span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gráfico de 2 variables; ídem plot(Petal.Length~Sepal.Length)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Species<span style="color: #009900;">[</span>Sepal.Length><span style="color: #cc66cc;">5.1</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gráfico de 1 factor</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Species<span style="color: #339933;">,</span> Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># gráfico de 1 factor (diagrama de cajas)</span>
<a href="http://inside-r.org/r-doc/graphics/hist"><span style="color: #003399; font-weight: bold;">hist</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#histograma</span>
<a href="http://inside-r.org/r-doc/graphics/pie"><span style="color: #003399; font-weight: bold;">pie</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>Species<span style="color: #009900;">[</span>Sepal.Length><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/qqline"><span style="color: #003399; font-weight: bold;">qqline</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># comparación de un vector con los valores esperados normales</span>
<a href="http://inside-r.org/r-doc/stats/qqplot"><span style="color: #003399; font-weight: bold;">qqplot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span> Petal.Length<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># comparación de las distribuciones de dos vectores o variables </span>
cols <- <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"red"</span><span style="color: #339933;">,</span><span style="color: blue;">"blue"</span><span style="color: #339933;">,</span><span style="color: blue;">"green"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> each=<span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>
pich <- <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">21</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">22</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">23</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>each=<span style="color: #cc66cc;">50</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #339933;">,</span>Petal.Length<span style="color: #339933;">,</span> pch=pich<span style="color: #339933;">,</span> bg=colores<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">################################################################################ </span><span style="color: #666666; font-style: italic;"># 3. Otras funciones para el manejo de datos # </span>
<span style="color: #666666; font-style: italic;">################################################################################ </span>
<span style="color: #666666; font-style: italic;">######### Primer visión de los datos</span>
worms=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/worms.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#las funciones by y aggregate permiten resumir un dataframe según los niveles del factor</span>
<a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a><span style="color: #009900;">(</span>worms<span style="color: #339933;">,</span> Vegetation<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## Subíndice e índices</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>:<span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Seleccionar filas aleatoriamente de un Dataframe</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Ordenando los Dataframes</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Slope<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/rev"><span style="color: #003399; font-weight: bold;">rev</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Slope<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #339933;">,</span> Worm.density<span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Usar condiciones lógicas para seleccionar filas de un Dataframe</span>
worms<span style="color: #009900;">[</span>Damp == T<span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span>Worm.density > <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span>Worm.density<span style="color: #009900;">)</span> & Soil.pH < <span style="color: #cc66cc;">5.2</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>worms<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.numeric"><span style="color: #003399; font-weight: bold;">is.numeric</span></a><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>worms<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.factor"><span style="color: #003399; font-weight: bold;">is.factor</span></a><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Drop filas</span>
worms<span style="color: #009900;">[</span>-<span style="color: #009900;">(</span><span style="color: #cc66cc;">6</span>:<span style="color: #cc66cc;">15</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span>!<span style="color: #009900;">(</span>Vegetation==<span style="color: blue;">"Grassland"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
worms<span style="color: #009900;">[</span>-<a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a><span style="color: #009900;">(</span>Damp==F<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Omitir filas que contengan valores perdidos</span>
data<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>worms.missing.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a>
<a href="http://inside-r.org/r-doc/stats/na.omit"><span style="color: #003399; font-weight: bold;">na.omit</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span>
new.frame<-<a href="http://inside-r.org/r-doc/stats/na.exclude"><span style="color: #003399; font-weight: bold;">na.exclude</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/stats/complete.cases"><span style="color: #003399; font-weight: bold;">complete.cases</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#### Usar order y unique para eliminar la pseudoreplicación</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/rev"><span style="color: #003399; font-weight: bold;">rev</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Worm.density<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/unique"><span style="color: #003399; font-weight: bold;">unique</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">#### Ordenar de manera compleja con direcciones mixtas </span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #339933;">,</span>-Worm.density<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># solo podemos utilizar el signo de menos cuando ordenamos variables numéricas, si no debemos utilizar la función rank para hacer numéricos los niveles de los factores.</span>
worms<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>-<a href="http://inside-r.org/r-doc/base/rank"><span style="color: #003399; font-weight: bold;">rank</span></a><span style="color: #009900;">(</span>Vegetation<span style="color: #009900;">)</span><span style="color: #339933;">,</span>-Worm.density<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Un dataframe con nombres en las filas en lugar de números</span>
<a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">)</span>
worms=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/worms.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#worms<-read.table("c:\\temp\\worms.txt",header=T,row.names=1)</span>
worms
<span style="color: #666666; font-style: italic;">##### Crear un dataframe</span>
x<-<a href="http://inside-r.org/r-doc/stats/runif"><span style="color: #003399; font-weight: bold;">runif</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
y<-<span style="color: black; font-weight: bold;">letters</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">]</span>
z<-<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>T<span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>F<span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
new<-<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span>z<span style="color: #339933;">,</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/methods/new"><span style="color: #003399; font-weight: bold;">new</span></a>
y<-<a href="http://inside-r.org/r-doc/stats/rpois"><span style="color: #003399; font-weight: bold;">rpois</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1500</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1.5</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
short.frame<-<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># expandir un dataframe</span>
long<-<a href="http://inside-r.org/r-doc/base/as.data.frame"><span style="color: #003399; font-weight: bold;">as.data.frame</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>short.frame<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> short.frame$Freq<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
long<span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">##### Eliminar filas duplicadas en un Dataframe</span>
dups<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>dups.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
dups
<a href="http://inside-r.org/r-doc/base/unique"><span style="color: #003399; font-weight: bold;">unique</span></a><span style="color: #009900;">(</span>dups<span style="color: #009900;">)</span>
dups<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/duplicated"><span style="color: #003399; font-weight: bold;">duplicated</span></a><span style="color: #009900;">(</span>dups<span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">##### Juntar dos Dataframes: Merge</span>
lifeform=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/lifeforms.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span><span style="color: #339933;">;</span> lifeform
flowering=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/fltimes.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span><span style="color: #339933;">;</span> flowering
<a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>flowering<span style="color: #339933;">,</span>lifeforms<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#contiene solo las filas que tienen entradas completas para ambos dataframes</span>
<span style="color: #009900;">(</span>both<-<a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>flowering<span style="color: #339933;">,</span>lifeforms<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/all"><span style="color: #003399; font-weight: bold;">all</span></a>=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># si queremos incluir todas las especies poniendo NA donde no hay coincidencias</span>
<span style="color: #666666; font-style: italic;"># cuando tenemos diferentes nombres en 2 dataframes que queremos unir:</span>
<span style="color: #009900;">(</span>seeds<-<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"c:<span style="color: #000099; font-weight: bold;">\\</span>temp<span style="color: #000099; font-weight: bold;">\\</span>seedwts.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/merge"><span style="color: #003399; font-weight: bold;">merge</span></a><span style="color: #009900;">(</span>both<span style="color: #339933;">,</span>seeds<span style="color: #339933;">,</span>by.x=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"Genus"</span><span style="color: #339933;">,</span><span style="color: blue;">"species"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>by.y=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"name1"</span><span style="color: #339933;">,</span><span style="color: blue;">"name2"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">##### Juntar dos Dataframes: Match</span>
herb=<a href="http://inside-r.org/r-doc/utils/read.table"><span style="color: #003399; font-weight: bold;">read.table</span></a><span style="color: #009900;">(</span><span style="color: blue;">"http://www.bio.ic.ac.uk/research/mjcraw/therbook/data/herbicides.txt"</span><span style="color: #339933;">,</span>header=T<span style="color: #009900;">)</span>
herb
worms$hb=herb$Herbicide<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/match"><span style="color: #003399; font-weight: bold;">match</span></a><span style="color: #009900;">(</span>worms$Vegetation<span style="color: #339933;">,</span>herb$Type<span style="color: #009900;">)</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;">## agregar un herbicida recomendado </span>
<a href="http://inside-r.org/r-doc/base/match"><span style="color: #003399; font-weight: bold;">match</span></a><span style="color: #009900;">(</span>worms$Vegetation<span style="color: #339933;">,</span>herb$Type<span style="color: #009900;">)</span>
worms
<span style="color: #666666; font-style: italic;">##### Resumir los contenidos de un dataframe: </span>
<span style="color: #666666; font-style: italic;">#summary: resume los contenidos de todas las variables </span>
<span style="color: #666666; font-style: italic;">#aggregate: crea una tabla del tipo tapply</span>
<span style="color: #666666; font-style: italic;">#by: crea funciones para cada nivel del factor especificado</span>
<a href="http://inside-r.org/r-doc/stats/aggregate"><span style="color: #003399; font-weight: bold;">aggregate</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>veg=Vegetation<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/aggregate"><span style="color: #003399; font-weight: bold;">aggregate</span></a><span style="color: #009900;">(</span>worms<span style="color: #009900;">[</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>veg=Vegetation<span style="color: #339933;">,</span>d=Damp<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
x1=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> x2=<span style="color: #cc66cc;">11</span>:<span style="color: #cc66cc;">18</span><span style="color: #339933;">;</span> x3=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"i"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>each=<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x4=<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"f"</span><span style="color: #339933;">,</span><span style="color: blue;">"m"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>=<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>y=x1<span style="color: #339933;">,</span>z=x2<span style="color: #339933;">,</span>age=x3<span style="color: #339933;">,</span>sex=x4<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$sex<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># returns frequencies</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/table"><span style="color: #003399; font-weight: bold;">table</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">## apply (to a matrix)</span>
x=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x <span style="color: #666666; font-style: italic;"># 2 rows, 3 cols</span>
<a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 21</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 9 12 sum rows(1)</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/sum"><span style="color: #003399; font-weight: bold;">sum</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 3, 7 11 sum cols(2)</span>
<span style="color: #666666; font-style: italic;">## tapply (table apply)</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># tapply(vector,factor,function) for table apply</span>
<a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>=<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$age<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/df"><span style="color: #003399; font-weight: bold;">df</span></a>$sex<span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a>
<a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a><span style="color: #009900;">[</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"m"</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/t"><span style="color: #003399; font-weight: bold;">t</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># 3 you can reference individual elements</span>
<span style="color: #666666; font-style: italic;"># Unir y cortar vectores de un Dataframe</span>
<a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/formula"><span style="color: #003399; font-weight: bold;">formula</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/PlantGrowth"><span style="color: #003399; font-weight: bold;">PlantGrowth</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># check the default formula</span>
pg <- <a href="http://inside-r.org/r-doc/utils/unstack"><span style="color: #003399; font-weight: bold;">unstack</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/PlantGrowth"><span style="color: #003399; font-weight: bold;">PlantGrowth</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># unstack according to this formula</span>
pg
<a href="http://inside-r.org/r-doc/utils/stack"><span style="color: #003399; font-weight: bold;">stack</span></a><span style="color: #009900;">(</span>pg<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># now put it back together</span>
<a href="http://inside-r.org/r-doc/utils/stack"><span style="color: #003399; font-weight: bold;">stack</span></a><span style="color: #009900;">(</span>pg<span style="color: #339933;">,</span> select = -ctrl<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># omitting one vector</span>
<a href="http://inside-r.org/r-doc/base/subset"><span style="color: #003399; font-weight: bold;">subset</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <a href="http://inside-r.org/packages/cran/cond">cond</a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#devuelve una selección de x que cumple unas condiciones</span>
<a href="http://inside-r.org/r-doc/base/split"><span style="color: #003399; font-weight: bold;">split</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> f<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#divide el vector o la hora de datos x en grupos definidos por los valores de f</span>
<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> breaks<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=T o F<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># divide el rango del vector x en intervalos y codifica los elementos de x de acuerdo con el intervalo en el que caigan</span></pre>
</div>
</div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-50492974909322143892011-09-29T06:54:00.000-07:002013-03-10T08:14:41.445-07:00Diagramas de cajas (boxplot), cuantiles, IQR, valores atípicos, y más. Aplicación en R<span class="Apple-style-span" style="font-size: large;">Diagrama de caja o Caja con bigotes</span><br />
Es un gráfico, basado en <span style="font-weight: bold;">cuartiles</span>, mediante el cual se visualiza un conjunto de datos. Está compuesto por un rectángulo, la "caja", y dos brazos, los "bigotes".<br />
Se dibujará una caja entre las bisagras (<i>hinges</i>) superior e inferior con una línea en la mediana. Bigotes (<i>whisker</i>, una sola línea, no un cuadro) se extienden desde las bisagras.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
Si preguntamos a R por los detalles:<br />
?boxplot.stats<br />
<br />
<br />
<div style="font-family: sans-serif; font-size: 13px;">
The two ‘hinges’ are versions of the first and third quartile, i.e., close to <code><a href="http://127.0.0.1:28002/help/library/grDevices/help/quantile">quantile</a>(x, c(1,3)/4)</code>. The hinges equal the quartiles for odd <i>n</i> (where <code>n <- code="" length="" x="">) and differ for even <i>n</i>. Whereas the quartiles only equal observations for <code>n %% 4 == 1</code> (<i>n = 1 mod 4</i>), the hinges do so <em>additionally</em> for <code>n %% 4 == 2</code> (<i>n = 2 mod 4</i>), and are in the middle of two observations otherwise.<!-----><!-----></-></code></div>
<div style="font-family: sans-serif; font-size: 13px;">
The notches (if requested) extend to <code>+/-1.58 IQR/sqrt(n)</code>. This seems to be based on the same calculations as the formula with 1.57 in Chambers <em>et al.</em> (1983, p. 62), given in McGill <em>et al.</em> (1978, p. 16). They are based on asymptotic normality of the median and roughly equal sample sizes for the two medians being compared, and are said to be rather insensitive to the underlying distributions of the samples. The idea appears to be to give roughly a 95% confidence interval for the difference in two medians.</div>
<div style="font-family: sans-serif; font-size: 13px;">
<br /></div>
<div style="font-family: sans-serif; font-size: 13px;">
<br /></div>
<div style="font-family: sans-serif; font-size: 13px;">
<span class="Apple-style-span" style="font-family: Times; font-size: small;">A la izquierda observamos el gráfico que lanza R al pedirle:</span></div>
<div style="font-family: sans-serif; font-size: 13px;">
boxplot(alltime.movies$Gross)</div>
<div style="font-family: sans-serif; font-size: 13px;">
<br /></div>
<div style="font-family: sans-serif; font-size: 13px;">
<span class="Apple-style-span" style="font-family: Times; font-size: small;">Mientras que a la derecha he generado un gráfico que explica cada estadístico utilizado. El código para este gráfico se observa debajo.</span></div>
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyH7t0u7WCriRZSm8kHcZ1NcVMOAYufCtjpvf4mBrDyBIvRNzHe1-lsQ4CZphjevoaNDIbm3x1LvxSkmjujRz3coN2GSRICRaL5i-Qt03yfY8pNUYvfH82etF5M8zsdIbLaYkbFmfFios/s1600/boxplot3.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="480" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyH7t0u7WCriRZSm8kHcZ1NcVMOAYufCtjpvf4mBrDyBIvRNzHe1-lsQ4CZphjevoaNDIbm3x1LvxSkmjujRz3coN2GSRICRaL5i-Qt03yfY8pNUYvfH82etF5M8zsdIbLaYkbFmfFios/s640/boxplot3.jpeg" width="640" /></a></div>
<br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">##############################################################</span>
<span style="color: #666666; font-style: italic;">####### boxplot2 grafica un diagrama de cajas ################</span>
<span style="color: #666666; font-style: italic;">####### identificando los estadísticos que intervienen #######</span>
<span style="color: #666666; font-style: italic;">##############################################################</span>
<a href="http://inside-r.org/r-doc/utils/install.packages"><span style="color: #003399; font-weight: bold;">install.packages</span></a><span style="color: #009900;">(</span><span style="color: blue;">"UsingR"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/packages/cran/UsingR">UsingR</a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#creamos una nueva función </span>
boxplot2<-<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x=Gross<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<span style="color: #666666; font-style: italic;">#calculo estadísticos</span>
<a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a>=<a href="http://inside-r.org/r-doc/grDevices/boxplot.stats"><span style="color: #003399; font-weight: bold;">boxplot.stats</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>$stats <span style="color: #666666; font-style: italic;">#extreme of the lower whisker, the lower ‘hinge’, the median, the upper ‘hinge’ and the extreme of the upper whisker.</span>
iqr=<a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>-<a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;">#rango intercuartílico</span>
f=<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#minimum, lower-hinge, median, upper-hinge, maximum</span>
stats2<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #339933;">,</span>f<span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#agrego los min y max del fivenum</span>
<span style="color: #666666; font-style: italic;">#grafico</span>
<a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#hago el gráfico de cajas</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>h=<a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span>lty=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"red"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#agrego una línea roja en el lower whisker</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>h=<a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span>lty=<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"red"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#agrego una línea roja en el upper whisker</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1.35</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> stats2<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">'minimum'</span><span style="color: #339933;">,</span><span style="color: blue;">'lower whisker'</span><span style="color: #339933;">,</span> <span style="color: blue;">'lower hinge'</span><span style="color: #339933;">,</span> <span style="color: blue;">'median'</span><span style="color: #339933;">,</span> <span style="color: blue;">'upper hinge'</span><span style="color: #339933;">,</span> <span style="color: blue;">'upper whisker'</span><span style="color: #339933;">,</span><span style="color: blue;">'maximum'</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> cex=<span style="color: #cc66cc;">0.6</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">##agrego labels</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.7</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> stats2<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span>stats2<span style="color: #009900;">)</span><span style="color: #339933;">,</span> cex=<span style="color: #cc66cc;">0.6</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">##agrego los valores</span>
<span style="color: #666666; font-style: italic;">#para estos datos el mínimo coincide con el lower whisker</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1.35</span><span style="color: #339933;">,</span>f<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>-<span style="color: #cc66cc;">1.5</span>*iqr <span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<span style="color: blue;">"lower notche"</span><span style="color: #339933;">,</span> cex=<span style="color: #cc66cc;">0.6</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">##agrego labels</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.7</span><span style="color: #339933;">,</span> f<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>-<span style="color: #cc66cc;">1.5</span>*iqr<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>-<span style="color: #cc66cc;">1.5</span>*iqr<span style="color: #009900;">)</span><span style="color: #339933;">,</span> cex=<span style="color: #cc66cc;">0.6</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">##agrego los valores</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>h=f<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>-<span style="color: #cc66cc;">1.5</span>*iqr<span style="color: #339933;">,</span> lty=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1.35</span><span style="color: #339933;">,</span>f<span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>+<span style="color: #cc66cc;">1.5</span>*iqr <span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<span style="color: blue;">"upper notche"</span><span style="color: #339933;">,</span> cex=<span style="color: #cc66cc;">0.6</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">##agrego labels</span>
<a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.7</span><span style="color: #339933;">,</span> f<span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>+<span style="color: #cc66cc;">1.5</span>*iqr<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/labels"><span style="color: #003399; font-weight: bold;">labels</span></a>=<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>+<span style="color: #cc66cc;">1.5</span>*iqr<span style="color: #009900;">)</span><span style="color: #339933;">,</span> cex=<span style="color: #cc66cc;">0.6</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">##agrego los valores</span>
<a href="http://inside-r.org/r-doc/graphics/abline"><span style="color: #003399; font-weight: bold;">abline</span></a><span style="color: #009900;">(</span>h=f<span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>+<span style="color: #cc66cc;">1.5</span>*iqr<span style="color: #339933;">,</span>lty=<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: blue;">"blue"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#lower notche no sale en este gráfico porque es menor al mínimo </span>
<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#ahora para cualquier dato podemos graficar el boxplot con la descripción</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span>alltime.movies<span style="color: #009900;">)</span>
boxplot2<span style="color: #009900;">(</span>x=alltime.movies$Gross<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#################################################</span>
<span style="color: #666666; font-style: italic;">#### EXPLICACIÓN ####</span>
<span style="color: #666666; font-style: italic;">#################################################</span>
<span style="color: #666666; font-style: italic;"># preguntar cómo funciona boxplot.stats y fivenum</span>
<a href="http://inside-r.org/r-doc/grDevices/boxplot.stats"><span style="color: #003399; font-weight: bold;">boxplot.stats</span></a>
<span style="color: #666666; font-style: italic;"># function (x, coef = 1.5, do.conf = TRUE, do.out = TRUE) </span>
<span style="color: #666666; font-style: italic;"># {</span>
<span style="color: #666666; font-style: italic;"># if (coef < 0){stop("'coef' must not be negative")}</span>
<span style="color: #666666; font-style: italic;"># nna <- !is.na(x)</span>
<span style="color: #666666; font-style: italic;"># n <- sum(nna)</span>
<span style="color: #666666; font-style: italic;"># stats <- stats::fivenum(x, na.rm = TRUE)</span>
<span style="color: #666666; font-style: italic;"># iqr <- diff(stats[c(2, 4)])</span>
<span style="color: #666666; font-style: italic;"># if (coef == 0){do.out <- FALSE}</span>
<span style="color: #666666; font-style: italic;"># else {</span>
<span style="color: #666666; font-style: italic;"># out <- if (!is.na(iqr)) {</span>
<span style="color: #666666; font-style: italic;"># x < (stats[2L] - coef * iqr) | x > (stats[4L] + coef * iqr) #por fuera del notch</span>
<span style="color: #666666; font-style: italic;"># }</span>
<span style="color: #666666; font-style: italic;"># else !is.finite(x)</span>
<span style="color: #666666; font-style: italic;"># if (any(out[nna], na.rm = TRUE)) </span>
<span style="color: #666666; font-style: italic;"># stats[c(1, 5)] <- range(x[!out], na.rm = TRUE)</span>
<span style="color: #666666; font-style: italic;"># }</span>
<span style="color: #666666; font-style: italic;"># conf <- if (do.conf) </span>
<span style="color: #666666; font-style: italic;"># stats[3L] + c(-1.58, 1.58) * iqr/sqrt(n)</span>
<span style="color: #666666; font-style: italic;"># list(stats = stats, n = n, conf = conf, out = if (do.out) x[out & </span>
<span style="color: #666666; font-style: italic;"># nna] else numeric())</span>
<span style="color: #666666; font-style: italic;"># }</span>
<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a>
<span style="color: #666666; font-style: italic;"># function (x, na.rm = TRUE) </span>
<span style="color: #666666; font-style: italic;"># {</span>
<span style="color: #666666; font-style: italic;"># xna <- is.na(x)</span>
<span style="color: #666666; font-style: italic;"># if (na.rm) </span>
<span style="color: #666666; font-style: italic;"># x <- x[!xna]</span>
<span style="color: #666666; font-style: italic;"># else if (any(xna)) </span>
<span style="color: #666666; font-style: italic;"># return(rep.int(NA, 5))</span>
<span style="color: #666666; font-style: italic;"># x <- sort(x)</span>
<span style="color: #666666; font-style: italic;"># n <- length(x)</span>
<span style="color: #666666; font-style: italic;"># if (n == 0) </span>
<span style="color: #666666; font-style: italic;"># rep.int(NA, 5)</span>
<span style="color: #666666; font-style: italic;"># else {</span>
<span style="color: #666666; font-style: italic;"># n4 <- floor((n + 3)/2)/2</span>
<span style="color: #666666; font-style: italic;"># d <- c(1, n4, (n + 1)/2, n + 1 - n4, n)</span>
<span style="color: #666666; font-style: italic;"># 0.5 * (x[floor(d)] + x[ceiling(d)])</span>
<span style="color: #666666; font-style: italic;"># }</span>
<span style="color: #666666; font-style: italic;"># }</span>
<span style="color: #666666; font-style: italic;">######################################## </span>
<span style="color: #666666; font-style: italic;">### PARA UNOS DATOS CUALQUIERA Y=1:10</span>
<span style="color: #666666; font-style: italic;">########## ############################## </span>
<span style="color: #666666; font-style: italic;">### ¿Qué son los hinges (bisagras)?:</span>
<span style="color: #666666; font-style: italic;">#lower y upper hinges son versiones de los cuartiles 1 y 3, respectivamente.</span>
<span style="color: #666666; font-style: italic;">#cuando la muestra (o número de datos n=length(x)) es impar, son los cuartiles</span>
<span style="color: #666666; font-style: italic;">#cuando la muestra es par, difiere de los cuartiles. </span>
<span style="color: #666666; font-style: italic;">#ej</span>
y=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">9</span> <span style="color: #666666; font-style: italic;">#si la muestra es impar, no hay problema, los cuartiles son números enteros</span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">.75</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># 3 y 7</span>
y=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span> <span style="color: #666666; font-style: italic;">#pero si la muestra es par, los cuartiles son números reales (salen con decimales), cuando en realidad "y" no tiene decimales</span>
<a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">.75</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#3.25 y 7.75</span>
<span style="color: #666666; font-style: italic;">### ¿Cómo se calculan los hinges?:</span>
<span style="color: #666666; font-style: italic;">#para solucionarlo hay 3 formas:</span>
<span style="color: #666666; font-style: italic;"># 0) la forma directa</span>
<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># 3; lower hinges </span>
<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># 8; upper hinges </span>
<span style="color: #666666; font-style: italic;"># 1) redondeamos los datos</span>
<a href="http://inside-r.org/r-doc/base/round"><span style="color: #003399; font-weight: bold;">round</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>y<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">.75</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#3 y 8</span>
<span style="color: #666666; font-style: italic;"># 2) aplicamos la función floor((n + 3)/2)/2 , donde floor redondea el valor</span>
ny=<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
n4 <- <a href="http://inside-r.org/r-doc/base/floor"><span style="color: #003399; font-weight: bold;">floor</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span>ny + <span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>/<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>/<span style="color: #cc66cc;">2</span>
dy <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> n4<span style="color: #339933;">,</span> <span style="color: #009900;">(</span>n + <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>/<span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> n + <span style="color: #cc66cc;">1</span> - n4<span style="color: #339933;">,</span> n<span style="color: #009900;">)</span>
fdy<-<span style="color: #cc66cc;">0.5</span> * <span style="color: #009900;">(</span>y<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/floor"><span style="color: #003399; font-weight: bold;">floor</span></a><span style="color: #009900;">(</span>dy<span style="color: #009900;">)</span><span style="color: #009900;">]</span> + y<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/ceiling"><span style="color: #003399; font-weight: bold;">ceiling</span></a><span style="color: #009900;">(</span>dy<span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#min, lower hinges, median, upper hinges, max</span>
fdy<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># 3; lower hinges o fivenum(y)[2]</span>
fdy<span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># 8; upper hinges o fivenum(y)[4]</span>
<span style="color: #666666; font-style: italic;">### ¿Qué son los whiskers (bigotes)?</span>
<span style="color: #666666; font-style: italic;">#upper whisker: máximo de los valores de x cuando eliminamos aquellos que están por encima de upper hinges+1.5*IQR (hinges+notches) </span>
<span style="color: #666666; font-style: italic;">#lower whisker: mínimo de los valores de x cuando eliminamos aquellos que están por encima de lower hinges-1.5*IQR (hinges-notches)</span>
<span style="color: #666666; font-style: italic;">#donde, notches (muescas): son los valores hinges+-1.5*iqr/sqrt(n)</span>
<span style="color: #666666; font-style: italic;">### ¿Cómo se calculan los whiskers (bigotes)?</span>
<span style="color: #666666; font-style: italic;">#0) forma rápida:</span>
<a href="http://inside-r.org/r-doc/grDevices/boxplot.stats"><span style="color: #003399; font-weight: bold;">boxplot.stats</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>$stats<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">#1) paso a paso. primero calculamos los notches </span>
<a href="http://inside-r.org/r-doc/stats/IQR"><span style="color: #003399; font-weight: bold;">IQR</span></a>=<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>-<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;">#rango intercuartílico, diferencia entre el tercer y primer cuartil</span>
<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span>+<span style="color: #cc66cc;">1.5</span>*<a href="http://inside-r.org/r-doc/stats/IQR"><span style="color: #003399; font-weight: bold;">IQR</span></a> <span style="color: #666666; font-style: italic;">#notche superior; el /sqrt(n) lo usa solo para la salida de do.conf=T</span>
<a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>-<span style="color: #cc66cc;">1.5</span>*<a href="http://inside-r.org/r-doc/stats/IQR"><span style="color: #003399; font-weight: bold;">IQR</span></a> <span style="color: #666666; font-style: italic;">#notche inferior; el /sqrt(n) lo usa solo para la salida de do.conf=T</span>
<span style="color: #666666; font-style: italic;">#luego calculamos los whiskers</span>
outy <- <span style="color: black; font-weight: bold;">if</span> <span style="color: #009900;">(</span>!<a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/IQR"><span style="color: #003399; font-weight: bold;">IQR</span></a><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>y < <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span> - <a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a> * <a href="http://inside-r.org/r-doc/stats/IQR"><span style="color: #003399; font-weight: bold;">IQR</span></a><span style="color: #009900;">)</span> | y > <span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/fivenum"><span style="color: #003399; font-weight: bold;">fivenum</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">]</span> + <a href="http://inside-r.org/r-doc/stats/coef"><span style="color: #003399; font-weight: bold;">coef</span></a> * <a href="http://inside-r.org/r-doc/stats/IQR"><span style="color: #003399; font-weight: bold;">IQR</span></a><span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #666666; font-style: italic;">#por fuera de los notches </span>
<a href="http://inside-r.org/r-doc/base/range"><span style="color: #003399; font-weight: bold;">range</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">[</span>!outy<span style="color: #009900;">]</span><span style="color: #339933;">,</span> na.rm = <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#whiskers</span>
<span style="color: #666666; font-style: italic;">#en estos datos de prueba no hay datos extremos o outliers (y=1:10)</span>
<span style="color: #666666; font-style: italic;">#outliers: the values of any data points which lie beyond the extremes of the whiskers.</span></pre>
</div>
</div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a><br />
<br />
<br />
<span class="Apple-style-span" style="font-size: large;"><br /></span>
<span class="Apple-style-span" style="font-size: large;"><br /></span>
<span class="Apple-style-span" style="font-size: large;">Ventajas</span><br />
* Proporcionan una visión general de la simetría de la distribución de los datos; si la mediana no está en el centro del rectángulo, la distribución no es simétrica.<br />
* Son útiles para ver la presencia de valores atípicos también llamados outliers.<br />
<br />
Veamos entonces las definiciones de cada elemento:<br />
<ul>
<li><span style="font-weight: bold;">Cuantiles (quantil)</span>: son colectivamente los cuartiles (Qi, i=1,...,3 divide los datos en 4 regiones), deciles (Di, i=1,...,9 divide los datos en 10 regiones) y percentiles (Pi, i=1,...,99 divide los datos en 100 regiones).</li>
<li><span style="font-weight: bold;">Rango intercuartílico (IQR)</span>: es la diferencia entre el tercer y el primer cuartil. IQR=Q3-Q1.</li>
</ul>
<ul>
<li><span style="font-weight: bold;">Valores atípicos (outliers)</span>: son observaciones numéricamente distantes del resto de los datos. Tomando como referencia el IQR, en un diagrama de cajas se considera un valor atípico el que se encuentra 1,5 veces esa distancia de uno de los cuartiles (atípico leve) o a 3 veces esa distancia (atípico extermo).</li>
</ul>
<ol>
<li>atípicos leves: son aquellos que se encuentran en x < Q1-1.5*IQR <span style="color: red;">o</span> x > Q3 + 1.5*IQR</li>
<li>atípicos extremos: son aquellos que se encuentran en x < Q1-3*IQR <span style="color: red;">o</span> x > Q3+3*IQR</li>
</ol>
<ul>
<li><span style="font-weight: bold;">Bisagras (hinges)</span>: son los mismos que los cuartiles a menos que al dividir el tamaño de la muestra por 4 el resto sea 3 (como el 39 / 4 = 9 I 3).</li>
</ul>
<ol>
<li>bisagra inferior: es la mediana de la mitad inferior de los datos hasta e incluyendo la mediana.</li>
<li>bisagra superior: es la mediana de la mitad superior de los datos hasta e incluyendo la mediana.</li>
</ol>
<ul>
<li><span style="font-weight: bold;">Resumen de 5-números (five number summary)</span>: consiste en el valor mínimo, la bisagra inferior, la mediana, la bisagra superior y el valor máximo. (aunque algunos libros utilizan los cuartiles en lugar de las bisagras).</li>
</ul>
<span style="font-weight: bold;"></span><br />
Para observar cómo realizar estos gráficos en R, basta con pedirle ejemplos de la función "boxplot".<br />
<br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><a href="http://inside-r.org/r-doc/utils/example"><span style="color: #003399; font-weight: bold;">example</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics/boxplot"><span style="color: #003399; font-weight: bold;">boxplot</span></a><span style="color: #009900;">)</span>
</pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><b>
</b></pre>
</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZryss71HHNRFs-zr6hHojEw5umMKd1L1h9LdQzMyGsJclfKGnorbaY_7KT_nv5ZDMQbA1ClK7D3yVPvuBjQ1St9E-XuzPySM4miOry0jxTvU6XmBZrR8dHP_EcgwPvl1QqtmgbY8jHDQ/s1600/boxplot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="548" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZryss71HHNRFs-zr6hHojEw5umMKd1L1h9LdQzMyGsJclfKGnorbaY_7KT_nv5ZDMQbA1ClK7D3yVPvuBjQ1St9E-XuzPySM4miOry0jxTvU6XmBZrR8dHP_EcgwPvl1QqtmgbY8jHDQ/s640/boxplot.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-82131790991825675392011-09-29T05:29:00.000-07:002011-09-29T05:29:38.538-07:00"R" you ready for R? comandos básicos (Clase 1)<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Análisis de datos estadísticos con R #</span>
<span style="color: #666666; font-style: italic;"># MANEJO DE DATOS #</span>
<span style="color: #666666; font-style: italic;"># 2011/2012 # #</span><span style="color: #666666; font-style: italic;"> Msc. Rosana Ferrero #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Clase 1. Introducción a R</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 0. Antes de comenzar #</span>
<span style="color: #666666; font-style: italic;"># 1. R como calculadora: Aritmética y Funciones básicas # </span>
<span style="color: #666666; font-style: italic;"># 2. R como lenguaje: objetos en R #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 0. Antes de comenzar #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Ayuda detro del programa</span>
?<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a>
<a href="http://inside-r.org/r-doc/utils/help"><span style="color: #003399; font-weight: bold;">help</span></a><span style="color: #009900;">(</span><span style="color: blue;">"rnorm"</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/help.start"><span style="color: #003399; font-weight: bold;">help.start</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># abre la ayuda en html</span>
?<a href="http://inside-r.org/r-doc/utils/help.search"><span style="color: #003399; font-weight: bold;">help.search</span></a>
<a href="http://inside-r.org/r-doc/utils/help.search"><span style="color: #003399; font-weight: bold;">help.search</span></a><span style="color: #009900;">(</span><span style="color: blue;">"normal"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># lista de funciones o paquetes donde aparece una palabra</span>
?<a href="http://inside-r.org/r-doc/utils/apropos"><span style="color: #003399; font-weight: bold;">apropos</span></a>
<a href="http://inside-r.org/r-doc/utils/apropos"><span style="color: #003399; font-weight: bold;">apropos</span></a><span style="color: #009900;">(</span><span style="color: blue;">"normal"</span><span style="color: #009900;">)</span>
?<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># lista de las demotraciones disponibles</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics"><span style="color: #006600; font-weight: bold;">graphics</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics/persp"><span style="color: #003399; font-weight: bold;">persp</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/demo"><span style="color: #003399; font-weight: bold;">demo</span></a><span style="color: #009900;">(</span>lm.glm<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/example"><span style="color: #003399; font-weight: bold;">example</span></a><span style="color: #009900;">(</span><span style="color: blue;">"mean"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># ejemplos de aplicación</span>
<a href="http://inside-r.org/r-doc/utils/vignette"><span style="color: #003399; font-weight: bold;">vignette</span></a><span style="color: #009900;">(</span><span style="color: blue;">"sandwich"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># descarga un pdf del paquete sandwich</span>
<span style="color: #666666; font-style: italic;"># Listado y remoción de objetos en memoria</span>
<a href="http://inside-r.org/r-doc/base/ls"><span style="color: #003399; font-weight: bold;">ls</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para saber los objetos que tenemos en el espacio de trabajo</span>
x <span style="color: #666666; font-style: italic;"># escribir el nombre de un objeto muestra su contenido</span>
<a href="http://inside-r.org/r-doc/base/save"><span style="color: #003399; font-weight: bold;">save</span></a><span style="color: #009900;">(</span><objetos><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/file"><span style="color: #003399; font-weight: bold;">file</span></a>=<span style="color: blue;">"nombre.RData"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para guardar el contenido del espacio de trabajo. También se puede usar: save.image() , si queremos que guarde todo.</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span><span style="color: blue;">"misdatos.RData"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para acceder a los objetos de la carpeta de trabajo</span>
<a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span><span style="color: blue;">"misdatos.RData"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># para deshacer la selección</span>
<a href="http://inside-r.org/r-doc/base/rm"><span style="color: #003399; font-weight: bold;">rm</span></a><span style="color: #009900;">(</span><span style="color: blue;">"x"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># borra el objeto "x", y rm() todo el escritorio de trabajo</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 1. R como calculadora: Aritmética y Funciones básicas #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># operaciones aritméticas básicas</span>
<span style="color: #cc66cc;">2</span> + <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># suma</span>
<span style="color: #cc66cc;">2</span> - <span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># resta</span>
<span style="color: #cc66cc;">2</span>*<span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># multiplicación</span>
<span style="color: #cc66cc;">2</span>/<span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># división</span>
<span style="color: #cc66cc;">2</span>^<span style="color: #cc66cc;">3</span> <span style="color: #666666; font-style: italic;"># potenciación</span>
<a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#exponencial</span>
<a href="http://inside-r.org/r-doc/base/sqrt"><span style="color: #003399; font-weight: bold;">sqrt</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># raíz cuadrada</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># logaritmo</span>
<a href="http://inside-r.org/r-doc/base/sin"><span style="color: #003399; font-weight: bold;">sin</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/cos"><span style="color: #003399; font-weight: bold;">cos</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/tan"><span style="color: #003399; font-weight: bold;">tan</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># trigonométricas</span>
max<span style="color: #339933;">;</span> min<span style="color: #339933;">;</span> range<span style="color: #339933;">;</span> mean<span style="color: #339933;">;</span> sum<span style="color: #339933;">;</span> diff<span style="color: #339933;">;</span> cumsum<span style="color: #339933;">;</span> sd<span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a> <span style="color: #666666; font-style: italic;"># otras</span>
<span style="color: #666666; font-style: italic;">#operadores lógicos</span>
<<span style="color: #339933;">,</span> ><span style="color: #339933;">,</span> <=<span style="color: #339933;">,</span> =><span style="color: #339933;">,</span> ==<span style="color: #339933;">,</span> !=
!<span style="color: #339933;">,</span> &<span style="color: #339933;">,</span> |<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/xor"><span style="color: #003399; font-weight: bold;">xor</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
&&<span style="color: #339933;">,</span> ||
<span style="color: black; font-weight: bold;">if</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># precedencia de operaciones</span>
<span style="color: #cc66cc;">4</span>^<span style="color: #cc66cc;">2</span> - <span style="color: #cc66cc;">3</span>*<span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">1</span> - <span style="color: #cc66cc;">6</span> + <span style="color: #cc66cc;">4</span>
<span style="color: #cc66cc;">2</span>^-<span style="color: #cc66cc;">3</span>
<span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span>^<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> - <span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span>*<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># usar paréntisis para agrupar, clarifica</span>
<span style="color: #666666; font-style: italic;"># Funciones, argumentos en las funciones</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base"><span style="color: #006600; font-weight: bold;">base</span></a>=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/log10"><span style="color: #003399; font-weight: bold;">log10</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># equivalente</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> b=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># argumentos abreviados</span>
<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># argumentos por posición</span>
x<-<a href="http://inside-r.org/r-doc/base/log"><span style="color: #003399; font-weight: bold;">log</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># generación de objetos</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># 2. R como lenguaje: objetos en R #</span>
<span style="color: #666666; font-style: italic;">###########################################################################</span>
<span style="color: #666666; font-style: italic;"># Creación de objetos y funciones básicas</span>
<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x<-<a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x <span style="color: #666666; font-style: italic;"># equivalente a print(x)</span>
<a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/summary"><span style="color: #003399; font-weight: bold;">summary</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Tipos de objetos: vector, array, matriz, factor, lista, hoja de datos, función</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/utils/str"><span style="color: #003399; font-weight: bold;">str</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Vector: colección ordenada de elementos del mismo tipo</span>
x<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span> <span style="color: #339933;">;</span> y<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span> <span style="color: blue;">"b"</span><span style="color: #339933;">,</span> <span style="color: blue;">"c"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#concatenación "c"</span>
z<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/is.vector"><span style="color: #003399; font-weight: bold;">is.vector</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.vector"><span style="color: #003399; font-weight: bold;">is.vector</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.vector"><span style="color: #003399; font-weight: bold;">is.vector</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #339933;">;</span> y<span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> z<span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;">#cómo acceder a los elementos del vector</span>
<span style="color: #666666; font-style: italic;"># Matrices y Array: generalización multidimensional del vector. Elementos del mismo tipo. Varias columnas (o filas) de vectores</span>
<a href="http://inside-r.org/r-doc/base/is.array"><span style="color: #003399; font-weight: bold;">is.array</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
a<-<a href="http://inside-r.org/r-doc/base/array"><span style="color: #003399; font-weight: bold;">array</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a>=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> a<span style="color: #339933;">;</span> a<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span> a<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,,</span><a href="http://inside-r.org/r-doc/base/drop"><span style="color: #003399; font-weight: bold;">drop</span></a>=<span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">]</span>
b<-<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/nrow"><span style="color: #003399; font-weight: bold;">nrow</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> b<span style="color: #339933;">;</span> b<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;"># Data frame: similar al array, pero con columnas de diferentes tipos.</span>
dates<-<a href="http://inside-r.org/r-doc/base/data.frame"><span style="color: #003399; font-weight: bold;">data.frame</span></a><span style="color: #009900;">(</span>ID=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"gen0"</span><span style="color: #339933;">,</span><span style="color: blue;">"genB"</span><span style="color: #339933;">,</span><span style="color: blue;">"genZ"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>subj1=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">33</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>subj2=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">34</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>oncogen=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>loc=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">125</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a> <span style="color: #666666; font-style: italic;"># carga la hoja de datos "iris"</span>
<span style="color: #666666; font-style: italic;"># Factor: tipo de vector para datos cualitativos (o categóricos)</span>
x<-<a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># List: vector generalizado. Sus componentes pueden ser también listas, o pueden ser de distinto tipo. No tienen estructura.</span>
una.lista<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>un.vector=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>una.palabra=<span style="color: blue;">"hola"</span><span style="color: #339933;">,</span>una.matriz=<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>lista2=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>=<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span>b=<a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"b"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Funciones: código</span>
f1<-<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #009900;">{</span><a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*x<span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.function"><span style="color: #003399; font-weight: bold;">is.function</span></a><span style="color: #009900;">(</span>f1<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Atributos de los objetos</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">15</span><span style="color: #339933;">;</span> y<-<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> z<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: black; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
w<-<a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>a=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>b=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>w<span style="color: #009900;">)</span>
f1<-<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #009900;">{</span><a href="http://inside-r.org/r-doc/base/return"><span style="color: #003399; font-weight: bold;">return</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span>*x<span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/attributes"><span style="color: #003399; font-weight: bold;">attributes</span></a><span style="color: #009900;">(</span>f1<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.function"><span style="color: #003399; font-weight: bold;">is.function</span></a><span style="color: #009900;">(</span>f1<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Modo (mode): lógico, entero, real, carácter, etc.</span>
<a href="http://inside-r.org/r-doc/base/mode"><span style="color: #003399; font-weight: bold;">mode</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/mode"><span style="color: #003399; font-weight: bold;">mode</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/mode"><span style="color: #003399; font-weight: bold;">mode</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Tipo (typeof): double, character...</span>
x1<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> x2<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x3<-<span style="color: blue;">"patata"</span>
<a href="http://inside-r.org/r-doc/base/typeof"><span style="color: #003399; font-weight: bold;">typeof</span></a><span style="color: #009900;">(</span>x1<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/typeof"><span style="color: #003399; font-weight: bold;">typeof</span></a><span style="color: #009900;">(</span>x2<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/typeof"><span style="color: #003399; font-weight: bold;">typeof</span></a><span style="color: #009900;">(</span>x3<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Nombres (names): etiquetas.</span>
<span style="color: #666666; font-style: italic;">#Poner un nombre en minúscula no es lo mismo que ponerlo en mayúscula (R es "case-sensitive": x!=X)</span>
<span style="color: #666666; font-style: italic;">#Hay nombres reservados, por lo que hay que evitarlos. Ej: "function", "if"</span>
z <- <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>a=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> b=<span style="color: blue;">"c"</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a>=<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>z<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Dimensiones (dim): número de filas y columnas (puede ser cero)</span>
<a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/dim"><span style="color: #003399; font-weight: bold;">dim</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Nombres de las dimensiones de los arrays (dimnames)</span>
y<-<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/dimnames"><span style="color: #003399; font-weight: bold;">dimnames</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"8am"</span><span style="color: #339933;">,</span><span style="color: blue;">"9am"</span><span style="color: #339933;">,</span><span style="color: blue;">"10am"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># [[1]] para filas y [[2]] para columnas</span>
<a href="http://inside-r.org/r-doc/base/colnames"><span style="color: #003399; font-weight: bold;">colnames</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"lunes"</span><span style="color: #339933;">,</span> <span style="color: blue;">"martes"</span><span style="color: #339933;">,</span> <span style="color: blue;">"miércoles"</span><span style="color: #339933;">,</span> <span style="color: blue;">"jueves"</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># alternativa</span>
<span style="color: #666666; font-style: italic;"># Clase (class): lista de las clases del objeto (vector alfa-numérico)</span>
x <- <span style="color: #cc66cc;">10</span>
<a href="http://inside-r.org/r-doc/base/class"><span style="color: #003399; font-weight: bold;">class</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># "numeric"</span>
<span style="color: #666666; font-style: italic;"># Otros: por ejemplo, para series temporales.</span>
<span style="color: #666666; font-style: italic;">### Indexación o referenciación</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> x<span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span>
x<span style="color: #009900;">[</span>x><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span>
y<-x><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> x<span style="color: #009900;">[</span>y<span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/base/names"><span style="color: #003399; font-weight: bold;">names</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"a"</span><span style="color: #339933;">,</span><span style="color: blue;">"b"</span><span style="color: #339933;">,</span><span style="color: blue;">"c"</span><span style="color: #339933;">,</span><span style="color: blue;">"d"</span><span style="color: #339933;">,</span><span style="color: blue;">"e"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> x<span style="color: #009900;">[</span><span style="color: blue;">"c"</span><span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/utils/data"><span style="color: #003399; font-weight: bold;">data</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a>
<a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #666666; font-style: italic;"># igual que iris[[1]]</span>
<a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span>:<span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">2</span><span style="color: #009900;">]</span>
<a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a>$Sepal.Length <span style="color: #666666; font-style: italic;"># referencia a la columna "Sepal.Length" utilizando el formato dataframe$columna</span>
<a href="http://inside-r.org/r-doc/base/attach"><span style="color: #003399; font-weight: bold;">attach</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>Sepal.Length<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/detach"><span style="color: #003399; font-weight: bold;">detach</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/iris"><span style="color: #003399; font-weight: bold;">iris</span></a><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># referencia a los nombres de las variables (columnas) mediante la función attach</span>
<span style="color: #666666; font-style: italic;">### Valores ausentes: NA (not available); NaN (not a number); Inf (infinito)</span>
x<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/which"><span style="color: #003399; font-weight: bold;">which</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">]</span><-<span style="color: #cc66cc;">0</span> <span style="color: #666666; font-style: italic;">#sustituir NA por 0</span>
<a href="http://inside-r.org/r-doc/base/is.na"><span style="color: #003399; font-weight: bold;">is.na</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span>/<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/is.nan"><span style="color: #003399; font-weight: bold;">is.nan</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span>/<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/is.infinite"><span style="color: #003399; font-weight: bold;">is.infinite</span></a><span style="color: #009900;">(</span>-<span style="color: #cc66cc;">5</span>/<span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#quitar NA al evaluar la media</span>
x<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: black; font-weight: bold;">NA</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> y<-<a href="http://inside-r.org/r-doc/stats/na.omit"><span style="color: #003399; font-weight: bold;">na.omit</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> y <span style="color: #666666; font-style: italic;">#para funciones de modelado como lm es mejor usar "na.omit" o "na.exclude", que eliminan los NA</span>
<span style="color: #666666; font-style: italic;">### Ordenar vectores</span>
x<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>decreasing=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/sort"><span style="color: #003399; font-weight: bold;">sort</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/rank"><span style="color: #003399; font-weight: bold;">rank</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span>
x<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #009900;">]</span>
x<span style="color: #009900;">[</span><a href="http://inside-r.org/r-doc/base/order"><span style="color: #003399; font-weight: bold;">order</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span>decreasing=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span>
<span style="color: #666666; font-style: italic;">### Generación de secuencias</span>
<span style="color: #666666; font-style: italic;"># Secuencias aleatorias</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>sample<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span>
probs<-x/sum<span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">;</span>sample<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><a href="http://inside-r.org/packages/cran/prob">prob</a>=probs<span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Secuencias discretas y continuas</span>
x<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span>
x<-<a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span>from=<span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>to=<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/by"><span style="color: #003399; font-weight: bold;">by</span></a>=<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># Secuencias con repeticiones</span>
<a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/rep"><span style="color: #003399; font-weight: bold;">rep</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/gl"><span style="color: #003399; font-weight: bold;">gl</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#como rep(1:3, rep(5,3))</span>
<a href="http://inside-r.org/r-doc/base/expand.grid"><span style="color: #003399; font-weight: bold;">expand.grid</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span>:<span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">7</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Vector de caracteres</span>
pegar<-<a href="http://inside-r.org/r-doc/base/paste"><span style="color: #003399; font-weight: bold;">paste</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: blue;">"A"</span><span style="color: #339933;">,</span><span style="color: blue;">"B"</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span>:<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>sep=<span style="color: blue;">""</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> pegar
<a href="http://inside-r.org/r-doc/base/substr"><span style="color: #003399; font-weight: bold;">substr</span></a><span style="color: #009900;">(</span><span style="color: blue;">"abcdef"</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span>
y<-<a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> y
x <- <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/methods/as"><span style="color: #003399; font-weight: bold;">as</span></a> = <span style="color: blue;">"asfef"</span><span style="color: #339933;">,</span> qu = <span style="color: blue;">"qwerty"</span><span style="color: #339933;">,</span> <span style="color: blue;">"yuiop["</span><span style="color: #339933;">,</span> <span style="color: blue;">"b"</span><span style="color: #339933;">,</span> <span style="color: blue;">"stuff.blah.yech"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># split x on the letter e</span>
<a href="http://inside-r.org/r-doc/base/strsplit"><span style="color: #003399; font-weight: bold;">strsplit</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <span style="color: blue;">"e"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Discretizar</span>
v<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">9</span>
cut1<-<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> cut1
cut2<-<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>include.lowest=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> cut2
cut3<-<a href="http://inside-r.org/r-doc/base/cut"><span style="color: #003399; font-weight: bold;">cut</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/quantile"><span style="color: #003399; font-weight: bold;">quantile</span></a><span style="color: #009900;">(</span>v<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span>/<span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>include.lowest=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> cut3
<span style="color: #666666; font-style: italic;">### Combinar vectores o matrices</span>
<a href="http://inside-r.org/r-doc/base/rbind"><span style="color: #003399; font-weight: bold;">rbind</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/cbind"><span style="color: #003399; font-weight: bold;">cbind</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span>
a<-<a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span>
mediai<-media<-NULL<span style="color: #339933;">;</span>for<span style="color: #009900;">(</span>i <span style="color: black; font-weight: bold;">in</span> <span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">4</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>mediai<-<a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>a<span style="color: #009900;">[</span>i<span style="color: #339933;">,</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> media<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>media<span style="color: #339933;">,</span>mediai<span style="color: #009900;">)</span><span style="color: #009900;">}</span> <span style="color: #666666; font-style: italic;"># loop</span>
<a href="http://inside-r.org/r-doc/base/apply"><span style="color: #003399; font-weight: bold;">apply</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># aplica una función a filas, columnas o ambas. Hay que especificar el "margin"</span>
<a href="http://inside-r.org/r-doc/base/sapply"><span style="color: #003399; font-weight: bold;">sapply</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># simplifica el resultado a un vector o una matriz</span>
<a href="http://inside-r.org/r-doc/base/lapply"><span style="color: #003399; font-weight: bold;">lapply</span></a><span style="color: #009900;">(</span>a<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #339933;">,</span>na.rm=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;"># devuelve una lista</span>
<a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span><span style="color: #339933;">;</span> groups <- <a href="http://inside-r.org/r-doc/base/as.factor"><span style="color: #003399; font-weight: bold;">as.factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rbinom"><span style="color: #003399; font-weight: bold;">rbinom</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">32</span><span style="color: #339933;">,</span> n = <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/packages/cran/prob">prob</a> = <span style="color: #cc66cc;">0.4</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/tapply"><span style="color: #003399; font-weight: bold;">tapply</span></a><span style="color: #009900;">(</span>groups<span style="color: #339933;">,</span> groups<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/stats/aggregate"><span style="color: #003399; font-weight: bold;">aggregate</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/datasets/state.x77"><span style="color: #003399; font-weight: bold;">state.x77</span></a><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/list"><span style="color: #003399; font-weight: bold;">list</span></a><span style="color: #009900;">(</span>Region = <a href="http://inside-r.org/r-doc/datasets/state.region"><span style="color: #003399; font-weight: bold;">state.region</span></a><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">### Programar en R</span>
<span style="color: #666666; font-style: italic;">#Definición de funciones</span>
funcion1<-<a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>A<span style="color: #339933;">,</span>B<span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/stats/C"><span style="color: #003399; font-weight: bold;">C</span></a><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>x1<-A*B/C<span style="color: #339933;">;</span> x1<span style="color: #009900;">}</span>
<span style="color: #666666; font-style: italic;">#Argumentos</span>
<a href="http://inside-r.org/r-doc/base/args"><span style="color: #003399; font-weight: bold;">args</span></a><span style="color: #009900;">(</span>funcion1<span style="color: #009900;">)</span>
funcion1<span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Control de ejecución: condicionales, loops</span>
f = <a href="http://inside-r.org/r-doc/base/factor"><span style="color: #003399; font-weight: bold;">factor</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><span style="color: black; font-weight: bold;">letters</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: black; font-weight: bold;">TRUE</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">for</span><span style="color: #009900;">(</span> i <span style="color: black; font-weight: bold;">in</span> <a href="http://inside-r.org/r-doc/base/unique"><span style="color: #003399; font-weight: bold;">unique</span></a><span style="color: #009900;">(</span>f<span style="color: #009900;">)</span> <span style="color: #009900;">)</span> <a href="http://inside-r.org/r-doc/base/print"><span style="color: #003399; font-weight: bold;">print</span></a><span style="color: #009900;">(</span>i<span style="color: #009900;">)</span>
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: black; font-weight: bold;">if</span><span style="color: #009900;">(</span>x==<span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>x<-x+<span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: black; font-weight: bold;">else</span> <span style="color: #009900;">{</span>x<-x+<span style="color: #cc66cc;">2</span><span style="color: #009900;">}</span><span style="color: #339933;">;</span> x
x<-<span style="color: #cc66cc;">1</span>:<span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <a href="http://inside-r.org/r-doc/base/ifelse"><span style="color: #003399; font-weight: bold;">ifelse</span></a><span style="color: #009900;">(</span>x %% <span style="color: #cc66cc;">2</span> == <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: blue;">"Odd"</span><span style="color: #339933;">,</span> <span style="color: blue;">"Even"</span><span style="color: #009900;">)</span>
<span style="color: black; font-weight: bold;">while</span><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><=<span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>x<-<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #009900;">}</span>
<a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats"><span style="color: #006600; font-weight: bold;">stats</span></a><span style="color: #009900;">)</span>
centre <- <a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> type<span style="color: #009900;">)</span>
<span style="color: #009900;">{</span>
<a href="http://inside-r.org/r-doc/base/switch"><span style="color: #003399; font-weight: bold;">switch</span></a><span style="color: #009900;">(</span>type<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a> = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a> = <a href="http://inside-r.org/r-doc/stats/median"><span style="color: #003399; font-weight: bold;">median</span></a><span style="color: #009900;">(</span>x<span style="color: #009900;">)</span><span style="color: #339933;">,</span> trimmed = <a href="http://inside-r.org/r-doc/base/mean"><span style="color: #003399; font-weight: bold;">mean</span></a><span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> trim = <span style="color: #cc66cc;">.1</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #009900;">}</span>
x <- <a href="http://inside-r.org/r-doc/stats/rcauchy"><span style="color: #003399; font-weight: bold;">rcauchy</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> centre<span style="color: #009900;">(</span>x<span style="color: #339933;">,</span> <span style="color: blue;">"mean"</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Control de errores: traceback, browser, debug</span>
<a href="http://inside-r.org/r-doc/base/debug"><span style="color: #003399; font-weight: bold;">debug</span></a><span style="color: #009900;">(</span>centre<span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/base/undebug"><span style="color: #003399; font-weight: bold;">undebug</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;">#Control de la duración de la operación:</span>
<a href="http://inside-r.org/r-doc/base/unix.time"><span style="color: #003399; font-weight: bold;">unix.time</span></a><span style="color: #009900;">(</span>centre<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span><span style="color: blue;">"mean"</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #666666; font-style: italic;">#unix.time, Rprof</span>
<a href="http://inside-r.org/r-doc/base/traceback"><span style="color: #003399; font-weight: bold;">traceback</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
</pre>
</div>
</div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-43723862805728353722011-09-29T05:16:00.000-07:002011-09-29T05:16:24.130-07:00Breve introducción a R. (I)<br />
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 26.0pt; mso-bidi-font-size: 11.0pt;">¿Qué es R?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";"><a href="http://www.r-project.org/">http://www.r-project.org/</a><b><o:p></o:p></b></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.r-project.org/hpgraphic.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="213" src="http://www.r-project.org/hpgraphic.png" width="320" /></a></div>
<div class="MsoNormal" style="line-height: normal;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">R
es un lenguaje y entorno (o ambiente) con un conjunto integrado de programas
para la manipulación de datos, cálculo y gráficos.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">R
es un proyecto GNU similar al lenguaje S (J. Chambers et al., Bell Labs.), pero
que s</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">e distribuye bajo la General
Public Licen</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">se (GPL, <i>Open Sourse</i>). </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">Esta
licencia no tiene restricciones de uso,</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"> sólo obliga a
que la distribución sea siempre GPL.<o:p></o:p></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTYDwdi_usNepyYqPjyLFXsOzL9BWjQ72D0D040P1OqvLIDXbqftLgYNC2ThKr8WOGOwzQcYpGx-WqshjVri8yCQumenVDia7xIQOZHr5CgYi-OmLlivzKe2eN47T2XY3HZqLjqUczQyQ/s1600/07program.600.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="160" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTYDwdi_usNepyYqPjyLFXsOzL9BWjQ72D0D040P1OqvLIDXbqftLgYNC2ThKr8WOGOwzQcYpGx-WqshjVri8yCQumenVDia7xIQOZHr5CgYi-OmLlivzKe2eN47T2XY3HZqLjqUczQyQ/s320/07program.600.jpg" width="320" /></a></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 9.0pt; mso-bidi-font-size: 11.0pt;">Robert Gentleman
y Ross Ihaka (también conocidos como “<i>R
& R</i>”) liberaron el código R como software libre en 1996. </span><span style="font-family: "Verdana","sans-serif";"><a href="http://www.nytimes.com/2009/01/07/technology/business-computing/07program.html?pagewanted=all"><span style="font-size: 9.0pt; mso-bidi-font-size: 11.0pt;">http://www.nytimes.com/2009/01/07/technology/business-computing/07program.html?pagewanted=all</span></a></span><span style="font-family: "Verdana","sans-serif"; font-size: 9.0pt; mso-bidi-font-size: 11.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Permite<b> </b></span><span style="font-family: "Verdana","sans-serif";"><o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo1; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Almacenamiento y manipulación
efectiva de los datos.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Operadores para cálculo sobre
variables indexadas (arrays) y matrices.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Amplia, coherente e integrada
colección de herramientas para análisis de datos.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">G</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">randes
posibilidades graficas.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo1; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Lenguaje de programación orientado a
objetos bien desarrollado, simple y efectivo.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Ventajas<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Arial; mso-bidi-font-size: 11.0pt;">Es un
ambiente o entorno de trabajo, es decir, presenta herramientas muy flexibles
que pueden extenderse fácilmente mediante paquetes (<i>packages; </i></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><a href="file:///E:/ROSANA/Rosana/Documents/clases%20particulares/R/R%20curso%20Rosana/%20http:/cran.r-project.org/mirrors.html"><span>http://cran.r-project.org/mirrors.html</span></a></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Arial; mso-bidi-font-size: 11.0pt;">) y permite crear nuestras propias funciones.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">Gráficos
de alta calidad exportables en diversos </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">formatos:
PostScript, pdf, bitmap, pictex (LaTeX), png, jpeg, etc.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">Comunidad
muy dinámica de desarrolladores de </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">paquetes que
cuenta con estadísticos de prestigio<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">Lenguaje
orientado a objetos</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">Consume
pocos recursos informáticos</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt;">Puede
ejecutarse remotamente (telnet</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt;">Obtención e instalación de R<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Depende del
sistema operativo, pero todo se puede encontrar en
http://cran.r-project.org/bin.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l6 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l6 level1 lfo5; text-indent: -18.0pt;">
<!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"> Windows: bajar
("download") el ejecutable desde
http://cran.r-project.org/bin/windows/base. (por ejemplo, http://cran.r-project.org/bin/windows/base/rw1070.exe).<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level2 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Ejecutar
el fichero. Instalará el sistema base y los paquetes recomendados.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l6 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">GNU/Linux: dos opciones:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level2 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Obtener
el R-x.y.z.tar.gz y compilar desde las fuentes, y también bajar los paquetes
adicionales e instalar. (Buena forma de comprobar que el sistema tiene
development tools).<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level2 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Obtener
binarios (ej., *.deb para Debian, *.rpm para RedHat, SuSE, Mandrake).<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt;">¿Cómo trabaja y cómo debo utilizarlo?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Windows:
<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l3 level1 lfo6; text-align: justify; text-indent: -18.0pt;">
</div>
<ul>
<li><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Hacer
click dos veces en el icono. Se abrirá "Rgui".</span></li>
<li><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Iniciar
R desde Tinn-R.</span></li>
<li><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Desde
una "ventana del sistema" ejecutar "Rterm"; parecido a
"R" en Unix o Linux.</span></li>
</ul>
<!--[if !supportLists]--><br />
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">GNU/Linux:
<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l2 level1 lfo7; text-align: justify; text-indent: -18.0pt;">
</div>
<ul>
<li><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span><span style="font: 7.0pt "Times New Roman";"> </span></span></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Teclear
"R" en una shell.</span></li>
<li><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span><span style="font: 7.0pt "Times New Roman";"> </span></span></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Iniciar
R desde Tinn-R.</span></li>
</ul>
<!--[if !supportLists]--><br />
<div class="MsoNormal" style="line-height: normal; tab-stops: 52.65pt; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">- R es un lenguaje Orientado a Objetos: significa
que las variables, datos, funciones, resultados, etc., se guardan en la memoria
activa del computador en forma de objetos con un nombre específico. El usuario
puede modificar o manipular estos objetos con operadores (aritméticos, lógicos,
y comparativos) y funciones (que a su vez son objetos).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; tab-stops: 52.65pt; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">Mientras que programas más clásicos muestran
directamente los resultados de un análisis, R guarda estos resultados como un
“objeto”, de tal manera que se puede hacer un análisis sin necesidad de mostrar
su resultado inmediatamente. Esto puede ser un poco extraño para el usuario,
pero esta característica suele ser muy útil.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; tab-stops: 52.65pt; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">Otras características de los lenguajes
orientados a objetos son la herencia: las subclases heredan las características
de las superclases, y el polimorfismo la misma operación aplicada a diferentes
objetos resulta en diferentes implementaciones.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; tab-stops: 52.65pt; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">- R es un lenguaje interpretado (como Java) y
no compilado (como Fortran, Pascal, . . . ), lo cual significa que los comandos
escritos en el teclado son ejecutados directamente sin necesidad de construir
ejecutable<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Sistema de
ventanas <o:p></o:p></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://imagenes.sftcdn.net/es/scrn/69000/69379/r-project-30.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="213" src="http://imagenes.sftcdn.net/es/scrn/69000/69379/r-project-30.jpg" width="320" /></a></div>
<div class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;"><span class="Apple-style-span" style="font-size: 13px;">MacOS X RAqua desktop</span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://imagenes.sftcdn.net/es/scrn/69000/69379/r-project-34.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="237" src="http://imagenes.sftcdn.net/es/scrn/69000/69379/r-project-34.jpg" width="320" /></a></div>
<div class="MsoNormal" style="line-height: normal; text-align: center;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 13px;">Unix
desktop</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://ibxk.com.br/57974/46125.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="249" src="http://ibxk.com.br/57974/46125.jpg" width="320" /></a></div>
<div class="MsoNormal" style="line-height: normal; text-align: center;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 13px;">Windows desktop</span></div>
<div align="center" class="MsoNormal" style="background: white; line-height: normal; mso-margin-top-alt: auto; mso-outline-level: 1; text-align: center;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="font-size: 11px;"><b><br /></b></span></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: ES;">Al hacer doble click en el ícono se abre una consola de
línea de comandos, la “Gui” (Graphical User Interface), con un mensaje de
inicio.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">R y los editores de textos <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 14.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 14.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">Tinn-R </span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-themecolor: text1;">Tinn-R (Tinn
Is Not Notepad-R) es un editor de código gratuito que se puede utilizar con R,
pero también está preparado para escribir código en C++, HTML, Java y otros
lenguajes populares.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span lang="EN-US" style="color: black; font-family: Verdana, sans-serif;"><span class="Apple-style-span" style="font-size: x-small;">Está d</span></span><span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 13px;">istribuido bajo licencia GNU, y trabaja tanto en Windows (9X/Me/2000/XP) como en los demás sistemas operativos.</span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;">A diferencia de Notepad permite resaltar la
sintaxis de R (en archivos *.R, *.r, *.Q o *.q), lo cual da mayor claridad al
escribir el código. También permite la numeración de renglones, la ayuda
contextual y el envío de código al entorno de R. </span><span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 13px;">Disponible en: <a href="http://www.sciviews.org/Tinn-R/">http://www.sciviews.org/Tinn-R/</a>.</span></div>
<div class="MsoNormal" style="line-height: normal;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 13px;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://sciviews.org/Tinn-R/Tinn-R.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="230" src="http://sciviews.org/Tinn-R/Tinn-R.jpg" width="320" /></a></div>
<div class="MsoNormal" style="line-height: normal;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;"><b><br /></b></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">Podemos modificar los comandos rápidos en
el apartado R>Hotkeys. <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">Seleccionamos CTRL y Key ponemos R<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">Lo ubicamos en Selection<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">y decimos que Add<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">Y lo activamos<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;"><o:p><br /></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;"><o:p>RStudio</o:p></span></div>
<div class="MsoNormal">
<o:p><span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;"></span></o:p></div>
<div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;">RStudio es otro editor para scripts disponible para usuarios de R en todos los sistemas operativos.</span></div>
<div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;">Los autores lo consideran un entornor de desarrollo integrado que combina un interfaz muy intuitiva con herramientas de código muy potentes que permiten sacar el máximo provecho a R.</span></div>
<div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;">La versión en pruebas es gratuita y está disponible en <a href="http://www.rstudio.org/">http://www.rstudio.org/</a>.</span></div>
<div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;">Como los anteriores la ventaja de este editor es que ofrece una serie de opciones no existentes en R, entre otras, por ejemplo, comprobar rápidamente que ningún paréntesis queda sin cerrarse o marcar, copiar y pegar columnas.</span></div>
<div class="MsoNormal">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: x-small;">Pero además nos da un listado de las variables y nos da una descripción de los bancos de datos que hemos introducido. También tiene una lista de los paquetes instalados y los gráfi cos realizados.</span></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.r-bloggers.com/wp-content/uploads/2011/02/rstudio-windows2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="http://www.r-bloggers.com/wp-content/uploads/2011/02/rstudio-windows2.png" width="320" /></a></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;"><o:p><br /></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Sintaxis básica
de R<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">> R cursor (prompt): símbolo del
sistema.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">+ Continuartion promt: indica una orden
incompleta.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">; separa órdenes en la misma línea.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">, separa argumentos entre paréntesis.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;"># para realizar comentarios.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">q() # para
salir del programa.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;"><- o = operador para la asignación de
objetos.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">El operador “asignar” permite crear objetos en R. El
nombre de un objeto debe:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">- comenzar con una letra (A-Z y a-z).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">- puede incluir letras, dígitos (0-9), y puntos (.).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">- R discrimina entre letras mayúsculas y minúsculas para
el nombre de un objeto, de tal manera que x y X se refiere a objetos
diferentes.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">- R no interpreta los espacios excepto para la asignación
(bien: <- , mal: < -)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">Todos los objetos creados se guardan en el workspace o
área de trabajo. Para ver qué objetos están en el workspace se usa la función
ls().<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">ls() relación de objetos disponibles en el
entorno (.Englov)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;"><span> </span>ls(package=__) ó (library:__) relación de
objetos disponibles en la librería_ <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;"><span> </span>rm(__)
para borrar objetos __. rm(list=ls()) borra todos.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">Guardar un
archivo<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; tab-stops: 52.65pt;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">-.RData<span> </span> #
archivo de objetos<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">-.RHistory
<span> </span>#archivo de historia de órdenes<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">-guardar u
recuperar el trabajo de una sesión: Savehistory(), loadhistory()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">-directorio
de trabajo: getwd(), setwd()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">-Instalar
y activar (loading) los paquetes:</span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;"> update.packages(), <span style="color: black; mso-themecolor: text1;">install.packages(); library(); require()<o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="color: black; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-themecolor: text1;">-Cambios
en la consola de R: Edit-> GUI preferences<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialMT; mso-fareast-font-family: ArialMT;">Flechas
verticales del teclado </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialUnicodeMS; mso-fareast-font-family: ArialMT;">(</span><!--[if gte msEquation 12]><m:oMath><i
style='mso-bidi-font-style:normal'><span style='font-size:10.0pt;font-family:
"Cambria Math","serif";mso-fareast-font-family:ArialMT;mso-bidi-font-family:
ArialUnicodeMS'><m:r>↑↓</m:r></span></i></m:oMath><![endif]--><!--[if !msEquation]--><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; line-height: 115%; mso-ansi-language: ES; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-text-raise: -4.0pt; position: relative; top: 4.0pt;"><!--[if gte vml 1]><v:shape
id="_x0000_i1025" type="#_x0000_t75" style='width:9pt;height:13.5pt'>
<v:imagedata src="file:///C:\Users\Rosana\AppData\Local\Temp\msohtmlclip1\01\clip_image009.png"
o:title="" chromakey="white"/>
</v:shape><![endif]--><!--[if !vml]--><img height="18" src="file:///C:/Users/Rosana/AppData/Local/Temp/msohtmlclip1/01/clip_image010.gif" v:shapes="_x0000_i1025" width="12" /><!--[endif]--></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialUnicodeMS; mso-fareast-font-family: ArialMT;">) </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialMT; mso-fareast-font-family: ArialMT;">recorren el
historial de ordenes<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialMT; mso-fareast-font-family: ArialMT;">Flechas
horizontales </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialUnicodeMS; mso-fareast-font-family: ArialMT;">(</span><!--[if gte msEquation 12]><m:oMath><i
style='mso-bidi-font-style:normal'><span style='font-size:10.0pt;font-family:
"Cambria Math","serif";mso-fareast-font-family:ArialMT;mso-bidi-font-family:
ArialUnicodeMS'><m:r>←→</m:r></span></i></m:oMath><![endif]--><!--[if !msEquation]--><span style="font-family: "Calibri","sans-serif"; font-size: 11.0pt; line-height: 115%; mso-ansi-language: ES; mso-ascii-theme-font: minor-latin; mso-bidi-font-family: "Times New Roman"; mso-bidi-language: AR-SA; mso-bidi-theme-font: minor-bidi; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-text-raise: -4.0pt; position: relative; top: 4.0pt;"><!--[if gte vml 1]><v:shape
id="_x0000_i1025" type="#_x0000_t75" style='width:16.5pt;height:13.5pt'>
<v:imagedata src="file:///C:\Users\Rosana\AppData\Local\Temp\msohtmlclip1\01\clip_image011.png"
o:title="" chromakey="white"/>
</v:shape><![endif]--><!--[if !vml]--><img height="18" src="file:///C:/Users/Rosana/AppData/Local/Temp/msohtmlclip1/01/clip_image012.gif" v:shapes="_x0000_i1025" width="22" /><!--[endif]--></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialUnicodeMS; mso-fareast-font-family: ArialMT;">) </span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: ArialMT; mso-fareast-font-family: ArialMT;">permiten el
movimiento dentro de la línea<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">attach(pepita) permite trabajar con los
elementos de pepita sin nombrar a pepita<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;"><span> </span>dattach(pepita) vuelve a unir los componentes
de pepita<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Ayuda<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";">help(summary)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";">?summary<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";">help.search(“median”)
# lista de funciones o paquetes donde aparece una palabra<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";">help.start()
# abre la ayuda en html <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";">demo()
# lista las demostraciones disponibles<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif";">demo(graphics)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; mso-bidi-font-family: Palatino-Roman;">data() relación de todas las hojas de
datos (data.frame) de todas las librerías disponibles<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; mso-bidi-font-family: Palatino-Roman;">data(package=__) relación de los
data.frame de la librería ...<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; mso-bidi-font-family: Palatino-Roman;">data(pepita) carga en la memoria en uso el
objeto pepita<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Órdenes básicas </span><span style="font-family: "Verdana","sans-serif";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">-R
como calculadora: aritmética y funciones básicas<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l4 level1 lfo3; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 10.0pt; mso-ascii-font-family: Verdana; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-hansi-font-family: Calibri;"></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">R puede operar sobre vectores enteros de un
golpe<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l4 level1 lfo3; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 10.0pt; mso-ascii-font-family: Verdana; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-hansi-font-family: Calibri;"></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Regla del reciclado<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l4 level1 lfo3; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 10.0pt; mso-ascii-font-family: Verdana; mso-bidi-font-family: Calibri; mso-bidi-font-size: 11.0pt; mso-hansi-font-family: Calibri;"></span><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Operaciones aritméticas +, -, *, /, ^, %%,
%/% ,sqrt, log, log10, log2, log(x, base), exp, sin, cos, tan<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l4 level1 lfo3; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><span> </span>Otras operaciones con vectores: max, min,
range, mean, var, sum, prod, which.max, which.min, cumsum, cumprod, …<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l4 level1 lfo3; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><span> </span>Operaciones comparativas y lógicas: <,
>, <=, >=, ==, ¡= , ¡, &, |, xor() y los parecidos &&, ||<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l4 level1 lfo3; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><span> </span>Generacion de secuencias regulares: seq, :,
rep<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">-R
como lenguaje: objetos<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l5 level1 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Tipos de objetos: vectores, arrays,
matrices, listas, data.frames, factores. Modos y atributos.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; text-align: justify;">
<br /></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt;">Citando R<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">citation():
Muestra los datos de R y como éste ha de ser citado.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">citation("pkgname"):
Indica cómo se han de citar los paquetes empleados.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Documentación
sobre R<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">•Michael
J. Crawley. </span><span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">2007. The R
Book. John Wiley & Sons Ltd,<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">•Michael J. Crawley. 2005. Statistics: An Introduction
using R. Wiley. ISBN 0-470-02297-3.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">•John Verzani. 2005. Using R for Introductory
Statistics. Chapman & Hall/CRC, Boca Raton, FL, ISBN 1-584-88450-9.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Para
ver más: http://www.r-project.org/doc/bib/R-books.html<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 16.0pt; mso-bidi-font-size: 11.0pt;">Trabajar con los
datos</span><span style="font-family: "Verdana","sans-serif";"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">-Importando
y exportando datos<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l5 level1 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Formato .Rdata. Formato .RData: <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level2 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Guardar:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 108.0pt; mso-add-space: auto; mso-list: l5 level3 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Wingdings; font-size: 10.0pt; mso-bidi-font-family: Wingdings; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Wingdings;"><span>§<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;"><span> </span>save(__, __ , file=”___.Rdata”)<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 108.0pt; mso-add-space: auto; mso-list: l5 level3 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Wingdings; font-size: 10.0pt; mso-bidi-font-family: Wingdings; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Wingdings;"><span>§<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">save.image(file=”___.RData”)
guarda todo el directorio de trabajo<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level2 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Recuperar:
load(“__.RData”)<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l5 level1 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Formato ASCCI. Archivos .r<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level2 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">Guardar: dump(“__“, “__“, ”___.r”, append=_T ó F_)<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level2 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "Courier New"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Recuperar:
source(“__.r”) ejecuta las órdenes de R que están en el archivo __.r<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l5 level1 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Formato ASCCI. Archivos .txt (hojas
de datos)<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level2 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">Guardar: write.table(_data.frame_, ”___.txt”, sep=”\t”, quote=F)<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level2 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Courier New";"><span>o<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">Recuperar: read.table(“__.txt”, header=T, , sep=”\t”)<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l5 level1 lfo4; text-align: justify; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-family: Symbol; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: Symbol;"><span>·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span lang="EN-US" style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-ansi-language: EN-US; mso-bidi-font-size: 11.0pt;">Desde internet: Dataweb02 <-
read.table(url("http://tinyurl.com/lzco8s"), row.names=1, dec=",")<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-align: justify; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; mso-layout-grid-align: none; text-align: justify; text-autospace: none;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: Palatino-Roman;">-Siempre las tablas de datos deberán tener
las variables en columnas y las observaciones en filas.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;">-Eliminar todos los
espacios no necesarios.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;">-Poner nombres
cortos tanto en variables como en observaciones. En estas últimas, todas
deberán ser del mismo tamaño.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;">-Eliminar todo
símbolo inútil (@, ·, \, #, &, %, $,…)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;">-Comprobar que no
hay celdas vacías. Para asegurarnos eliminar todas las filas y columnas
sobrantes.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-align: justify;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt;">Los datos que
tienes en excel han de ser guardados en documentos de texto delimitados por
tabulaciones. Y los huecos deben ser rellenados con NA (No Available) en
mayúsculas.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">Lo que sucede es
que en ocasiones lo reconoce como una matriz en lugar de como un data frame,
por lo que si lo detectas al hacer un str(data) pues tienes que decirle que se
trata de un data frame para ello:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-size: 11.0pt;">data01<-as.data.frame(data01)
# se le dice que considere a data como un data.frame</span></div>
<div class="MsoNormal" style="line-height: normal;">
<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 13px;">data01$var01<-as.factor(data01$var01)<span> </span># estamos diciendo que considere la variable01
como un factor de la variable01</span></div>
<br />
Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-90526240444654052422011-06-05T07:21:00.000-07:002011-06-05T07:21:46.951-07:00Ecoinformatics Tools & Products | NCEAS<a href="http://www.nceas.ucsb.edu/ecoinfo/tools">Ecoinformatics Tools & Products | NCEAS</a><br /><br /><h1 class="title">Ecoinformatics Tools & Products</h1> <div class="content clearfix"> <p>NCEAS collaborative research efforts have produced a variety of software tools and products that assist scientists in the creation, management, access, and analysis of ecological data. These tools can be classified into five categories. The <a href="http://www.ecoinformatics.org/">ecoinformatics.org</a> web site provides further details, including download instructions, where appropriate, for each tool. All the software tools that NCEAS has developed are free for download, typically run on at least several popular computing platforms, and utilize best of class open source software frameworks when appropriate. </p><h3>Analytical And Modeling Tools</h3><p><a name="kepler" title="kepler"></a><a href="http://kepler.ecoinformatics.org/">Kepler</a> is an open-source scientific workflow system that allows scientists to design scientific workflows and execute them efficiently using emerging Grid-based approaches to distributed computation. Kepler facilitates access to a broad range of ecologically relevant data via the <a href="http://seek.ecoinformatics.org/Wiki.jsp?page=EcoGrid">EcoGrid</a>, while also providing a basis for sharing analyses through a growing library of executable components and workflows. <a href="http://www.kepler-project.org/Wiki.jsp?page=KeplerCOREAnnouncement">Kepler/CORE</a> moves Kepler from a research prototype to a reliable and easily extensible system. </p><h3>Metadata Specifications</h3><p><a href="http://knb.ecoinformatics.org/software/eml/">Ecological Metadata Language (EML)</a> is a metadata specification developed by the ecology discipline and for the ecology discipline. EML is implemented as a series of XML document types that can be used in a modular and extensible manner to document ecological data. Each EML module is designed to describe one logical part of the total metadata that should be included with any ecological dataset. It is likely that EML will be useful as a Metadata Language outside of the ecological discipline since, despite its intended relevance for ecological data, it does not contain any metadata structures that are strictly ecological. Rather, the metadata structures in EML all have broader applicability. </p><h3>Metadata Entry Tools and Editors</h3><p><a href="http://knb.ecoinformatics.org/morphoportal.jsp">Morpho</a> allows users to create and manage EML metadata and to share those metadata, and their associated data, with others. It was created to provide an easy-to-use, cross-platform application for accessing and manipulating metadata and data both locally and on the network through powerful connections with <a href="http://www.nceas.ucsb.edu/ecoinfo/tools#meta">Metacats</a> . </p><p><a href="http://jalama.ecoinformatics.org/">Jalama</a> is a (research prototype) system that can be used for developing metadata and data entry forms, based on rich metadata descriptions, for use in lab and field environments for ecology. </p><h3>Data Repositories & Registries<br /></h3><p><a name="meta" title="meta"></a><a href="http://knb.ecoinformatics.org/software/metacat/">Metacat</a> is a flexible metadata database architecture that utilizes XML as a common syntax for representing metadata content standards utilized by ecologists. Metacats have specialized features suitable for guaranteeing local autonomy and access control, while also affording the possibility of broad-scale replication and information sharing within the <a href="http://www.nceas.ucsb.edu/ecoinfo/tools#knbrep">KNB</a> . </p><h3>Online Databases</h3><p><a name="knbrep" title="knbrep"></a><a href="http://knb.ecoinformatics.org/">KNB Data Repository</a>: Supports the Knowledge Network for Biocomplexity, a national network that facilitates ecological and environmental research on biocomplexity.</p><p><a href="http://knb.ecoinformatics.org/knb/style/skins/nceas/">NCEAS Data Repository</a>: Contains information about research data sets collected and collated as part of NCEAS' funded activities. It is concurrently available through the <a href="http://knb.ecoinformatics.org/index.jsp">KNB Data Repository</a> .</p><p><a href="http://www.vegbank.org/">Vegbank</a>: The vegetation plot database of the Ecological Society of America's Panel on Vegetation Classification.</p><p><a href="http://www.paleodb.org/">Paleobiology Database</a>: Fossil information that includes 52,000 collection records and 511,889 taxonomic occurrences from 13,962 published references.</p><p><a href="http://www.nceas.ucsb.edu/interactionweb/">Interaction Web Database</a><strong>: </strong>Data concerning ecological interactions, particularly pollination/pollinator relationships.<br /><a href="http://www3.imperial.ac.uk/cpb/research/patternsandprocesses/gpdd"><br />Global Population Dynamics Database</a>: An extensive collection of time series data from plant and animal populations.<strong><br /></strong> </p> </div>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-20168645890338523592011-06-04T15:24:00.000-07:002011-09-29T05:37:50.361-07:00Legendre - Taichung09<a href="http://biol09.biol.umontreal.ca/Taichung09/">Legendre - Taichung09</a> Un muy buen curso dado por un grande. Material de sobra para entretenerse, espero les guste.<br />
<br />
<h1>
<center>Biostatistics Short Course</center></h1>
<h3>
<center>Department of Life Science</center></h3>
<h3>
<center>and Center for Tropical Ecology and Biodiversity</center></h3>
<h3>
<center>Tunghai University, Taichung, Taiwan</center></h3>
<h3>
<center>November 6-8 and 13-15, 2009</center></h3>
<h2>
<center>Pierre Legendre</center></h2>
<h3>
<center>Professor, Université de Montréal</center></h3>
<hr />
<strong>This page contains links to various documents and programs that will be used during the course</strong><br />
<a href="http://biol09.biol.umontreal.ca/Taichung09/Outline_Taichung_Short_course.pdf">Course outline, Taichung 2009</a><br />
<hr />
<h2>
<center>Documents in PDF format</center></h2>
<h3>
<b>Part 1: Linear model</b></h3>
<b>Day 1</b><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Chapter_4_pp_131-145.pdf"> "Multidimensional quantitative data" (Chapter extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Chapter_10_pp_497-504.pdf"> "Regression" (Chapter extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Regression_algebra.pdf"> "Multiple regression: computing the parameters by matrix inversion" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Model_II_User_Guide.pdf"> "Model II regression" (R library support document)</a><br />
<b>Day 2</b><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Statistical_tests.pdf"> "Statistical tests, permutation tests" (Chapter extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Compare_two_samples.pdf"> "Comparison of two samples" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Welch_correction.pdf"> "Appendix: t-test with Welch correction" (Manuscript extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Compare_means_by_anova.pdf"> "Compare several group means: anova" (Teaching document)</a><br />
<b>Day 3</b><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Nested_and_two-way_anova.pdf"> "Other forms of anova" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/General_linear_model.pdf"> "General linear model" (Teaching document)</a><br />
<h3>
<b>Part 2: Community ecology ordination</b></h3>
<b>Day 1</b><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/PCA_algebra.pdf"> "Algebra of Principal Component and Correspondence Analyses" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Ordination.pdf">"From classical to canonical ordination" (Chapter)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Contingency_table_analysis.pdf"> "Contingency table analysis" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Legendre_&_Gallagher_2001.pdf">"Ecologically meaningful transformations for ordination of species data" (Article)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Species_ordination_methods.pdf">"Ordination methods for community composition data tables" (Summary Figure)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Eigenanalysis.pdf"> "Eigenvalues and eigenvectors" (Teaching document)</a><br />
<b>Day 2</b><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Some_measures_S_and_D.pdf"> "Some measures of resemblance" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Rubinstein_2000.pdf"> "Is Miss America an undernourished role model?" (Article)</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Box_4.1.pdf"> "Variation partitioning" (Chapter extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Variables_in_regression_matrix_X.pdf"> "Types of variables in explanatory matrix X of multiple regression" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Chapter_10_p_525.pdf"> "R-square, adjusted R-square" (Chapter extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Peres-Neto_et_al_Ecology_2006.pdf">"Variation partitioning of species data matrices: estimation and comparison of fractions" (Article)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Pplynomial_regression.pdf"> "Polynomial regression, trend surface analysis" (Teaching document)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Permutation_test_summary.pdf">"Permutation test summary" (Classroom notes)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Function_regression.pdf"> "Multiple regression with permutation test" (R function)</a><br />
<b>Day 3</b><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Chapter_10_p_529-539.pdf"> "Partial linear regression and variation partitioning" (Chapter extract)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Legendre_J_Plant_Ecology_2007-08.pdf"> "Studying beta diversity: ecological variation partitioning by multiple regression and canonical analysis" (Article, Chinese and English versions)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Legendre_Canonical.pdf">"Canonical analysis" (Classroom notes)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Hooper_et_al_2002.pdf"> "Response of 20 native tree species..." (Article)</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Legendre_&_Anderson_1999.pdf">"Distance-based redundancy analysis..." (Article)</a><br />
<b> </b><br />
<hr />
<h2>
<center>Other documents</center></h2>
<a href="">(There is nothing in this section at the moment)</a><br />
<b> </b><br />
<hr />
<h2>
<center>Practicals in R</center></h2>
<a href="">Practicals last updated November 6, 2009</a><br />
<a href="http://biol09.biol.umontreal.ca/Beijing_Linear_model/Practicals_linear_model.zip"> Practicals for Part 1 of the course using the R statistical language</a><br />
<a href="http://biol09.biol.umontreal.ca/PLcourses/Practicals_in_R.zip"> Practicals for Part 2 of the course using the R statistical language</a><br />
<b> </b><br />
<hr />
<b><a href="http://www.bio.umontreal.ca/legendre/indexEn.html"> Programs</a> and <a href="http://www.bio.umontreal.ca/legendre/reprints/"> PDF reprints</a> from the lab of Pierre Legendre</b> Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-49228437338423489322011-01-12T04:57:00.000-08:002011-01-12T04:57:28.861-08:00Processing.org<a href="http://processing.org/learning/topics/">Topics \ Processing.org</a>: "- Enviado mediante la barra Google"<br /><br />el próximo Lunes 17 de enero a las 18:00, en el Aula T3 de la Facultad de Bellas Artes se impartirá el Taller de Processing.<br />Intervendrá como ponente Adrian Cuervo. Adrian es un artista formado en la Facultad de BBAA de Granada, Utiliza el video, sonido y sistemas interactivos como lenguajes con los que construye su discurso. Sus obras han sido premiadas, adquiridas y seleccionadas en importantes certámenes e instituciones. Es miembro de LitiumLab, Laboratorio de Arte Interactivo de la Univ. de Ganada, y miembro fundador de MADEMOTION, iniciativa dedicada a eventos relacionados con la cultura visual, el arte y otras tendencias transdisciplinares.<br /><br /><a href="http://processing.org/" target="_blank">Processing</a> es un software libre muy utilizado en creaciones digitales, interactivos, gráficos educativos y videojuegos, de una gran versatilidad y relativamente fácil de usar. Está disponible para Linux, Mac OSX y Windows, y podéis descargarlo en su sitio web (<a href="http://processing.org/download/" target="_blank">http://processing.org/<wbr>download/</a>)<br />Es necesario que llevéis vuestro portátilRosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-1645792296625377362011-01-10T09:20:00.000-08:002011-01-10T09:20:54.337-08:00SINC - Servicio de Información y Noticias Científicas<a href="http://www.plataformasinc.es/">SINC - Servicio de Información y Noticias Científicas</a>: "- Enviado mediante la barra Google"Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-60347899684332773842011-01-10T09:03:00.000-08:002011-09-29T05:36:37.354-07:00Gráficos en movimiento de Google, ahora con R!<a href="http://www.ted.com/talks/hans_rosling_at_state.html">Hans Rosling: Let my dataset change your mindset | Video on TED.com</a>: "- Enviado mediante la barra Google"<br />
<br />
<object height="326" width="446"><param name="movie" value="http://video.ted.com/assets/player/swf/EmbedPlayer.swf">
<param name="allowFullScreen" value="true">
<param name="allowScriptAccess" value="always">
<param name="wmode" value="transparent">
<param name="bgColor" value="#ffffff">
<param name="flashvars" value="vu=http://video.ted.com/talks/dynamic/HansRosling_2009S-medium.flv&su=http://images.ted.com/images/ted/tedindex/embed-posters/HansRosling-2009S.embed_thumbnail.jpg&vw=432&vh=240&ap=0&ti=620&introDuration=15330&adDuration=4000&postAdDuration=830&adKeys=talk=hans_rosling_at_state;year=2009;theme=presentation_innovation;theme=numbers_at_play;theme=rethinking_poverty;theme=medicine_without_borders;theme=africa_the_next_chapter;event=TED%40State;&preAdTag=tconf.ted/embed;tile=1;sz=512x288;">
<embed src="http://video.ted.com/assets/player/swf/EmbedPlayer.swf" pluginspace="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" bgcolor="#ffffff" allowfullscreen="true" allowscriptaccess="always" flashvars="vu=http://video.ted.com/talks/dynamic/HansRosling_2009S-medium.flv&su=http://images.ted.com/images/ted/tedindex/embed-posters/HansRosling-2009S.embed_thumbnail.jpg&vw=432&vh=240&ap=0&ti=620&introDuration=15330&adDuration=4000&postAdDuration=830&adKeys=talk=hans_rosling_at_state;year=2009;theme=presentation_innovation;theme=numbers_at_play;theme=rethinking_poverty;theme=medicine_without_borders;theme=africa_the_next_chapter;event=TED%40State;" height="326" width="446"></embed></object><br />
<br />
Aquí está el script para la construcción del gráfico:<br />
<br />
<div style="overflow: auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family: monospace;"><a href="http://inside-r.org/r-doc/base/library"><span style="color: #003399; font-weight: bold;">library</span></a><span style="color: #009900;">(</span><a href="http://www.blogger.com/packages/animation">animation</a><span style="color: #009900;">)</span>
ani.options<span style="color: #009900;">(</span><a href="http://www.blogger.com/packages/interval">interval</a> = <span style="color: #cc66cc;">0.2</span><span style="color: #339933;">,</span> ani.height = <span style="color: #cc66cc;">450</span><span style="color: #339933;">,</span> ani.width = <span style="color: #cc66cc;">600</span><span style="color: #339933;">,</span> outdir = <a href="http://inside-r.org/r-doc/base/getwd"><span style="color: #003399; font-weight: bold;">getwd</span></a><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>
<a href="http://inside-r.org/r-doc/graphics/title"><span style="color: #003399; font-weight: bold;">title</span></a> = <span style="color: blue;">"The Bubbles Animation in Hans Rosling's Talk"</span><span style="color: #339933;">,</span>
description = <span style="color: blue;">"An imitation of Hans Rosling's moving bubbles."</span><span style="color: #009900;">)</span>
ani.start<span style="color: #009900;">(</span><span style="color: #009900;">)</span>
<a href="http://inside-r.org/r-doc/graphics/par"><span style="color: #003399; font-weight: bold;">par</span></a><span style="color: #009900;">(</span><a href="http://www.blogger.com/packages/mAr">mar</a> = <a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.2</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span>
<span style="color: #666666; font-style: italic;"># with 'years' as the background</span>
Rosling.bubbles<span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/graphics/text"><span style="color: #003399; font-weight: bold;">text</span></a> = <span style="color: #cc66cc;">1951</span>:<span style="color: #cc66cc;">2000</span><span style="color: #009900;">)</span>
ani.stop<span style="color: #009900;">(</span><span style="color: #009900;">)</span></pre>
<pre class="r geshifilter-R" style="font-family: monospace;"><span style="color: #009900;">
</span></pre>
</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinY5HgZ2gasCR-4AoNZc9AT-1QF6JOBJZvV0WWeiQT2GXipgFTEYng7LFSBa09b5uMa4q4MXY-dI_0DN_d_9YTM_QZSiLwfWDkm9jZuDBxt7kjai5NMXHwcnumKpQpBZW871OOe6IPW6k/s1600/animation.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="204" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinY5HgZ2gasCR-4AoNZc9AT-1QF6JOBJZvV0WWeiQT2GXipgFTEYng7LFSBa09b5uMa4q4MXY-dI_0DN_d_9YTM_QZSiLwfWDkm9jZuDBxt7kjai5NMXHwcnumKpQpBZW871OOe6IPW6k/s320/animation.png" width="320" /></a></div>
<a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org"><br /></a><br />
En general, podemos crear gráficos en movimiento en R (del tipo desarrollado por Google), con el paquete "googleVis" (<a href="http://code.google.com/p/google-motion-charts-with-r/">http://code.google.com/p/google-motion-charts-with-r/</a>). Por ejemplo:<br />
* Motion Chart via gvisMotionChart<br />
* Geo Map via gvisGeoMap<br />
* Map via gvisMap<br />
* Table via gvisTable<br />
* Annotated Time Line via gvisAnnotatedTimeLine<br />
* Tree Map via gvisTreeMapRosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-70774651715429574652011-01-10T08:23:00.000-08:002011-01-10T08:23:02.615-08:00JGR - Java GUI for R - RForge.net<a href="http://rforge.net/JGR/">JGR - Java GUI for R - RForge.net</a>. Interfaz gráfica para RRosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-18853304492347311602011-01-10T07:35:00.000-08:002011-01-10T07:36:28.260-08:00Construcción de redes neuronales en R. Paquete AMORE.<div style="overflow:auto;"><div class="geshifilter"><pre class="r geshifilter-R" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">###### Ejemplo: Construcción de una RNA ########</span><br /><span style="color: #666666; font-style: italic;">#Queremos construir una red con: 2 neuronas de entrada (-1 y -2), una primera capa oculta con 4 neuronas (1-4), una segunda capa oculta con 2 neuronas (5 y 6) y finalmente, una capa de salida con 2 neuronas de salida (7 y 8); especificamos estos parámetros como n.neurons=c(2,4,2,2).</span><br />net.start <span style=""><-</span> newff<span style="color: #009900;">(</span>n.neurons=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span>learning.rate.global=1e<span style="">-</span>2<span style="color: #339933;">,</span>momentum.global=<span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span> error.criterium=<span style="color: #0000ff;">"LMS"</span><span style="color: #339933;">,</span> Stao=<span style="color: #000000; font-weight: bold;">NA</span><span style="color: #339933;">,</span>hidden.layer=<span style="color: #0000ff;">"tansig"</span><span style="color: #339933;">,</span> output.layer=<span style="color: #0000ff;">"purelin"</span><span style="color: #339933;">,</span> method=<span style="color: #0000ff;">"ADAPTgdwm"</span><span style="color: #009900;">)</span><br /> <br /><span style="color: #666666; font-style: italic;">#capas</span><br />net.start<span style="">$</span>layers<br /> <br /><span style="color: #666666; font-style: italic;">#neuronas</span><br /> <span style="color: #666666; font-style: italic;">#id</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>id<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>id<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>id<br /> <span style="color: #666666; font-style: italic;">#type</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>type<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>type<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>type<br /> <span style="color: #666666; font-style: italic;"># activation.function</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>activation.function<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>activation.function<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>activation.function<br /> <span style="color: #666666; font-style: italic;"># output.links</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>output.links<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>output.links<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>output.links<br /> <span style="color: #666666; font-style: italic;"># output.aims</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>output.aims<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>output.aims<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>output.aims<br /> <span style="color: #666666; font-style: italic;"># input.links</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>input.links<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>input.links<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>input.links<br /> <span style="color: #666666; font-style: italic;"># weigths</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>weights<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>weights<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>weights<br /> <span style="color: #666666; font-style: italic;"># bias</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>bias<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>bias<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>bias<br /> <span style="color: #666666; font-style: italic;"># v0</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>v0<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>v0<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>v0<br /> <span style="color: #666666; font-style: italic;"># v1</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>v1<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>v1<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>v1<br /> <span style="color: #666666; font-style: italic;"># f0</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>f0<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>f0<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>f0<br /> <span style="color: #666666; font-style: italic;"># f1</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>f1<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>f1<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>f1<br /> <span style="color: #666666; font-style: italic;"># method</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>method<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>method<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>method<br /> <span style="color: #666666; font-style: italic;"># method.dep.variables:</span><br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>method.dep.variables<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>method.dep.variables<br /> net.start<span style="">$</span>neurons<span style="color: #009900;">[</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">]</span><span style="color: #009900;">]</span><span style="">$</span>method.dep.variables<br /> <span style="color: #666666; font-style: italic;">#deltaE</span><br /> net.start<span style="">$</span>deltaE</pre></div></div><p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-17407696105835974672011-01-10T07:34:00.000-08:002011-01-10T07:36:54.312-08:00Entrenamiento de redes neuronales en R. Paquete AMORE.<div style="overflow:auto;"><div class="geshifilter"><pre class="r geshifilter-R" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">##### Ejemplo: Entrenamiento y simulación de la red #####</span><br /><span style="color: #666666; font-style: italic;">#entrenamiento para una red simple:</span><br /><a href="http://inside-r.org/r-doc/base/require"><span style="color: #003399; font-weight: bold;">require</span></a><span style="color: #009900;">(</span><a href="/packages/AMORE"><span style="">AMORE</span></a><span style="color: #009900;">)</span><br /><span style="color: #666666; font-style: italic;">## Creamos dos conjuntos de datos artificiales: ''P'' es el conjunto</span><br /><a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> datos <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> entrada y <span style="color: #0000ff;">''</span>target<span style="color: #0000ff;">''</span> el <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> salida.<br />P <span style=""><-</span> <a href="http://inside-r.org/r-doc/base/matrix"><span style="color: #003399; font-weight: bold;">matrix</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/sample"><span style="color: #003399; font-weight: bold;">sample</span></a><span style="color: #009900;">(</span><a href="http://inside-r.org/r-doc/base/seq"><span style="color: #003399; font-weight: bold;">seq</span></a><span style="color: #009900;">(</span><span style="">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><a href="http://inside-r.org/r-doc/base/length"><span style="color: #003399; font-weight: bold;">length</span></a>=<span style="color: #cc66cc;">500</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/replace"><span style="color: #003399; font-weight: bold;">replace</span></a>=<span style="color: #000000; font-weight: bold;">FALSE</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/ncol"><span style="color: #003399; font-weight: bold;">ncol</span></a>=<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br />target <span style=""><-</span> P<span style="">^</span><span style="color: #cc66cc;">2</span> <span style="">+</span> <a href="http://inside-r.org/r-doc/stats/rnorm"><span style="color: #003399; font-weight: bold;">rnorm</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0.5</span><span style="color: #009900;">)</span><br /><span style="color: #666666; font-style: italic;">## Creamos el objeto red neuronal feedforward</span><br />net.start <span style=""><-</span> newff<span style="color: #009900;">(</span>n.neurons=<a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> learning.rate.global=1e<span style="">-</span>2<span style="color: #339933;">,</span><br />momentum.global=<span style="color: #cc66cc;">0.5</span><span style="color: #339933;">,</span>error.criterium=<span style="color: #0000ff;">"LMS"</span><span style="color: #339933;">,</span>Stao=<span style="color: #000000; font-weight: bold;">NA</span><span style="color: #339933;">,</span>hidden.layer=<span style="color: #0000ff;">"<br />tansig"</span><span style="color: #339933;">,</span>output.layer=<span style="color: #0000ff;">"purelin"</span><span style="color: #339933;">,</span>method=<span style="color: #0000ff;">"ADAPTgdwm"</span><span style="color: #009900;">)</span><br /><span style="color: #666666; font-style: italic;">## Entrenamos la red según P y target</span><br />result <span style=""><-</span> train<span style="color: #009900;">(</span>net.start<span style="color: #339933;">,</span> P<span style="color: #339933;">,</span> target<span style="color: #339933;">,</span> error.criterium=<span style="color: #0000ff;">"LMS"</span><span style="color: #339933;">,</span><br />report=<span style="color: #000000; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> show.step=<span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> n.shows=<span style="color: #cc66cc;">5</span> <span style="color: #009900;">)</span><br /><a href="http://inside-r.org/r-doc/utils/str"><span style="color: #003399; font-weight: bold;">str</span></a><span style="color: #009900;">(</span>result<span style="color: #009900;">)</span><br /><span style="color: #666666; font-style: italic;">#objeto red neuronal entrenada con pesos y tendencias ajustadas</span><br />mediante el entrenamiento adaptativo backpropagation con el<br />método <a href="http://inside-r.org/r-doc/utils/de"><span style="color: #003399; font-weight: bold;">de</span></a> momentum <span style="color: #009900;">(</span>detallado en el apartado anterior<span style="color: #009900;">)</span><br /><span style="color: #666666; font-style: italic;">#matriz con los errores obtenidos durante el entrenamiento</span><br />result<span style="">$</span>Merror<br /><span style="color: #009900;">[</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span><br /><span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.2753484</span><br /><span style="color: #009900;">[</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.2749079</span><br /><span style="color: #009900;">[</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.2746274</span><br /><span style="color: #009900;">[</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.2744657</span><br /><span style="color: #009900;">[</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.2743568</span><br /><span style="color: #666666; font-style: italic;">## Gráficos para remarcar que ahora la red entrenada en un elemento de</span><br />la lista resultante<br />y <span style=""><-</span> sim<span style="color: #009900;">(</span>result<span style="">$</span>net<span style="color: #339933;">,</span> P<span style="color: #009900;">)</span><br /><span style="color: #666666; font-style: italic;">#vector de 500 elementos</span><br /><a href="http://inside-r.org/r-doc/utils/str"><span style="color: #003399; font-weight: bold;">str</span></a><span style="color: #009900;">(</span>y<span style="color: #009900;">)</span><br />num <span style="color: #009900;">[</span><span style="color: #cc66cc;">1</span><span style="">:</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">]</span> <span style="color: #cc66cc;">0.0374</span> <span style="color: #cc66cc;">0.8303</span> <span style="color: #cc66cc;">0.7052</span> <span style="color: #cc66cc;">0.0704</span> <span style="color: #cc66cc;">0.2761</span> ...<br /><a href="http://inside-r.org/r-doc/graphics/plot"><span style="color: #003399; font-weight: bold;">plot</span></a><span style="color: #009900;">(</span>P<span style="color: #339933;">,</span>y<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: #0000ff;">"blue"</span><span style="color: #339933;">,</span> pch=<span style="color: #0000ff;">"+"</span><span style="color: #009900;">)</span><br /><a href="http://inside-r.org/r-doc/graphics/points"><span style="color: #003399; font-weight: bold;">points</span></a><span style="color: #009900;">(</span>P<span style="color: #339933;">,</span>target<span style="color: #339933;">,</span> <a href="http://inside-r.org/r-doc/base/col"><span style="color: #003399; font-weight: bold;">col</span></a>=<span style="color: #0000ff;">"red"</span><span style="color: #339933;">,</span> pch=<span style="color: #0000ff;">"x"</span><span style="color: #009900;">)</span></pre></div></div><p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-78754724436109094612011-01-10T07:25:00.001-08:002011-01-10T07:25:23.941-08:00Redes Neuronales. Aplicaciones estadísticas.<a title="View TEMA 3 on Scribd" href="http://www.scribd.com/doc/46596337/TEMA-3" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;">TEMA 3</a> <object id="doc_54998362548448" name="doc_54998362548448" height="600" width="100%" type="application/x-shockwave-flash" data="http://d1.scribdassets.com/ScribdViewer.swf" style="outline:none;" > <param name="movie" value="http://d1.scribdassets.com/ScribdViewer.swf"> <param name="wmode" value="opaque"> <param name="bgcolor" value="#ffffff"> <param name="allowFullScreen" value="true"> <param name="allowScriptAccess" value="always"> <param name="FlashVars" value="document_id=46596337&access_key=key-2k11pj9ahq3y2zw26kkj&page=1&viewMode=list"> <embed id="doc_54998362548448" name="doc_54998362548448" src="http://d1.scribdassets.com/ScribdViewer.swf?document_id=46596337&access_key=key-2k11pj9ahq3y2zw26kkj&page=1&viewMode=list" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="600" width="100%" wmode="opaque" bgcolor="#ffffff"></embed> </object>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-17080855097232785072011-01-10T07:22:00.000-08:002011-01-10T07:23:38.109-08:00Redes Neuronales en R. Función de activación.<div style="overflow:auto;"><div class="geshifilter"><pre class="r geshifilter-R" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">######## Minería de datos ########</span><br /><span style="color: #666666; font-style: italic;">##### Tema 1: Redes neuronales ###</span><br /><span style="color: #666666; font-style: italic;"># La función de activación se utiliza para limitar el rango de valores de la respuesta de la neurona.</span><br /><span style="color: #666666; font-style: italic;"># Existen diversas funciones de activación y la decisión entre una u otra dependerá de la aplicación o problema a resolver</span><br /><span style="color: #666666; font-style: italic;"># Sea una neurona cuyas entradas sean el vector de estímulos X, el vector de pesos sonápticos W y el sesgo b, calculamos la respuesta de la neurana según la función umbral seleccionada.</span><br />X<span style=""><-</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><br />W<span style=""><-</span><a href="http://inside-r.org/r-doc/base/c"><span style="color: #003399; font-weight: bold;">c</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">.8</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">.5</span><span style="color: #009900;">)</span><br />b<span style=""><--</span><span style="color: #cc66cc;">1</span><br /> <br />respuesta<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #339933;">,</span>metodo=<span style="color: #0000ff;">"umbral"</span><span style="color: #339933;">,</span>alfa=<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>b<br /> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"umbral"</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"signo"</span><span style="color: #009900;">)</span><br /> <span style="color: #009900;">{</span><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><--</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">></span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"logistica"</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">/</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">+</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span>alfa<span style="">*</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"tang_hiperb"</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">-</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span>alfa<span style="">*</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="">/</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">+</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span>alfa<span style="">*</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"lineal"</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"lineal_acot"</span><span style="color: #009900;">)</span><br /> <span style="color: #009900;">{</span><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">></span><span style="color: #cc66cc;">0</span> <span style="">&&</span> salida<span style=""><</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">>=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">(</span>metodo<span style="">==</span><span style="color: #0000ff;">"lineal_acot_sim"</span><span style="color: #009900;">)</span><br /> <span style="color: #009900;">{</span><span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><=-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><--</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">>-</span><span style="color: #cc66cc;">1</span> <span style="">&&</span> salida<span style=""><</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">}</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">>=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: #009900;">}</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br /><span style="color: #666666; font-style: italic;">### En particular... #####</span><br />func_umbral<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>b<br /> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">{</span>fi<span style=""><-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br />func_signo<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>sesgo<br /> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><br /> fi<span style=""><--</span><span style="color: #cc66cc;">1</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><span style="color: #cc66cc;">0</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">></span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><span style="color: #cc66cc;">1</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br />func_logistica<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #339933;">,</span>alfa=<span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>sesgo<br /> fi<span style=""><-</span><span style="color: #cc66cc;">1</span><span style="">/</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">+</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span>alfa<span style="">*</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">)</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br />func_tang_hiperb<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #339933;">,</span>alfa=<span style="color: #cc66cc;">2</span><span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>sesgo<br /> fi<span style=""><-</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">-</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span>alfa<span style="">*</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="">/</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="">+</span><a href="http://inside-r.org/r-doc/base/exp"><span style="color: #003399; font-weight: bold;">exp</span></a><span style="color: #009900;">(</span>alfa<span style="">*</span>salida<span style="color: #009900;">)</span><span style="color: #009900;">)</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br />func_lineal<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>sesgo<br /> fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span>salida<span style="color: #009900;">)</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br />func_lineal_acot<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>sesgo<br /> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><span style="color: #cc66cc;">0</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">></span><span style="color: #cc66cc;">0</span> <span style="">&&</span> salida<span style=""><</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span>salida<span style="color: #009900;">)</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">>=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><span style="color: #cc66cc;">1</span><br /> fi<br /><span style="color: #009900;">}</span><br /> <br />func_lineal_acotSim<span style=""><-</span><a href="http://inside-r.org/r-doc/base/function"><span style="color: #003399; font-weight: bold;">function</span></a><span style="color: #009900;">(</span>entrada=X<span style="color: #339933;">,</span>pesos=W<span style="color: #339933;">,</span>sesgo=b<span style="color: #009900;">)</span><br /><span style="color: #009900;">{</span><br /> salida<span style=""><-</span>X<span style="">%*%</span>W<span style="">+</span>sesgo<br /> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style=""><=-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /> fi<span style=""><--</span><span style="color: #cc66cc;">1</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">>-</span><span style="color: #cc66cc;">1</span> <span style="">&&</span> salida<span style=""><</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><a href="http://inside-r.org/r-doc/base/as.numeric"><span style="color: #003399; font-weight: bold;">as.numeric</span></a><span style="color: #009900;">(</span>salida<span style="color: #009900;">)</span><br /> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">(</span>salida<span style="">>=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span><br /> fi<span style=""><-</span><span style="color: #cc66cc;">1</span><br /> fi<br /><span style="color: #009900;">}</span></pre></div></div><p><a href="http://www.inside-r.org/pretty-r" title="Created by Pretty R at inside-R.org">Created by Pretty R at inside-R.org</a></p>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0tag:blogger.com,1999:blog-6841022238556785612.post-27633203203940443152011-01-10T07:20:00.000-08:002011-01-10T07:21:09.623-08:00Redes Neuronales en R. Paquete AMORE.<a title="View The AMORE Package on Scribd" href="http://www.scribd.com/doc/46596332/The-AMORE-Package" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;">The AMORE Package</a> <object id="doc_47804352624705" name="doc_47804352624705" height="600" width="100%" type="application/x-shockwave-flash" data="http://d1.scribdassets.com/ScribdViewer.swf" style="outline:none;" > <param name="movie" value="http://d1.scribdassets.com/ScribdViewer.swf"> <param name="wmode" value="opaque"> <param name="bgcolor" value="#ffffff"> <param name="allowFullScreen" value="true"> <param name="allowScriptAccess" value="always"> <param name="FlashVars" value="document_id=46596332&access_key=key-1pc52zocc6dxt3lxgkum&page=1&viewMode=list"> <embed id="doc_47804352624705" name="doc_47804352624705" src="http://d1.scribdassets.com/ScribdViewer.swf?document_id=46596332&access_key=key-1pc52zocc6dxt3lxgkum&page=1&viewMode=list" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="600" width="100%" wmode="opaque" bgcolor="#ffffff"></embed> </object>Rosana Ferrerohttp://www.blogger.com/profile/05891745549463934951noreply@blogger.com0