Formularios Stats
Formularios Stats
Formularios Stats
Prefijos:
calculo de f (x) d
generador aleatorio r
función de distribución F (x) p
cuantiles , xp tal que F (xp ) = p q
x̂T +h|T = aT
METODO DE HOLT
at = αxt + (1 − α)(at−1 + bt−1 ) SELECCION DEL NUMERO DE GRUPOS EN ANALISIS CLUSTER NO JERARQUICO
S_datos<-cov(datos)
# calculo de la matriz de varianzas covarianzas variab <- get_pca_var(datos_PCA)
corrplot(R_datos,type="lower") cargas_factoriales<-variab$cor
#representacion grafica y contrastes de la cargas_factoriales
#matriz de correlaciones corrplot(cargas_factoriales, is.corr=TRUE)
#para representarlas
CALCULO DE VALORES Y VECTORES PROPIOS
fviz_pca_var(encuesta_PCA, col.var = "blue")
datos_PCA<-PCA(datos,scale.unit=FALSE,
graph=FALSE)
# con matriz var-covar
datos_PCA<-PCA(datos,scale.unit=TRUE,
graph=FALSE)
# con matriz de correlaciones
get_eigenvalue(datos_PCA)
prcomp(datos, scale.=TRUE)
# a partir de la matriz de correlaciones
prcomp(datos, scale.=FALSE)
# a partir de la matriz de varianzas-covarianzas
#calculo de los vectores propios asociados
SELECCION DE COMPONENTES PRINCIPALES
fviz_eig(datos_PCA,addlabels = TRUE)
#visualiza el grafo sedimentacion
valores_propios<-datos_PCA$eig[,1]
valores_propios
mean(valores_propios)
#criterio de la media
EXTRACCION DE FACTORES: METODO DE LOS EJES PRINCIPALES.
Universidad Politécnica de Cartagena
Dpto. Matemática Aplicada y Estadı́stica
datos_ep<- fa(R_datos,nfactors=p,fm="pa",rotate="none",n.obs=n)
Estadı́stica Industrial. Pilar Sanmartı́n
#sin rotar
FORMULARIO R ANALISIS FACTORIAL
datos_ep_rot<- fa(R_datos,nfactors=p,fm="pa",rotate="varimax",n.obs=n)
LIBRERIAS #rotacion varimax
plot(datos_ep_rot)
library(corrplot) #grafico cargas factoriales vs ejes factores
library(psych)
OBTENCION OPUNTUACIONES
ANALISIS PREVIO
datos_pca <-principal(datos,nfactors=p,rotate="none")
#p factores sin rotar
datos_pca_rot <-principal(datos,nfactors=p,rotate="varimax")
#rotacion por el metodo varimax
plot(datos_pca_rot)
#grafico cargas factoriales vs ejes factores
datos_ml <-fa(R_datos,nfactors=p,fm="ml",rotate="none",n.obs=n)
#sin rotar
datos_ml_rot <-fa(R_datos,nfactors=p,fm="ml",rotate="varimax",n.obs=n)
#rotation varimax
plot(datos_ml_rot)
#grafico cargas factoriales vs ejes factores
METODO DEL VECINO MAS PROXIMO
Universidad Politécnica de Cartagena
Dpto. Matemática Aplicada y Estadı́stica
Estadı́stica Industrial. Pilar Sanmartı́n hclust.single<-hclust(datos.dis.euclid2,method="single")
FORMULARIO R ANALISIS CLUSTER
#dendograma
LIBRERIAS data.frame(hclust.single[2:1])
plot.hclust<-plot(hclust.single)
library(stats) rect.hclust(hclust.single, k = 2, border = "red")
library(psych) # k= numero de grupos a retener
ANALISIS PREVIO
METODO DEL VECINO MAS ALEJADO
#dendograma
REPRESENTACION GRAFICA DE LOS GRUPOS
data.frame(hclust.centroide[2:1])
plot.hclust<-plot(hclust.centroide)
rect.hclust(hclust.centroide, k = 2, border = "red") library(factoextra)
# k= numero de grupos a retener fviz_cluster(kmeans.datos, data = datos)
ESTUDIO DEL TAMAÑO DE LOS GRUPOS
library(psych)
fit.x_i<-aov(varx~kmeans.datos$cluster,data=datos)
summary(fit.x_i)
#x_i una de las variables considerada
library(factoextra)
grupo.ward<-cutree(hclust.ward,k=2,h=NULL)
datos.grupos<-cbind(datos,grupo.ward)
round(aggregate(datos.grupos,list(grupo.ward),mean),2)
c1<-c(m11, m12, m13,...., m1p)
c2<-c(m21, m22, m23,...., m2p)
#medias de los dos grupos (suponemos k=2)
solucion.datos<-kmeans(datos,rbind(c1,c2))
solucion.datos
fviz_cluster(solucion.datos, data = datos)
Universidad Politécnica de Cartagena #componente estacional
Dpto. Matemática Aplicada y Estadı́stica a.1$random
Estadı́stica Industrial. Pilar Sanmartı́n #error
FORMULARIO R ANALISIS CLASICO Dt.1<-serie_data-a1.1$seasonal
DE SERIES TEMPORALES Dt.1
#serie desestacionalizada
LIBRERIAS
tiempo<-c(1:length(Dt.2))
logDt.2<-log(Dt.2)
a.2<-decompose(serie_data,type="multiplicative")
ajuste.2.3<-lm(logDt.2~tiempo)
plot(a.2)
summary(ajuste.2.3)
a.2$trend
#ajuste exponencial
# tendencia
#h=12 (componente estacional periodo 12)
a.2$seasonal
predicciones.3<-exp((a0+a1*tiempoh))*a.2$figure
a.2$figure
predicciones.3
#componente estacional
a.2$random
#error
Dt.2<-serie_data/a.2$seasonal
Dt.2
#serie desestacionalizada
tiempo<-c(1:length(Dt.2))
ajuste.2<-lm(Dt.2~tiempo)
summary(ajuste.2)
#ajuste lineal
tiempoh<-length(Dt.2)+c(1:h)
#h=12 (componente estacional periodo 12)
predicciones.2<-(a0+a1*tiempoh)*a.2$figure
predicciones.2
tiempo<-c(1:length(Dt.2))
tiempo2<-tiempo^2
ajuste.2.2<-lm(Dt.2~tiempo+tiempo2)
summary(ajuste.2.2)
#ajuste cuadratico
#h=12 (componente estacional periodo 12)
tiempoh<-length(Dt.2)+c(1:h)
tiempoh2<-tiempoh^2
predicciones.2.2<-(a0+a1*tiempoh+a2*tiempoh2) *a.2$figure
predicciones.2.2
ALISADO EXPONENCIAL SIMPLE (ETS(A,N,N))
Universidad Politécnica de Cartagena
Dpto. Matemática Aplicada y Estadı́stica
ses.data <- ses(serie.data, alpha=NULL, beta=0, initial="optimal", h=k)
Estadı́stica Industrial. Pilar Sanmartı́n
# k predicciones futuras
FORMULARIO R ALISADO EXPONENCIAL
summary(ses.data) #modelo ajustado
ses.data$mean
LIBRERIAS
#predicciones
plot(ses.data)
library(fpp)
#representacion del modelo y predicciones
library(stats)
METODO DE HOLT (ETS(A,A,N))
CARACTERISTICAS PRINCIPALES
GRAFICOS PRELIMINARES
ts.plot(serie_data,main="titulo",xlab="ejex", ylab="ejey")
plot(aggregate(serie_data))
#grafico con la serie agregada por frecuencia
cycle(serie_data)
boxplot(serie_data~cycle(serie_data))
# diagrama de cajas y bigotes por componente estacional
medias.an<- aggregate(serie_data,nfrequency=1,FUN=mean)
detip.an<- aggregate(serie_data,nfrequency=1,FUN=sd)
plot(medias.an,detip.an)
#grafico medias vs desviaciones
METODO DE HOLT-WINTERS MULTIPLICATIVO (ETS(M,A,M)) METODO DE HOLT-WINTERS MULTIPLICATIVO (ETS(A,A,M))
ANALISIS PRELIMINARES
#supuesto de normalidad, k predicciones futuras
pred<-forecast(fit, h=k)
print(pred)
#grafico temporal
#sin normalidad pero independencia
ts.plot(data,main="titulo",xlab="ejex", ylab="ejey")
# y varianza constante de los residuos
#funcion de autocorrelacion muestral simple
pred<-forecast(fit,bootstrap = TRUE, h=k)
acf(data)
print(pred)
##funcion de autocorrelacion parcial muestral
#representacion gráfica
pacf(data)
plot(pred)
## Tests KPSS de estacionaridad
## (Kwiatkowski-Phillips-Schmidt-Shin)
unitroot_kpss(data)
## diferenciacion de la serie
diff(x, lag = 1, differences = 1, ...)
#lag= retardo que se diferencia (por defecto 1)
#differences = orden de diferenciación
unitroot_ndiffs(data)
#test para ver el orden de diferenciacion
Do something Do something
Getting Help An integer
2:6 2 3 4 5 6
sequence } }
RStudio® is a trademark of RStudio, Inc. • CC BY Mhairi McNeill • [email protected] Learn more at web page or vignette • package version • Updated: 3/15
Types Matrices Strings Also see the stringr package.
m <- matrix(x, nrow = 3, ncol = 3) paste(x, y, sep = ' ')
Converting between common data types in R. Can always go Join multiple vectors together.
Create a matrix from x.
from a higher value in the table to a lower value.
paste(x, collapse = ' ') Join elements of a vector together.
m[2, ] - Select a row t(m)
w
ww Transpose
grep(pattern, x) Find regular expression matches in x.
ww
as.logical TRUE, FALSE, TRUE Boolean values (TRUE or FALSE).
w m[ , 1] - Select a column
m %*% n gsub(pattern, replace, x) Replace matches in x with a string.
ww
as.numeric 1, 0, 1
numbers.
w
ww
ww
preferred to factors. nchar(x) Number of characters in a string.
as.factor
'1', '0', '1',
levels: '1', '0'
Character strings with preset
levels. Needed for some
statistical models.
w Lists Factors
l <- list(x = 1:5, y = c('a', 'b')) factor(x) cut(x, breaks = 4)
Maths Functions A list is a collection of elements which can be of different types. Turn a vector into a factor. Can
set the levels of the factor and
Turn a numeric vector into a
factor by ‘cutting’ into
log(x) Natural log. sum(x) Sum. l[[2]] l[1] l$x l['y'] the order. sections.
New list with New list with
exp(x) Exponential. mean(x) Mean. Second element Element named
only the first only element
max(x) Largest element. median(x) Median.
of l.
element.
x.
named y. Statistics
min(x) Smallest element. quantile(x) Percentage
lm(y ~ x, data=df) prop.test
Also see the t.test(x, y)
quantiles.
dplyr package. Data Frames Linear model. Perform a t-test for Test for a
round(x, n) Round to n decimal rank(x) Rank of elements. difference
difference between
places. glm(y ~ x, data=df) between
df <- data.frame(x = 1:3, y = c('a', 'b', 'c')) means.
Generalised linear model. proportions.
signif(x, n) Round to n var(x) The variance. A special case of a list where all elements are the same length.
significant figures. pairwise.t.test
List subsetting summary aov
Perform a t-test for
cor(x, y) Correlation. sd(x) The standard x y Get more detailed information Analysis of
paired data.
deviation. out a model. variance.
df$x df[[2]]
1 a
Variable Assignment Distributions
2 b Understanding a data frame
> a <- 'apple' Random Density Cumulative
Quantile
> a See the full data Variates Function Distribution
3 c View(df)
[1] 'apple' frame. Normal rnorm dnorm pnorm qnorm
See the first 6
Matrix subsetting head(df) Poisson rpois dpois ppois qpois
rows.
The Environment Binomial rbinom dbinom pbinom qbinom
df[ , 2]
ls() List all variables in the nrow(df) cbind - Bind columns. Uniform runif dunif punif qunif
environment. Number of rows.
columns.
rm(list = ls()) Remove all variables from the rbind - Bind rows. plot(x) plot(x, y) hist(x)
environment. Values of x in Values of x Histogram of
dim(df)
Number of order. against y. x.
You can use the environment panel in RStudio to
df[2, 2] columns and
browse variables in your environment. rows.
Dates See the lubridate package.
RStudio® is a trademark of RStudio, Inc. • CC BY Mhairi McNeill • [email protected] • 844-448-1212 • rstudio.com Learn more at web page or vignette • package version • Updated: 3/15
RStudio IDE : : CHEAT SHEET
Documents and Apps Write Code R Support Pro Features
Open Shiny, R Markdown, Navigate Open in new Save Find and Compile as Run Import data History of past Display .RPres slideshows Share Project Active shared
knitr, Sweave, LaTeX, .Rd files tabs window replace notebook selected with wizard commands to File > New File > with Collaborators collaborators
and more in Source Pane code run/copy R Presentation Start new R Session
T H J in current project
Check Render Choose Choose Insert T H J
Close R
spelling output output output code Session in
format location chunk project
Select
Cursors of Re-run Source with or Show file Load Save Delete all Search inside R Version
shared users previous code without Echo outline workspace workspace saved objects environment
Jump to Jump Run Publish Show file PROJECT SYSTEM
previous to next selected to server outline Multiple cursors/column selection Choose environment to display from Display objects
chunk chunk lines list of parent environments as list or grid File > New Project
with Alt + mouse drag.
Code diagnostics that appear in the margin. RStudio saves the call history,
Access markdown guide at
Help > Markdown Quick Reference Hover over diagnostic symbols for details. workspace, and working
directory associated with a
Syntax highlighting based Name of
Jump to Set knitr Run this and Run this project. It reloads each when
on your file's extension current project
chunk chunk all previous code chunk you re-open a project.
options code chunks Tab completion to finish
function names, file paths, Displays saved objects by View in data View function RStudio opens plots in a dedicated Plots pane
arguments, and more. type with short description viewer source code
Multi-language code
snippets to quickly use Navigate Open in Export Delete Delete
common blocks of code. recent plots window plot plot all plots
RStudio recognizes that files named app.R,
server.R, ui.R, and global.R belong to a shiny app Jump to function in file Change file type GUI Package manager lists every installed package
Create Upload Delete Rename Change
folder file file file directory
Install Update Create reproducible package
Run Choose Publish to Manage Path to displayed directory Packages Packages library for your project
app location to shinyapps.io publish Working Maximize,
view app or server accounts Directory minimize panes
Press ! to see Drag pane A File browser keyed to your working directory.
command history boundaries Click on file or directory name to open. Click to load package with Package Delete
library(). Unclick to detach version from
Highlighted
Package Writing
line shows Stop Shiny Publish to shinyapps.io, Refresh
where app rpubs, RSConnect, …
execution has
paused File > New Project > View(<data>) opens spreadsheet like view of data set
New Directory > R Package
Run commands in Examine variables Select function Step through Step into and Resume Quit debug Turn project into package,
environment where in executing in traceback to code one line out of functions execution mode Enable roxygen documentation with
execution has paused environment debug at a time to run Tools > Project Options > Build Tools
Roxygen guide at Filter rows by value Sort by Search
Help > Roxygen Quick Reference or value range values for value
RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at www.rstudio.com • RStudio IDE 0.99.832 • Updated: 2016-01
1 LAYOUT Windows/Linux Mac 4 WRITE CODE Windows /Linux Mac WHY RSTUDIO SERVER PRO?
Move focus to Source Editor Ctrl+1 Ctrl+1 Attempt completion Tab or Ctrl+Space Tab or Cmd+Space RSP extends the the open source server with a
Move focus to Console Ctrl+2 Ctrl+2 Navigate candidates !/$ !/$ commercial license, support, and more:
Accept candidate Enter, Tab, or # Enter, Tab, or #
Move focus to Help Ctrl+3 Ctrl+3 • open and run multiple R sessions at once
Show History Ctrl+4 Ctrl+4 Dismiss candidates Esc Esc
Undo Ctrl+Z Cmd+Z
• tune your resources to improve performance
Show Files Ctrl+5 Ctrl+5 • edit the same project at the same time as others
Show Plots Ctrl+6 Ctrl+6 Redo Ctrl+Shift+Z Cmd+Shift+Z
Show Packages Ctrl+7 Ctrl+7 Cut Ctrl+X Cmd+X • see what you and others are doing on your server
Show Environment Ctrl+8 Ctrl+8 Copy Ctrl+C Cmd+C • switch easily from one version of R to a different version
Show Git/SVN Ctrl+9 Ctrl+9 Paste Ctrl+V Cmd+V • integrate with your authentication, authorization, and audit practices
Show Build Ctrl+0 Ctrl+0 Select All Ctrl+A Cmd+A Download a free 45 day evaluation at
Delete Line Ctrl+D Cmd+D www.rstudio.com/products/rstudio-server-pro/
2 RUN CODE Windows/Linux Mac Select Shift+[Arrow] Shift+[Arrow]
Search command history Ctrl+! Cmd+! Select Word Ctrl+Shift+ "/# Option+Shift+ "/# 5 DEBUG CODE Windows/Linux Mac
Navigate command history !/$ !/$ Select to Line Start Alt+Shift+" Cmd+Shift+" Toggle Breakpoint Shift+F9 Shift+F9
Move cursor to start of line Home Cmd+" Select to Line End Alt+Shift+# Cmd+Shift+# Execute Next Line F10 F10
Move cursor to end of line End Cmd+ # Select Page Up/Down Shift+PageUp/Down Shift+PageUp/Down Step Into Function Shift+F4 Shift+F4
Change working directory Ctrl+Shift+H Ctrl+Shift+H Select to Start/End Shift+Alt+!/$ Cmd+Shift+!/$ Finish Function/Loop Shift+F6 Shift+F6
Interrupt current command Esc Esc Delete Word Left Ctrl+Backspace Ctrl+Opt+Backspace Continue Shift+F5 Shift+F5
Clear console Ctrl+L Ctrl+L Delete Word Right Option+Delete Stop Debugging Shift+F8 Shift+F8
Quit Session (desktop only) Ctrl+Q Cmd+Q Delete to Line End Ctrl+K
Restart R Session Ctrl+Shift+F10 Cmd+Shift+F10 Delete to Line Start Option+Backspace Windows/Linux
6 VERSION CONTROL Mac
Run current line/selection Ctrl+Enter Cmd+Enter Indent Tab (at start of line) Tab (at start of line) Show diff Ctrl+Alt+D Ctrl+Option+D
Run current (retain cursor) Alt+Enter Option+Enter Outdent Shift+Tab Shift+Tab Commit changes Ctrl+Alt+M Ctrl+Option+M
Run from current to end Ctrl+Alt+E Cmd+Option+E Yank line up to cursor Ctrl+U Ctrl+U Scroll diff view Ctrl+!/$ Ctrl+!/$
Run the current function Ctrl+Alt+F Cmd+Option+F Yank line after cursor Ctrl+K Ctrl+K Stage/Unstage (Git) Spacebar Spacebar
Source a file
definition Ctrl+Alt+G Cmd+Option+G Insert yanked text Ctrl+Y Ctrl+Y Stage/Unstage and move to next Enter Enter
Source the current file Ctrl+Shift+S Cmd+Shift+S Insert <- Alt+- Option+-
Source with echo Ctrl+Shift+Enter Cmd+Shift+Enter Insert %>% Ctrl+Shift+M Cmd+Shift+M
7 MAKE PACKAGES Windows/Linux Mac
Show help for function F1 F1
3 NAVIGATE CODE Windows /Linux Mac Build and Reload Ctrl+Shift+B Cmd+Shift+B
Show source code F2 F2
Goto File/Function Ctrl+. Ctrl+. Load All (devtools) Ctrl+Shift+L Cmd+Shift+L
unction
New document Ctrl+Shift+N Cmd+Shift+N
Fold Selected Alt+L Cmd+Option+L Test Package (Desktop) Ctrl+Shift+T Cmd+Shift+T
New document (Chrome) Ctrl+Alt+Shift+N Cmd+Shift+Opt+N
Unfold Selected Shift+Alt+L Cmd+Shift+Option+L Test Package (Web) Ctrl+Alt+F7 Cmd+Opt+F7
Open document Ctrl+O Cmd+O
Fold All Alt+O Cmd+Option+O Check Package Ctrl+Shift+E Cmd+Shift+E
Save document Ctrl+S Cmd+S
Unfold All Shift+Alt+O Cmd+Shift+Option+O Close document Ctrl+W Cmd+W Document Package Ctrl+Shift+D Cmd+Shift+D
Go to line Shift+Alt+G Cmd+Shift+Option+G Close document (Chrome) Ctrl+Alt+W Cmd+Option+W
Jump to Shift+Alt+J Cmd+Shift+Option+J Close all documents Ctrl+Shift+W Cmd+Shift+W 8 DOCUMENTS AND APPS Windows/Linux Mac
Switch to tab Ctrl+Shift+. Ctrl+Shift+. Extract function Ctrl+Alt+X Cmd+Option+X Preview HTML (Markdown, etc.) Ctrl+Shift+K Cmd+Shift+K
Previous tab Ctrl+F11 Ctrl+F11 Extract variable Ctrl+Alt+V Cmd+Option+V Knit Document (knitr) Ctrl+Shift+K Cmd+Shift+K
Next tab Ctrl+F12 Ctrl+F12 Reindent lines Ctrl+I Cmd+I Compile Notebook Ctrl+Shift+K Cmd+Shift+K
First tab Ctrl+Shift+F11 Ctrl+Shift+F11 (Un)Comment lines Ctrl+Shift+C Cmd+Shift+C Compile PDF (TeX and Sweave) Ctrl+Shift+K Cmd+Shift+K
Last tab Ctrl+Shift+F12 Ctrl+Shift+F12 Reflow Comment Ctrl+Shift+/ Cmd+Shift+/ Insert chunk (Sweave and Knitr) Ctrl+Alt+I Cmd+Option+I
Navigate back Ctrl+F9 Cmd+F9 Reformat Selection Ctrl+Shift+A Cmd+Shift+A Insert code section Ctrl+Shift+R Cmd+Shift+R
Navigate forward Ctrl+F10 Cmd+F10 Select within braces Ctrl+Shift+E Ctrl+Shift+E Re-run previous region Ctrl+Shift+P Cmd+Shift+P
Jump to Brace Ctrl+P Ctrl+P Show Diagnostics Ctrl+Shift+Alt+P Cmd+Shift+Opt+P Run current document Ctrl+Alt+R Cmd+Option+R
Select within Braces Ctrl+Shift+Alt+E Ctrl+Shift+Option+E Transpose Letters Ctrl+T
Run from start to current line Ctrl+Alt+B Cmd+Option+B
Use Selection for Find Ctrl+F3 Cmd+E Move Lines Up/Down Alt+!/$ Option+!/$
Run the current code section Ctrl+Alt+T Cmd+Option+T
Find in Files Ctrl+Shift+F Cmd+Shift+F Copy Lines Up/Down Shift+Alt+!/$ Cmd+Option+!/$
Run previous Sweave/Rmd code Ctrl+Alt+P Cmd+Option+P
Find Next Win: F3, Linux: Ctrl+G Cmd+G Add New Cursor Above Ctrl+Alt+Up Ctrl+Option+Up
Find Previous W: Shift+F3, L: Cmd+Shift+G Add New Cursor Below Ctrl+Alt+Down Ctrl+Option+Down Run the current chunk Ctrl+Alt+C Cmd+Option+C
Jump to Word Ctrl+Shift+G
Ctrl+ "/# Option+ "/# Move Active Cursor Up Ctrl+Alt+Shift+Up Ctrl+Option+Shift+Up Run the next chunk Ctrl+Alt+N Cmd+Option+N
Jump to Start/End Ctrl+!/$ Cmd+!/$ Move Active Cursor Down Ctrl+Alt+Shift+Down Ctrl+Opt+Shift+Down Sync Editor & PDF Preview Ctrl+F8 Cmd+F8
Toggle Outline Ctrl+Shift+O Cmd+Shift+O Find and Replace Ctrl+F Cmd+F Previous plot Ctrl+Alt+F11 Cmd+Option+F11
Use Selection for Find Ctrl+F3 Cmd+E Next plot Ctrl+Alt+F12 Cmd+Option+F12
Replace and Find Ctrl+Shift+J Cmd+Shift+J Show Keyboard Shortcuts Alt+Shift+K Option+Shift+K
RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at www.rstudio.com • RStudio IDE 0.1.0 • Updated: 2017-09