MATLAB R Dictionary

17
MATLAB/RDic,onary RmeetupNYC January72010 HarlanHarris [email protected] @HarlanH MarckVaisman [email protected] @wahalulu MAT LABandtheMA TLABlogoarer egisteredtrad emarksofTheMathworks.

Transcript of MATLAB R Dictionary

Page 1: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 1/17

MATLAB/RDic,onary

RmeetupNYCJanuary72010

HarlanHarris

[email protected]

@HarlanH

MarckVaisman

[email protected]

@wahalulu

MATLABandtheMATLABlogoareregisteredtrademarksofTheMathworks.

Page 2: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 2/17

AboutMATLAB

WhatisMATLAB

•  Commercialnumerical

programminglanguage

simula,onandvisualiza,on•  Onemillionusers(engineers

scien,stsacademics)

•  MATrixLABoratory–

specializesinmatrix

opera,ons

•  Mathworks-base&add-ons

•  Open-sourceOctaveproject

MATLABHistory

•  DevelopedbyCleveMoler

(Math/CSProfatUNM)inthe

1970’sasahigher-levelnumericalprogramming

language(vs.FortranLINPACK)

•  Adoptedbyengineersfor

signalprocessingcontrol

modeling•  Mul,purposeprogramming

language

Page 3: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 3/17

Notes

•  Today’sfocus:CompareMATLAB&Rfordata

analysiscontrastasprogramminglanguages

•  MATLABisBaseplusmanytoolboxes

 –  Baseincludes:descrip,vestatscovarianceand

correla,onlinearandnonlinearregression

 –  Sta,s,cstoolboxadds:datasetandcategory(like

data.framesandfactors)arraysmorevisualiza,ons

distribu,onsANOVAmul,variateregressionhypothesistests

Page 4: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 4/17

->

•  Interac,veprogramming:ScriptsandRead-Evaluate-

PrintLoop

•  Similarrepresenta,onsofdata

 –  Bothusevectors/arraysastheprimarydatastructures•  Matlabisbasedon2-Dmatricies;Risbasedon1-Dvectors

 –  Bothprefervectorizedfunc,onsto for loops

 –  Variablesaredeclareddynamically

•  CandomostMATLABfunc,onalityinR;candomost

Rfunc,onalityinMATLAB.

Page 5: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 5/17

Thebasics:vectorsmatricesandindexing

Task

Createarowvector v=[1234] v<-c(1,2,3,4)

Createacolumnvector v=[1;2;3;4]orv=[1234]’ v<-c(1,2,3,4)Note:Rdoesnotdistinguish

betweenrowandcolumnvectors

EnteramatrixA A=[123;456] Entervaluesbyrow:

A<-matrix(c(1,2,3,4,5,6),

nrow=2,byrow=TRUE)Entervaluesbycolumn:

A<-matrix(c(1,4,2,5,3,6),nrow=2)

Accessthirdelementofvectorv v(3) v[3]orv[[3]]

AccesselementofmatrixA A(2,3) A[2,3]

“Glue”twomatricesa1anda2

samenumberofrowssidebyside

A=[a1a2] A<-cbind(a1,a2)

“Stack”twomatricesa1anda2

samenumberofcolumns

A=[a1;a2] A<-rbind(a1,a2)

Reshape*matrixAmakingitanm

xnmatrixwithelementstaken

columnwisefromA

A=reshape(A,m,n) dim(A)<-c(m,n)

Page 6: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 6/17

Operators

Task

Assignment = <-or=

WholeMatrixOpera,ons: Multiplication:A*BSquarethematrix:A^2Raisetopowerk:A^k

A%*%BA%*%AA%*%A%*%A…

Element-by-element

Opera,ons:

A.*BA./B

A.^k

A*BA/B

A^k

ComputeA-1B A\B A%*%solve(B)

Sums Columnsofmatrix:sum(A)Rowsofmatrix:sum(A,2)

colSums(A)rowSums(A)

Logicaloperators(element-by-

elementonvectors/matrices)

a<b,a>b,a<=b,a>=ba==b

a~=bAND:a&&b

OR:a||b

XOR:xor(a,b)NOT:~a

a<b,a>b,a<=b,a>=ba==b

a!=bAND:a&&b(short-circuit)a&b(element-wise)OR:a||ba|b

XOR:xor(a,b)NOT:!a

Page 7: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 7/17

Workingwithdatastructures

Task

Buildastructurevoflengthn

capableofcontainingdifferent

datatypesindifferentelements.

MATLAB:cellarray

R:list

