marzo 06, 2013

Densidades superpuestas


#####
# Densidades normales superpuestas
# secuencia de valores de -20 a 20; muestra de 100 números
x = seq(-20,20, length=100)

# media = 0 ; desviación estándar = 1
normal1 = dnorm(x,mean=0,sd=1)
plot(x,normal1, type='l', lwd=2, col='red', main='Funciones normales: Cambiando varianza')

# media = 0 ;  desviación estándar = 4
normal2 = dnorm(x,mean=0,sd=4)
lines(x,normal2,type='l',lwd=2, col='blue')

# media = 0 ; desviación estándar = 9
normal2 = dnorm(x,mean=0,sd=9)
lines(x,normal2,type='l',lwd=2, col='green')
plot of chunk unnamed-chunk-1


#####
# secuencia de valores de -6 a 6; muestra de 100 números
y = seq(-6,6, length=100)

# media = 0 ; desviación estándar = 1
normal1 = dnorm(y,mean=0,sd=1)
plot(y,normal1, type='l', lwd=2, col='red', main='Funciones normales: Cambiando media')

# media = 1 ;  desviación estándar = 1
normal2 = dnorm(y,mean=1,sd=1)
lines(y,normal2,type='l',lwd=2, col='blue')

# media = 2 ; desviación estándar = 1
normal2 = dnorm(y,mean=2,sd=1)
lines(y,normal2,type='l',lwd=2, col='green')
plot of chunk unnamed-chunk-1







marzo 05, 2013

t vs. z


Comparación de t(n-2) versus N(0,1)


# Distribución normal N(0,1)
x <- seq(-5,5,length=100)
plot(x,dnorm(x),type="l",lwd=3)

#t (3 grados de libertad)
lines(x,dt(x,df=3),lwd=3,col="red")

# t (10 grados de libertad)
lines(x,dt(x,df=10),lwd=3,col="blue")
plot of chunk unnamed-chunk-1








marzo 03, 2013

Gráfico de barras ± CI en R


Gráficos de barra con errores estándares o intervalos de confidencia son comunes en publicaciones científicas. En este sitio http://gallery.r-enthusiasts.com/graph/barplot2_:_Enhanced_Bar_Plots_54 encontrare un ejemplo de códigos.

# Example with confidence intervals and grid
# upload gplots, gdata, gtools, and caTools packages
library(gplots)
Warning: package 'gplots' was built under R version 3.0.1
Loading required package: gtools
Loading required package: gdata
gdata: Unable to locate valid perl interpreter gdata: gdata: read.xls()
will be unable to read Excel XLS and XLSX files gdata: unless the 'perl='
argument is used to specify the location gdata: of a valid perl
intrpreter. gdata: gdata: (To avoid display of this message in the future,
please gdata: ensure perl is installed and available on the executable
gdata: search path.)
gdata: Unable to load perl libaries needed by read.xls() gdata: to support
'XLX' (Excel 97-2004) files.
gdata: Unable to load perl libaries needed by read.xls() gdata: to support
'XLSX' (Excel 2007+) files.
gdata: Run the function 'installXLSXsupport()' gdata: to automatically
download and install the perl gdata: libaries needed to support Excel XLS
and XLSX formats.
Attaching package: 'gdata'
The following object is masked from 'package:stats':

nobs
The following object is masked from 'package:utils':

object.size
Loading required package: caTools
Warning: package 'caTools' was built under R version 3.0.1
Loading required package: grid
Loading required package: KernSmooth
KernSmooth 2.23 loaded Copyright M. P. Wand 1997-2009
Loading required package: MASS
Attaching package: 'gplots'
The following object is masked from 'package:stats':

lowess
hh <- t(VADeaths)[, 5:1]
mybarcol <- "gray20"
ci.l <- hh * 0.85
ci.u <- hh * 1.15
mp <- barplot2(hh, beside = TRUE,
               col = c("lightblue", "mistyrose",
                       "lightcyan", "lavender"),
               legend = colnames(VADeaths), ylim = c(0, 100),
               main = "Tasa de decesos en Virginia", font.main = 4,
               sub = "Faked 95 percent error bars", col.sub = mybarcol,
               cex.names = 1.5, plot.ci = TRUE, ci.l = ci.l, ci.u = ci.u,
               plot.grid = TRUE)
mtext(side = 1, at = colMeans(mp), line = 2,
      text = paste("Mean", formatC(colMeans(hh))), col = "red")
box()
plot of chunk unnamed-chunk-1


Fuente: R Graph Gallery. Barplot2: enhanced Bar Plots http://gallery.r-enthusiasts.com/graph/barplot2_:_Enhanced_Bar_Plots_54






marzo 02, 2013

Generando resultados de R en “.txt”


# Creando resultados en ".txt"
# directorio a guardar archivo
sink("C:/Users/Administrator/Desktop/RVida.txt")

# cargando datos
expV = read.csv("C:/Users/Administrator/Desktop/vida.csv", header = T)

# estadí­stica descriptiva
require(psych)
Loading required package: psych
describe(expV [,2-3])
        var   n  mean    sd median trimmed  mad   min  max range  skew
hombres   1 193 67.76  9.28  70.48   68.59 8.66 46.62 80.7 34.08 -0.70
mujeres   2 193 72.28 10.54  75.71   73.41 9.05 47.20 86.7 39.50 -0.86
        kurtosis   se
hombres    -0.53 0.67
mujeres    -0.43 0.76






marzo 01, 2013

Generando resultados de SAS en “.pdf”


*Creando resultados en '.pdf';
* Importando "vidaEx.csv" al editor;
proc import datafile = "C:\Users\Administrator\Desktop\vida.csv"
    out = vidaEx;
    getnames=yes; run;

ods pdf;
proc means n mean std median min max range skew kurtosis stderr nmiss sum var maxdec=2;
run;
ods pdf close;

/*
Los códigos
    "ods pdf;"
    "ods pdf close;"
al inicio y final, envuelven los procedimientos a imprimir en pdf

*/







febrero 28, 2013

Muestreo en R


# generando un millón de números aleatorios
x <- rnorm(1e6)
y <- rnorm(1e6)
plot(x,y,pch=19, col="blue")
plot of chunk unnamed-chunk-1

# Dispersión suavizada. El azul intenso, al centro del gráfico, muestra la ubicación de la mayor cantidad de números; los puntos dispersos en el borde, por otra parte, son outliers)
smoothScatter(x,y)
KernSmooth 2.23 loaded Copyright M. P. Wand 1997-2009
plot of chunk unnamed-chunk-1

# muestreando 1000 números 
sampledValues <- sample(1:1e4, size=1000, replace=F)
plot(x[sampledValues], y[sampledValues], pch=19, col="blue")
plot of chunk unnamed-chunk-1