Docsity

Comandi R - statistical methods, Formulari di Statistica

Toggle sidebar
bg1
R- commands
INSERIRE LA DIRECTORY: File-> Cambia directory…-> Seleziona la cartella
MOSTRA L’ATTUALE DIRECTORY: getwd()
MOSTRA I FILE CONTENUTI NELLA DIRECTORY: list.files()
CARICA UN FILE: load(“namefile”)
CARICA LE FUNZIONI: library(MASS)
COMANDI PER RAPPRESENTAZIONI GRAFICHE:
ISTOGRAMMA: hist(namevariable)
SCATTERPLOT: plot (namematrix$variable1, namematrix$variable2)
BOXPLOT WITH JUST ONE VARIABLE: boxplot(namematrix$variable)
BOXPLOT WITH MORE VARIABLES: boxplot(namematrix$variable1, namematrix$variable2)
GRAPHICAL DISPLAY OF CORRELATION MATRIX: pairs(name matrix)
abline(nome matrice)
COMANDI UTILI PER LA PCA:
Dopo aver inserito la directory e il set di dati, cerchiamo di trovare le prime informazioni sui dati:
WORKSPACE: ls()
LE PRIME 6 RIGHE DELLA MATRICE: head (namematrix)
DIMENSIONE DELLA MATRICE: dim(namematrix)
RIASSUNTO DELLE INFORMAZIONI: summary(namematrix)
oppure
Oppure potrebbe dare un set di dati tipo, quindi in questo caso dobbiamo creare prima singolarmente i
vettori e poi creare la matrice. Esempio:
CREARE UN VETTORE COLONNA:
x1<- c (45, 69, 58, 58, 62)
x2<- c (2.23, 2.69, 2.14, 8.25, 2.12)
x3<- c (154, 186, 136, 201, 222)
CREARE UNA MATRICE CON I SUDDETTI VETTORI COLONNA:
A<- cbind(x1,x2,x3)
pf3
pf4
pf5

Anteprima parziale del testo

Scarica Comandi R - statistical methods e più Formulari in PDF di Statistica solo su Docsity!

R- commands

INSERIRE LA DIRECTORY : File-> Cambia directory…-> Seleziona la cartella

MOSTRA L’ATTUALE DIRECTORY: getwd()

MOSTRA I FILE CONTENUTI NELLA DIRECTORY: list.files()

CARICA UN FILE: load(“ namefile ”)

CARICA LE FUNZIONI: library(MASS)

COMANDI PER RAPPRESENTAZIONI GRAFICHE:

ISTOGRAMMA : hist( namevariable )

SCATTERPLOT: plot ( namematrix $ variable1, namematrix$variable2 )

BOXPLOT WITH JUST ONE VARIABLE : boxplot( namematrix $ variable )

BOXPLOT WITH MORE VARIABLES : boxplot( namematrix $ variable1, namematrix$variable2 )

GRAPHICAL DISPLAY OF CORRELATION MATRIX: pairs( name matrix )

abline(nome matrice)

COMANDI UTILI PER LA PCA:

Dopo aver inserito la directory e il set di dati, cerchiamo di trovare le prime informazioni sui dati:

WORKSPACE: ls()

LE PRIME 6 RIGHE DELLA MATRICE: head ( namematrix )

DIMENSIONE DELLA MATRICE: dim(namematrix)

RIASSUNTO DELLE INFORMAZIONI : summary(namematrix)

oppure

Oppure potrebbe dare un set di dati tipo, quindi in questo caso dobbiamo creare prima singolarmente i

vettori e poi creare la matrice. Esempio:

CREARE UN VETTORE COLONNA:

x1<- c (45, 69, 58, 58, 62)

x2<- c (2.23, 2.69, 2.14, 8.25, 2.12)

x3<- c (154, 186, 136, 201, 222)

CREARE UNA MATRICE CON I SUDDETTI VETTORI COLONNA:

A<- cbind(x1,x2,x3)

STANDARDIZZARE I VALORI : scale ( namematrix )

CREARE COVARIANCE MATRIX: var ( nomedellamatricestandardizzata )

CREARE CORRELATION MATRIX: cor ( namematrix )

SPECTRAL DECOMPOSITION OF THE CORRELATION MATRIX: eigen( nomecorrelationmatrix )

In questo modo si ottengono due matrici:

  • Eigen Values: il cui numero è pari al rank e al numero dei p.c. che possiamo ottenere
  • Eigen Vectors: matrice il cui numero di righe è pari al numero del rank e il numero delle colonne è pari al numero dei covariates.

DETERMINATE

CREARE LA PCA SENZA ALCUN PASSAGGIO INTERMEDIO:

prcomp(A)

