Formularios Stats

Descargar como pdf o txt
Descargar como pdf o txt
Está en la página 1de 16

Calculo de probabilidades

nombre función sintaxis (se debe añadir prefijo)


uniforme unif(x, min, max)
binomial binom(x, size, prob)
geometrica geom(x, prob)
poisson pois(x, lambda)
exponencial exp(x, rate )
normal norm(x, mean , sd)
t-student t(x, df, ncp)

Estadı́stica. GIST y GIT. Variable aleatoria unidimensional


Calculo de probabilidades

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

Estadı́stica. GIST y GIT. Variable aleatoria unidimensional


MODELOS ARIMA(p,d,q)
Universidad Politécnica de Cartagena
Dpto. Matemática Aplicada y Estadı́stica
(I − ϕ1 B − . . . − ϕp B p )(I − B)d Xt = c + (1 + θ1 B + . . . + θq B q )Zt
Estadı́stica Industrial. Pilar Sanmartı́n
FORMULARIO TEORIA equivalentemente:

FORMULAS RECURRENTES DE ALISADO EXPONENCIAL


(I − ϕ1 B − . . . − ϕp B p )(I − B)d (Xt − µtd /d!) = (1 + θ1 B + . . . + θq B q )Zt
ALISADO EXPONENCIAL SIMPLE (SIMPLE EXPONENTIAL SMOOTHING, SES)
c = µ(1 − ϕ1 − . . . − ϕp ) y µ es la media de (I − B)d Xt .
at = αxt + (1 − α)at−1 0<α<1

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

bt = β ∗ (at − at−1 ) + (1 − β ∗ )bt−1 SCDG(K) − SCDG(K + 1)


F =
SCDG(K + 1)/(n − K − 1)
x̂t+h|t = at + bt h
METODO DE HOLT-WINTERS ADITIVO
introducir un grupo más si el cociente F es mayor que 10, con K número de grupos.

x̂t+h|t = at + hbt + s1t+h−m(k+1)

at = α(xt − st−m ) + (1 − α)(at−1 + bt−1 )

bt = β ∗ (at − at−1 ) + (1 − β ∗ )bt−1

st = γ(xt − at−1 − bt−1 ) + (1 − γ)st−m


METODO DE HOLT-WINTERS MULTIPLICATIVO.

x̂t+h|t = (at + hbt )st+h−m(k+1)

at = α(xt /st−m ) + (1 − α)(at−1 + bt−1 )

bt = β ∗ (at − at−1 ) + (1 − β ∗ )bt−1


st = γ (at−1x+b
t
t−1 )
+ (1 − γ)st−m
Universidad Politécnica de Cartagena CALCULO VECTORES PROPIOS ASOCIADOS y PUNTUACIONES
Dpto. Matemática Aplicada y Estadı́stica
Estadı́stica Industrial. Pilar Sanmartı́n prcomp(datos)
FORMULARIO R ANALISIS ind <- get_pca_ind(datos_PCA)
DE COMPONENTES PRINCIPALES puntuaciones<-ind$coord
head(puntuaciones[,i])
LIBRERIAS
# podemos seleccionar coordenadas
library("FactoMineR") fviz_pca_ind(datos_PCA, col.ind = "cos2",
library("factoextra") gradient.cols = c("blue", "orange", "red"),
library(corrplot) repel = TRUE # evita solapamiento de texto
ESTUDIO DE LAS VARIANZAS Y CORRELACIONES
)
#representacion grafica
R_datos<-cor(datos, method="pearson")
#calculo de la matriz de correlaciones CALCULO CARGAS FACTORIALES

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

R_datos<-cor(datos, method="pearson") #hay que usar directamente la matriz de datos


#calculo de la matriz de correlaciones datos_pa_rot <- fa(datos, nfactors=p,fm="pa",rotate="varimax",
S_datos<-cov(datos) scores="regression")
#calculo de la matriz de covarianzas datos_pa_rot$scores
#test de Bartlett
VALIDACION EX POST
cortest.bartlett(R_datos,n=numero observaciones)
#con matriz de correlaciones
#diferencia entre matriz observada y reproducida
cortest.bartlett(datos)
datos_pca_rot$residual
#con la matriz de datos
#RMS
#Medida de adecuacion muestral de Kaiser-Meyer-Olkin
datos_pca_rot$rms
KMO(R_datos)
#grafico de sedimentacion
scree(R_datos,factors=FALSE,pc=TRUE,hline=NULL,add=FALSE)

