Академический Документы
Профессиональный Документы
Культура Документы
RFAQ:HowcanIcalculateMoran'sIinR?
HelptheStatConsultingGroupby
stat
>
>
faq
givingagift
>morans_i.htm
NOTE:Thispagehasbeendelinked.Itisnolongerbeingmaintained,andinformationonthispage
maybeoutofdate.Becausethispagehasbeendelinked,wecannotnotanswerquestions
regardingthispage.
RFAQ:
HowcanIcalculateMoran'sIinR?
Moran'sIisameasureofspatialautocorrelationhowrelatedthevaluesofavariablearebasedonthelocationswherethey
weremeasured.Usingfunctionsintheapelibrary,wecancalculateMoran'sIinR.Todownloadandloadthislibrary,enter
install.packages("ape")andthenlibrary(ape).
Let'slookatanexample.Ourdataset,ozone,containsozonemeasurementsfromthirtytwolocationsintheLosAngeles
areaaggregatedoveronemonth.Thedatasetincludesthestationnumber(Station),thelatitudeandlongitudeofthestation
(LatandLon),andtheaverageofthehighesteighthourdailyaverages(Av8top).Thisdata,andotherspatialdatasets,can
bedownloadedfromtheUniversityofIllinois'SpatialAnalysisLab.Wecanlookatasummaryofourlocationvariablestosee
therangeoflocationsunderconsideration.
ozone<read.table("http://www.ats.ucla.edu/stat/r/faq/ozone.csv",sep=",",header=T)
head(ozone,n=10)
StationAv8topLatLon
1607.22580634.13583117.9236
2695.89919434.17611118.3153
3724.05288533.82361118.1875
4747.18145234.19944118.5347
5756.07661334.06694117.7514
6843.15725833.92917118.2097
7855.20161334.01500118.0597
8874.71774234.06722118.2264
9886.53225834.08333118.1069
10897.54032334.38750118.5347
TocalculateMoran'sI,wewillneedtogenerateamatrixofinversedistanceweights.Inthematrix,entriesforpairsofpoints
thatareclosetogetherarehigherthanforpairsofpointsthatarefarapart.Forsimplicity,wewilltreatthelatitudeand
longitudeasvaluesonaplaneratherthanonasphereourlocationsareclosetogetherandfarfromthepoles.Whenusing
latitudeandlongitudecoordinatesfrommoredistantlocations,it'swisetocalculatedistancesbasedonsphericalcoordinates
(thegeospherepackagecanbeused).
Wecanfirstgenerateadistancematrix,thentakeinverseofthematrixvaluesandreplacethediagonalentrieswithzero:
ozone.dists<as.matrix(dist(cbind(ozone$Lon,ozone$Lat)))
ozone.dists.inv<1/ozone.dists
diag(ozone.dists.inv)<0
ozone.dists.inv[1:5,1:5]
12345
10.0000002.5397952.4461651.6275705.391160
22.5397950.0000002.6670614.5314281.741071
32.4461652.6670610.0000001.9543572.002389
41.6275704.5314281.9543570.0000001.258716
55.3911601.7410712.0023891.2587160.000000
Wehavecreatedamatrixwhereeachoffdiagonalentry[i,j]inthematrixisequalto1/(distancebetweenpointiandpointj).
Notethatthisisjustoneofseveralwaysinwhichwecancalculateaninversedistancematrix.Thisistheformulationused
byStata.InSAS,inversedistancematriceshaveentriesequalto1/(1+distancebetweenpointiandpointj)andthereare
numerousscalingoptionsavailable.
WecannowcalculateMoran'sIusingthecommandMoran.I.
http://www.ats.ucla.edu/stat/r/faq/morans_i.htm
1/2
10/28/2016
RFAQ:HowcanIcalculateMoran'sIinR?
Moran.I(ozone$Av8top,ozone.dists.inv)
$observed
[1]0.2265501
$expected
[1]0.03225806
$sd
[1]0.03431138
$p.value
[1]4.596323e14
Basedontheseresults,wecanrejectthenullhypothesisthatthereiszerospatialautocorrelationpresentinthevariable
Av8topatalpha=.05.
Alternatively,onecoulduseabinarydistancematrix.Thismightbeusedinlookingatnetworkingproblemswheretwopoints
areeitherconnectedornotconnected.Inthisexample,wecanchooseadistancedsuchthatpairsofpointswithdistance
lessthandareconsideredconnectedandpairswithdistancegreaterthandarenotandusethisweightsmatrixtocalculate
Moran'sI.Thecodebelowdoesthisford=.75.
ozone.dists.bin<(ozone.dists>0&ozone.dists<=.75)
Moran.I(ozone$Av8top,ozone.dists.bin)
$observed
[1]0.2045738
$expected
[1]0.03225806
$sd
[1]0.04771132
$p.value
[1]6.910876e07
Inthisexample,weseesimilarresultstothosegeneratedusingtheinverseEuclideandistancematrixandwecanrejectthe
nullhypothesisthatthereiszerospatialautocorrelationpresentinthevariableAv8topatalpha=.05.
Howtocitethispage
Reportanerroronthispageorleaveacomment
Thecontentofthiswebsiteshouldnotbeconstruedasanendorsementofanyparticularwebsite,book,orsoftwareproductbytheUniversityofCalifornia.
High Performance
Computing
Statistical Computing
ABOUT
2016 UC Regents
CONTACT
NEWS
HighPerformanceComputing
GIS
StatisticalComputing
Hoffman2Cluster
Mapshare
Classes
Hoffman2AccountApplication
Visualization
Conferences
Hoffman2UsageStatistics
3DModeling
ReadingMaterials
UCGridPortal
TechnologySandbox
IDREListserv
UCLAGridPortal
TechSandboxAccess
IDREResources
SharedCluster&Storage
DataCenters
SocialSciencesDataArchive
AboutIDRE
EVENTS
OUR EXPERTS
http://www.ats.ucla.edu/stat/r/faq/morans_i.htm
2/2