CALCOLARE LA PCA:

  1. soprannominare la funzione: A<-eigen(namecorrelationmatrix)
  2. creare il vettore degli eigen vectors: v1<-A$vectors [,1]
  3. outer product tra la matrice dei valori standardizzati Z e il vettore degli eigen vectors v1: PC1<-Z%%v*

SCREEPLOT: graphical representation of the variability of each principal component on the total variability.

plot(nome della matrice degli eigen$values, type=”o”)

OPERAZIONI CON LE MATRICI:

MATRICE INVERSA: solve( namematrix )

DETERMINANTE : det( namematrix )

MATRICE TRASPOSTA: t(namematrix)

INNER PRODUCT: t(a) %*% a

OUTER PRODUCT: a %*% t(a)

OPERAZIONI LDA

PER INSERIRE I DATI:

list.files()

read.table("datiSenil.txt")

read.table("datiSenil.txt", header=TRUE)

Dove “a” è il nome della matrice originaria

CALCOLARE SOMMA RESIDUALS : sum(e) (deve venire circa 0)

CALCOLARE DEVIANZA DI E : sum(e^2)

CALCOLARE LA DEV(R): sum [(nomevariabileY-mediavariabileY)^2]

CALCOLARE R^2: 1-[DEV(E)/DEV(R)]

COMANDO PER SINTETIZZARE TUTTO: lm( nomevariabileY~nomevariabileX )

# LOGIT MODEL ----

Regression model for binary variable, describing a sinusoidal S-shape curve instead of a linear curve, using

the logit distribution function (similar but lower to the normal distribution) # μ = (e^(βo+β1X1+…+βpXp))/(1+(e^(βo+β1X1+…+βpXp))).

It is needed because the using a normal LRM would produce problem of prediction (fitted value outside

the 0-1 range of the binary variable) and heteroscedasticity (the opposite of the omosckedasticity required by the LDM).

It can be also done with the normal distribution , but in this case there is no explicit formulation, you

can't easily explain the parameters and you can't obtain odds ratio.

No R^

# Odds #Ratio between probability of success (G1) and probability of unsuccess (G0). #"The probability of G1 is Odds more times the probability of G0" #Larger it is, larger the probability of success is. #If 1 there is no association.

ψx = Pr(Y=G1)/Pr(Y=G0) = μ / (1-μ)

# Odds ratio # ψx|A/ψx|B. #Ratio between probability of G1 for category A and probability of G1 for category B. #"Moving from A to B increase the possibility of G1 of OddsRatio100%" (categorical variable), "The probability of G1 increase of OddsRation100% for unit increase of X" (numerical variable). #Can vary from 0 to +INF, with a range from 0 to 1 identifying negative association, 1 identifying no association and a range from 1 to +INF identifying positive association. exp(b1') # Inverse Odds Ratio # ψx|B/ψx|A. #"Moving from A to B decrease the possibility of G1 of InverseOddsRatio100%" (categorical variable), "The probability of G1 decrease of OddsRation100% for unit increase of X" #Useful to have a better understanding of Odds Ratio when β1 is negative. 1-exp(b1') # LM LM<-glm(G~X+factor(Y), family=binomial(logit)) #1. #G is the binary variable #Factor() produces dummy variable and is needed only for non-binary categorical variable. #To use the normal distribution, instead of logit set probit. sLM<-Summary(LM) #2. sLM$coefficients #Estimate (Intercept) #Regression coefficient βo'. #Average value when all the variables are 0. sLM$coefficients #Estimate (X) #As LMR's estimate (X). #A negative value would produce an Odds Ration under 1 and so identify a negative association (and vice versa; the same for the others Coefficients Estimates). Alessandro Li Vigni

sLM$coefficients #Estimate (Y(B)) #Regression coefficient β2' for the dummy variable Y from A to B. sLM$coefficients #Estimate (Y(C)) #Regression coefficient β2' for the dummy variable Y from A to C. #The inverse odds ratio would be from C to A. sLRM$coefficients #z value #As LRM's t value. sLRM$coefficients #Pr(>|t|) #As LRM's Pr(>|t|). #To define the best covariates patterns (highest overall probability of G1) is needed to take all negatively-associate covariates' lowest value and all the positively- associate covariates' highest value, and ordering them in decreasing order starting from the one with the highest t value LRMs$coefficients #Signif. codes #As LRM's Signif. codes. # Covariates pattern probability #Useful to identify the probability of success of a theoretical observation with certain value in the covariates. C<-exp(b0+…) #1 #For numerical covariates, it's needed to add the relative regression coefficient multiplied for the value which we want to set. #For categorical covariates, it's needed to add the regression coefficient of the category which we want to set #If we want to set a covariate to the base value, it's needed to omit its coefficient. (C/1+C)*100 #2.