EXTRACCION DE FACTORES: METODO DE COMPONENTES PRINCIPALES.

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

EXTRACCION DE FACTORES: METODO DE MAXIMA VEROSIMILITUD

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

#deteccion multivariante de datos atipicos


mean.datos<-colMeans(matriz.datos) hclust.lejano<-hclust(datos.dis.euclid2,method="complete")
Sx.datos<-cov(matriz.datos) data.frame(hclust.lejano[2:1])
D2<-mahalanobis(matriz.datos,mean.datos,Sx.datos,inverted=FALSE)
print(round(D2,2)) #p=numero variables plot(hclust.lejano)
#valor crı́tico rect.hclust(hclust.lejano, k = 2, border = "red")
qchisq(0.99,df=p) # k= numero de grupos a retener
#pvalores
round(pchisq(D2,df=p,lower.tail=FALSE),2) METODO DE WARD

CALCULO DE LA MATRIZ DE DISTANCIAS


hclust.ward<-hclust(datos.dis.euclid2,method="ward.D")
data.frame(hclust.ward[2:1])
#matriz de distancias
plot(hclust.ward, ylim=c(0,30))
library(stats)
rect.hclust(hclust.ward, k = 2, border = "red")
datos.dis.euclid<-dist(matriz.datos,method="euclidean", diag=TRUE)
#calculo de la distancia euclidea al cuadrado
METODOS NO JERARQUICOS
datos.dis.euclid2<-(datos.dis.euclid)^2
round(print(datos.dis.euclid2),2)
kmeans.datos<-kmeans(scale(datos), k)
METODO DEL CENTROIDE #k numero de grupos
#scale() para tipificar variables
hclust.centroide<-hclust(datos.dis.euclid2,method="centroid") kmeans.autonomias

#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

#para comparar analisis con k y k+1 grupos


#n el numero de datos
scdg_K<-kmeans.k.datos$tot.withinss
scdg_(K+1)<-kmeans.(k+1).datos$tot.withinss
F<-(scdg_K-scdg_(K+1))/(scdg_(K+1)/(n-K-1))
print(F)
#metodo del codo
#SCDG(k) en funcion de k
library(factoextra)
fviz_nbclust(scale(datos), kmeans, method = "wss")

ANOVA PARA VER INFLUENCIA DE LAS VARIABLES

library(psych)
fit.x_i<-aov(varx~kmeans.datos$cluster,data=datos)
summary(fit.x_i)
#x_i una de las variables considerada

VALORES INICIALES SEMILLAS

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

AJUSTE TENDENCIA (SERIE SIN COMPONENTE ESTACIONAL O DESESTACIONALIZADA)


library(fpp)
library(stats)
tiempo<-c(1:length(Dt.1))
CARACTERISTICAS PRINCIPALES ajuste.1<-lm(Dt.1~tiempo)
summary(ajuste.1)
class(serie_data) #ajuste lineal
start(serie_data) tiempoh<-length(Dt.1)+c(1:h)
end(serie_data) #h=12 (componente estacional periodo 12)
frequency(serie_data) predicciones.1<-a0+a1*tiempoh+a.1$figure
# caracteristicas de la serie temporal predicciones.1
ts.plot(serie_data,main="titulo",xlab="ejex", ylab="ejey")
plot(aggreagate(serie_data)) AJUSTE TENDENCIA (SERIE SIN COMPONENTE ESTACIONAL O DESESTACIONALIZADA)
#grafico con la serie agregada por frecuencia
tiempo<-c(1:length(Dt.1))
GRAFICOS PRELIMINARES
tiempo2<-tiempo^2
ts.plot(serie_data,main="titulo",xlab="ejex", ylab="ejey") ajuste.1.2<-lm(Dt.2~tiempo+tiempo2)
plot(aggreagate(serie_data)) summary(ajuste.1.2)
#grafico con la serie agregada por frecuencia #ajuste cuadratico
cycle(serie_data) #h=12 (componente estacional periodo 12)
boxplot(serie_data~cycle(serie_data)) tiempoh<-length(Dt.1)+c(1:h)
# diagrama de cajas y bigotes por componente estacional tiempoh2<-tiempoh^2
medias.an<- aggregate(serie_data,nfrequency=1,FUN=mean) predicciones.1.2<-(a0+a1*tiempoh+a2*tiempoh2) *a.1$figure
detip.an<- aggregate(serie_data,nfrequency=1,FUN=sd) predicciones.1.2
plot(medias.an,detip.an)
AJUSTE TENDENCIA (SERIE SIN COMPONENTE ESTACIONAL O DESESTACIONALIZADA)
#grafico medias vs desviaciones

