The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 327 Weeks Ago, 5 Days Ago |
| Questions Answered: | 12843 |
| Tutorials Posted: | 12834 |
MBA, Ph.D in Management
Harvard university
Feb-1997 - Aug-2003
Professor
Strayer University
Jan-2007 - Present
Hi please can anyone help me out on writting the pseudo code for the Morans I in calculating spatial correlation. This is what I have done so far is attached together with the python code of the morans I, but please I need help with what I have done so far and feedback. Please you can do any edit for what I have done.
Thank you so much
# # morans.py # # A program to calculate Morans I - index of spatial autocorrelation. # I = (n/sum of weights)* (sum (weight_xy*meandiff_x*meandiff_y)/ sum(meandiff_x^2) # where for each cell x, weight = 1 if neighbour with y (i.e. within kernel) # *Demonstrates use of 3x3 kernels on 2D numpy arrays* # # Author: Tony Moore # # Date: August 2014 # # Import numpy, as you would any other library in Python (note the definition of 'np' as an alias) import numpy as np def moran(ar): TYPE = 'A' # this means that we're going to evaluate autocorrelation of *agricultural* areas sum_weight = 0 run_tot = 0.0 # first time to work out the mean (TYPE = 1; everything else - 'W', 'U' - is 0) for i in range(ar.shape[0]): for j in range(ar.shape[1]): if ar[i,j]==TYPE: run_tot = run_tot + 1 mean = run_tot/ ar.size numerator = 0 denominator = 0 for i in range(1,ar.shape[0]-1): for j in range(1,ar.shape[1]-1): if ar[i,j]== TYPE: meandiff_x = 1 - mean else: meandiff_x = - mean # This is where the kernel comes in - in the loop it ranges from the cell before (-1) # to the cell after (+1) in both x and y, defining a 3x3 window for k in range(-1, 1): for l in range(-1,1): if k != 0 and l != 0: if ar[i+k,j+l]== TYPE: meandiff_y = 1- mean else: meandiff_y = -mean numerator = numerator + (meandiff_x*meandiff_y) denominator = denominator + (meandiff_x*meandiff_x) sum_weight = sum_weight + 1 return (ar.size/sum_weight)*(numerator/denominator)
Pseudo Code for Morans I
Import numpy Library
Define a function “moran” with an argument named “ar” for array
Define variable “Type” as “A” (evaluate autocorrelation of agricultural areas)
Assign sum weight = 0
Assign run total = 0.0
Nested for loop - loop through each array ending up with the running total
Loop iterate through the array until reach the range defined
If within array (i, j) = Type
New run total calculation is done where “run_tot = run_tot +1”
Define new variable named “mean” to calculate the total size of datasets
mean = run_tot/ar.size
Assign numerator = 0
Assign denominator = 0
Nested for Loop, define the location of i and j assign to a specific range of cell on the x-axis
Loop iterate through the array until its reach the range specified
If location within array = Type
Calculate mean difference of x-axis = “1 – mean (calculated above)”
Else if location not = Type
Display mean difference of x-axis as the negative version of the “mean”
Nested Loop look at each cell produce a kernel of 3x3
Define the location k and l assign to a specific range, “range (-1, 1)” of cell on y-axis
If feature k and l does not = to 0
Also if array i+k, j+l = Type
Calculate the mean difference of y-axis = “1 – mean (calculated above)”
Otherwise, if they do not = Type
Display mean difference of the y-axis as the negative version of the “mean”
Calculate the assigning numerator above = “numerator + (meandiff_x*meandiff_y)”
Calculate the assigning denominator above = “denominator + (meandiff_x*meandiff_y)”
Calculate the final sum weight was assigning above = “sum_weight + 1”
Return the calculation of the Moran Index, size of the array divides by sum weight times the
numerator divided by denominator
Moran Index Coefficient = (ar.size/sum_weight) * (numerator/denominator)
-----------