v=cell(1,n)Ingeneral,cell(m,n)makesanm×ncell

array.Thenyoucandoe.g.:v{1}=12

v{2}=’hithere’

v{3}=rand(3)

v<-vector(’list’,n)Thenyoucandoe.g.:

v[[1]]<-12v[[2]]<-’hithere’

v[[3]]<-matrix(runif(9),3)

Createamatrix-likeobjectwith

differentnamedcolumns.

MATLAB:structarray

R:data.frame

avals=2*ones(1,6);yvals=6:-1:1;v=[153237];

d=struct(’a’,avals,’yy’,yyvals,’fac’,v);

v<-c(1,5,3,2,3,7)d<-data.frame(cbind(a=2,

yy=6:1),v)

Page 8: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 8/17

Condi,onalscontrolstructuresloops

Task

or loopsovervaluesinvector

v

fori=vcommand1

command2end

Ifonlyonecommand:for(iinv)

command

Ifmultiplecommands:

for(iinv){command1command2

}

If/elsestatement ifcondcommand1command2

elsecommand3command4

end

MATLABalsohastheelseif

statement.

if(cond){command1command2

}else{command3command4

}

Ruseschained“elseif”

statements.

ifelse()func,on >print(ifelse(c(T,F),2,3))[1]23

Page 9: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 9/17

Help!

Task

Gethelponafunc,on helpfminsearch help(pmin)or

?pmin

Searchthehelpforaword lookforinverse ??inverse

Describeavariable class(a) class(a)str(a)

Showvariablesinenvironment who ls()

Underlyingtypeofvariable whos(‘a’) typeof(a)

Page 10: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 10/17

Example:k-meansclusteringofFisherIrisdataFisherIrisDataset

sepal_length,sepal_width,petal_length,petal_width,species5.1,3.5,1.4,0.2,setosa4.9,3.0,1.4,0.2,setosa

4.7,3.2,1.3,0.2,setosa4.6,3.1,1.5,0.2,setosa…

Page 11: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 11/17

MatlabandRasprogramminglanguages

Scrip,ngreal-,meanalysis Scrip,ngreal-,meanalysis

File-basedenvironments Filesunimportant

Impera,veprogrammingstyle Func,onalprogrammingstyle(impure)

Sta,callyscoped Dynamicallyscoped

Func,onswithmul,plereturnvalues Func,onswithnamedargumentslazy

evalua,on

EvolvingOOPsystem Mul,plecompe,ngOOPsystems

Canbecompiled Cannotbecompiled

Largelibraryoffunc,ons

Professionaldevelopedcostmoney

Largelibraryoffunc,ons

Varyingqualityandsupport

Canembed(in)manyotherlanguages Canembed(in)manyotherlanguages

Page 12: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 12/17

Func,ons

function[a,b]=minmax(z)%onefunctionper.mfile!%assigntoformalreturnnamesa=min(z)b=max(z)

end

%ifminmax.minpath[smallest,largest]=…

minmax([1303])

minmax<-function(c,opt=12){#functionsareassignedto#variablesret<-list(min=min(z),max=max(z))

ret#laststatementis#returnvalue}

#ifminmaxwascreatedincurrent#environmentx<-minmax(c(1,30,3))

smallest<-x$min

Page 13: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 13/17

Page 14: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 14/17

Othernotes

•  r.matlabpackage

•  Graphics

 –  Matlabhasmuchbeer3-d/interac,vegraphicssupport

 –  Rhasggplot2andmuchbeersta,s,calgraphics

Page 15: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 15/17

Addi,onalResources

•  WillDwinellDataMininginMATLAB

•  Computerworldar,cleonCleveMoler

•  Mathworks

•  Matlabcentral

•  ComparisonofDataAnalysispackages(hp://anyall.org/blog/2009/02/comparison-of-data-analysis-packages-r-matlab-scipy-excel-sas-spss-

stata/)•  R.matlabpackage

•  stackoverflow

Page 16: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 16/17

Referencesusedforthistalk

•  DavidHiebelerMATLAB/RReferencedocument:

hp://www.math.umaine.edu/~hiebeler/comp/

matlabR.html

•  hp://www.cyclismo.org/tutorial/R/index.html

•  hp://www.stat.berkeley.edu/~spector/R.pdf 

•  MATLABdocumenta,on

•  hp://www.r-cookbook.com/node/23

Page 17: MATLAB R Dictionary

8/6/2019 MATLAB R Dictionary

http://slidepdf.com/reader/full/matlab-r-dictionary 17/17

ThankYou!