AJUSTE CLASICO. ESQUEMA ADITIVO


tiempo<-c(1:length(Dt.1))
logDt.1<-log(Dt.1)
ajuste.1.3<-lm(logDt.1~tiempo)
a.1<-decompose(serie_data,type="additive") summary(ajuste.1.3)
plot(a.1) #ajuste exponencial
a.1$trend #h=12 (componente estacional periodo 12)
# tendencia predicciones.3<-exp((a0+a1*tiempoh))+a.1$figure
a.1$seasonal predicciones.3
a.1$figure
AJUSTE CLASICO. ESQUEMA MULTIPLICATIVO AJUSTE TENDENCIA (SERIE SIN COMPONENTE ESTACIONAL O DESESTACIONALIZADA)

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

AJUSTE TENDENCIA (SERIE SIN COMPONENTE ESTACIONAL O 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

AJUSTE TENDENCIA (SERIE SIN COMPONENTE ESTACIONAL O DESESTACIONALIZADA)

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

h.fit <- holt(serie.data, alpha=NULL, beta=NULL, initial="optimal", h=k)


ˆ Modelos de Espacio de Estados ETS(1,2,3): # k predicciones futuras
summary(h.fit) #modelo ajustado
1. Error aleatorio: A (aditivo), M (Multiplicativo), Z (selecciona el
h.fit$mean
algoritmo)
#predicciones
2. Tendencia-nivel: A (aditivo), M (Multiplicativo), N (no tiene), Z plot(h.fit)
(selecciona el algoritmo) #representacion del modelo y predicciones
3. Estacionalidad: A (aditivo), M (Multiplicativo), N (no tiene), Z
METODO DE HOLT-WINTERS ADITIVO
(selecciona el algoritmo)

ˆ Métodos como ETS: hw.fit<-hw(serie.data,seasonal="additive",initial="optimal",h=k)


# k predicciones futuras
1. Alisado Exponencial Simple: ETS(A,N,N) summary(hw.fit) #modelo ajustado
hw.fit$mean
2. Método de Holt : ETS(A,A,N).
#predicciones
3. Método de Holt-Winters Aditivo : ETS(A,A,A). plot(hw.fit)
4. Método de Holt-Winters Multiplicativo: ETS(A,A,M). (por #representacion del modelo y predicciones
defecto se aplica ETS(M,A,M) )

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))

(para usar verosimilitud e intervalos)


#ETS(M,A,M)
hw.fit<-hw(serie.data,seasonal="multiplicative",initial="optimal",h=k) #ETS(A,A,M)
# k predicciones futuras hw.fit<-ets(serie.data,"AAM",restrict=FALSE)
summary(hw.fit) #modelo ajustado summary(hw.fit)
hw.fit$mean #representacion del modelo y k predicciones
#predicciones forecast(hw.fit,h=k)
plot(hw.fit) plot(forecast(hw.fit,h=k))
VALIDACION DEL MODELO
Universidad Politécnica de Cartagena
Dpto. Matemática Aplicada y Estadı́stica
residuos<-residuals(fit)
Estadı́stica Industrial. Pilar Sanmartı́n
checkresiduals(residuos)
FORMULARIO R MODELOS ARIMA
hist(residuos)
qqnorm(residuos)
LIBRERIAS
#test de Shapiro-Wilk
shapiro.test(residuos)
library(forecast) tsdisplay(residuos)
library(fpp) #test de Ljungbox
library(fpp2) Box.test(residuals(fit), lag = 1, type = "Ljung-Box")
library(fpp3)
library(stats) PREDICCIONES

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

AJUSTE DEL MODELO

fit <- Arima(data, order=c(p,d,q))


