Ashish Mahabal AyBi199, Caltech 7 May 2009george/aybi199/Old... · 2009. 5. 8. · Ashish Mahabal...
Transcript of Ashish Mahabal AyBi199, Caltech 7 May 2009george/aybi199/Old... · 2009. 5. 8. · Ashish Mahabal...
R2
AshishMahabalAyBi199,Caltech
7May2009
Quickrecap
• ?Togethelp• #forcomments• ‐>or<‐(or=)forassignments• ==forcomparison
• Concatenatewithc #x=c(a,b)• seqtocreatesequence #z=seq(‐30,30)• is.na(x)tolookfornotavailableness
• summary(x)• data(x)• read.table(file)• save(file)• source(file) #input
• sink(file) #output
CommonrouSnes
• length(a) #lengthofvector• max(a) #similarlymin,mean
• sort(a),orsort(a,decreasing=T)• unique(a)• duplicated(a) #returnsalogicalarray!
TomShort’sRrefcardhWp://www.rpad.org/Rpad/R‐refcard.pdf
Example
• a=rnorm(100,mean=5,sd=1)• b=rnorm(200,mean=5,sd=1)
• c=c(100*a,100*b)• length(c)• c
• d=as.integer(c)• d• mean(d);max(d);min(d);sd(d)
• unique(d)• duplicated(d)• plot(duplicated(d)) #coercion
• duplicated(unique(d))• five(d) #whatsthefive‐of‐d• ?five #nothing?Doyouknowfive?• ??five #atleastsomethingthathasfive?• fivenum(d) #ah,soitsfivenum.I’lltakethat• ?fivenum #whatisitBTW?…minimum,lower‐hinge,median,upper‐hinge,maximum…
• fivenum(x,na.rm=TRUE) #opSons!• g=d• g[duplicated(d)]<‐NA• fivenum(d)– [1]264.0425.5483.5561.5742.0
• fivenum(g,na.rm=TRUE)– [1]264.0419.5492.0571.5742.0
• boxplot.stats(x,coef=1.5,do.conf=TRUE,do.out=TRUE)
• quanSle,range,bxp
• whichselectsindices:• a=which(d<500) #returnsindices• a=d[which(d<500)] #returnstheelements
• which.max(d) #indexofmaxelement
• match(x,y) #elementsofxiny,elseNA• merge(a,b) #usingcommoncolumns/rows
choose(n,k) #combinaSonsofkfromnchoose(5,3)returns10
sample(x,size) #resamplessizeelementsfromx(withtheopSonofreplacement)
• >sample(cards,5)[1]"S9""H4""D5""C3""C9"
• >sample(cards,2)
[1]"H10""D10"
• Conj(4+3i)• fft(x)• solve(a) #matrixinverseofa
• solve(a,b) #solvesa%*%x=bforx
Stringsanddate/Sme
• paste(…) #concatenaSngvectors• substr(x,start,stop) #canalsoassign• grep(paWern,x)• as.Date(s)
Graphicsdevices
• x11(),windows()• postscript(file)• png,pdf,jpeg,…
NextfewslidesfromhWp://scc.stat.ucla.edu/mini‐courses
Replacerbyd,p,qtogetprobdensity,cumulaSveprobdensityandthevalueofthequanSle
• pnorm(4,5,1) – #howmanybelow4formean5andsd1(asafracSon)
– [1]0.1586553• pnorm(6,5,1)–pnorm(4,5,1)– #between4and6– [1]0.6826895
• qnorm(0.9,5,1)– #90thpercenSle?– [1]6.281552
GeometricdistribuSon
• dgeom(4,0.35)– Probabilityoffirstsuccessonthe5thtrialgiventhattheprobabilityofsuccessonagiventryis0.35
– [1]0.06247719
Forafaircoinits0.5,0.25,0.125etc.pgeom(n,p)givestheprobabilityofsuccessunSlthatpoint.Soitwillbe0.5,0.75,0.875,…
BinomialdistribuSon
• dbinom(m,n,p)givesprobabilityofsuccessinmoutofnaWemptsgiventhatthesuccessatanygivenaWemptisp– dbinom(3,5,0.35) #successin3of5
• [1]0.1811469– sum(dbinom(3:5,5,0.35))#in3or4or5of5
• [1]0.2351694
StaSsScaltests
• shapiro.test Shapiro‐Wilk’sWtest(u)• ks.testKolmogorov‐Smirnov2‐sampletest(b)
• kruskal.test Kruskal‐Wallisk‐sampletest(b)
• wilcox.test Wilcoxonrank‐sumtest(b)
• cor.test BivariatecorrelaSoncoefficient(b)
• cov Covarience(b)
• rcorr CorrelaSonMatrix(m)
• prcomp PrincipleComponentAnalysis(m)
Linearregressionhelp(parSalsnapshot)
• library(Hmisc)• table<‐read.table(fname,header=F)• tposed=t(table)• rcorr(tposed,type="pearson”)• rcorr(tposed,type="spearman”)
CorrelaSonwithopSonSpearman
CorrelaSonwithopSonPearson
• library()• RCMDINSTALL‐llibpath~/Hmisc_3.6‐0.tar.gz
• Includethisinyour.Rprofilefile
PrincipalComponentAnalysis
• library(mva)• table<‐read.table(”fname",header=T)
• prcomp(table)
• summary(prcomp(table))
RediscoveringHRdiagram
• Hyadesstars(Hipparcusmaincatalog)• Mean/median/boxplot
• DensityesSmaSon(Histogram)
• Kernelsmoothing
• CorrelaSonmatrix
• X‐Yplot• MulSvariateclustering
HydStat(30Dec2004)AshishMahabal
• X‐YplotbetweenVmagandB‐Vrevealsthefamousstructureinthedataset:thecolor‐magnitudeofbrightstarsshowingthemainsequence,giantbranch(withredclumpstars),andafewHyadeswhitedwarfs.
FPofGlobularclusters
• MatrixofpairwisecorrelaSoncoefficients• Pairwiseplots• PrincipalComponentAnalysis
HydStat(30Dec2004)AshishMahabal
• Coreparametersasagrouptendtobehighlycorrelated,unlikethehalf‐lightparameters.ThisisindicaSveofthedynamicalevoluSondrivenbythecorecollapse.
• RGUI(RcommanderhWp://socserv.mcmaster.ca/jfox/Misc/Rcmdr/)
• hWp://vostat.org• hWp://scc.stat.ucla.edu/mini‐courses
• hWp://cran.r‐project.org• hWp://www.r‐project.org• hWp://en.wikipedia.org/wiki/R_(programming_language)