#ajuste del modelo ARIMA(p,d,q) seleccionado
fit<-auto.arima(data, seasonal=FALSE)
#seleccion automática de modelo no estacional
fit<-auto.arima(data,seasonal=FALSE,stepwise=FALSE,approximation=FALSE)
#para una busqueda más exhaustiva
summary(fit)
#resumen del modelo
Base R Vectors Programming
Creating Vectors For Loop While Loop
Cheat Sheet c(2, 4, 6) 2 4 6
Join elements into
for (variable in sequence){ while (condition){
a vector

Do something Do something
Getting Help An integer
2:6 2 3 4 5 6
sequence } }

Accessing the help files seq(2, 3, by=0.5) 2.0 2.5 3.0


A complex Example Example
sequence
?mean for (i in 1:4){ while (i < 5){
Get help of a particular function. rep(1:2, times=3) 1 2 1 2 1 2 Repeat a vector
j <- i + 10 print(i)
help.search(‘weighted mean’)
print(j) i <- i + 1
Search the help files for a word or phrase. rep(1:2, each=3) 1 1 1 2 2 2 Repeat elements
of a vector
help(package = ‘dplyr’) } }
Find help for a package. Vector Functions
More about an object If Statements Functions
sort(x) rev(x)
Return x sorted. Return x reversed. if (condition){ function_name <- function(var){
str(iris)
table(x) unique(x) Do something
Get a summary of an object’s structure. Do something
See counts of values. See unique values. } else {
class(iris) Do something different return(new_variable)
Find the class an object belongs to. } }
Selecting Vector Elements
Example
Using Packages Example
By Position if (i > 3){ square <- function(x){
install.packages(‘dplyr’) x[4] The fourth element. print(‘Yes’)
squared <- x*x
Download and install a package from CRAN. } else {
print(‘No’) return(squared)
library(dplyr) x[-4] All but the fourth.
} }
Load the package into the session, making all
its functions available to use. x[2:4] Elements two to four.
Reading and Writing Data Also see the readr package.
dplyr::select All elements except
x[-(2:4)] Input Ouput Description
Use a particular function from a package. two to four.

Elements one and Read and write a delimited text


data(iris) df <- read.table(‘file.txt’) write.table(df, ‘file.txt’)
x[c(1, 5)] file.
five.
Load a built-in dataset into the environment.
By Value Read and write a comma
Working Directory x[x == 10]
Elements which df <- read.csv(‘file.csv’) write.csv(df, ‘file.csv’) separated value file. This is a
special case of read.table/
are equal to 10.
write.table.
getwd()
All elements less
Find the current working directory (where x[x < 0]
than zero. Read and write an R data file, a
inputs are found and outputs are sent). load(‘file.RData’) save(df, file = ’file.Rdata’)
file type special for R.
x[x %in% Elements in the set
setwd(‘C://file/path’) c(1, 2, 5)] 1, 2, 5.
Change the current working directory.
Named Vectors Greater than
a == b Are equal a > b Greater than a >= b is.na(a) Is missing
or equal to
Conditions
Use projects in RStudio to set the working Element with Less than or
x[‘apple’] a != b Not equal a < b Less than a <= b is.null(a) Is null
directory to the folder you are working in. name ‘apple’. equal to

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.

Integers or floating point


w
ww Matrix Multiplication toupper(x) Convert to uppercase.

ww
as.numeric 1, 0, 1
numbers.

w m[2, 3] - Select an element


solve(m, n)
Find x in: m * x = n
tolower(x) Convert to lowercase.
as.character '1', '0', '1'
Character strings. Generally

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.

rm(x) Remove x from the


environment. df[2, ]
ncol(df)
Number of
Plotting Also see the ggplot2 package.

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

Debug Mode Version Control with Git or SVN


package with detach() installed library

RStudio opens documentation in a dedicated Help pane


Launch debugger Open traceback to examine Turn on at Tools > Project Options > Git/SVN
Open with debug(), browser(), or a breakpoint. RStudio will open the mode from origin the functions that R called Push/Pull View
Stage Show file Commit
debugger mode when it encounters a breakpoint while executing code. of error before the error occurred diff staged files to remote History
files:
Home page of Search within Search for
Click next to A
• Added helpful links help file help file
line number to D• Deleted
add/remove a M• Modified Viewer Pane displays HTML content, such as Shiny apps,
R• Renamed Open shell to current
breakpoint. RMarkdown reports, and interactive visualizations
?• Untracked type commands branch

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

También podría gustarte