0% found this document useful (0 votes)
1 views3 pages

Step Model Word Instructions

Uploaded by

Jomasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Step Model Word Instructions

Uploaded by

Jomasa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#function to plot each point and draw the direct line

plotpath <- function(x,y,n){


plot(x,y, type="o")
segments(x[1],y[1],x[n],y[n], col="red")
}

#master function
primatestepmodel <- function(distance, angledat, runs=200, initthresh=100,
totdthresinc=5, totdthresh=500, threshper=10, plot=FALSE){
# initialize the total run counter
l = 1
# intialize the shortest distance between starting and ending points
vector
directd = 0
# intialize the difference between the traveled and direct distance
vector
diffd = 0
# intialize the total distance traveled vector
totd = 0
# initialize the ratio of direct distance by distance traveled vector
ratio = 0
#intialize the threshold vector
threshold = initthresh

#loop for each total distance threshold


while(threshold[l] <= totdthresh){
#intialize the number of runs for each threshold

thresruns = 0

#loop for the number of runs (per each total distance


threshold)
while(thresruns <= runs){

# counter for ordered (x,y) points


n = 1
# intialize x values vector
x = 0
# intialize y values vector
y = 0
# intialize the runs total distance
totd[l] = 0
#intialize the deflection angles vector
angle = 0
#intialize the transformed angles vector
pangle = 0

#loop for the total distance to come within specified


threshold
while(totd[l] < ((1-(threshper/100))*threshold[l])){
#pick a random distance
i = sample(1:length(distance), 1)

#update the total distance counter


totd[l] = totd[l] + distance[i]

#reselects distance if it exceeds the threshold


by 10%
while(totd[l] >
((1+(threshper/100))*threshold[l])) {
totd[l] = totd[l] - distance[i]
i = sample(1:length(distance), 1)
totd[l] = totd[l] + distance[i]
}

#pick a random angle


k = sample(1:length(angledat), 1)

#keep a record of the selected angle


angle[n] = angledat[k]

# transform the deflected angles


if(n == 1){
pangle[n] = angle[n]
}
else{
pangle[n] = -angle[n] + pangle[n-1]
}

#allow for another point to be added to (x,y)


n = n + 1

#set the new (x,y) point based on the previous


point and the distance/angle
x[n] = x[n-1] + distance[i]*cos((pangle[n-1])*(pi/180))
y[n] = y[n-1] + distance[i]*sin((pangle[n-
1])*(pi/180))

# calculates the direct distance between the starting


point and ending point
directd[l] = sqrt(abs(x[1]-x[n])^2+abs(y[1]-y[n])^2)

# calculates the difference in the traveled and direct


distance
diffd[l] = totd[l] - directd[l]

# calculates the ratio of the direct distance over the


total distance
ratio[l] = directd[l]/totd[l]

#updates the next threshold


threshold[l+1] = threshold[l]

# plot each point and draw the direct line


if(plot==TRUE){
plotpath(x,y,n)
}
# add a run to the total
l = l + 1
thresruns = thresruns + 1
}

#increment the threshold


threshold[l] = threshold[l-1] + totdthresinc
}
#remove the trivial threshold entry
threshold = threshold[-l]

#return the complete dataframe


return(data.frame(threshold, totd, directd, diffd, ratio))

#open distance vector


distance <- as.numeric(as.matrix(read.csv("distance.csv", header=FALSE)))
Between the “” must exactly match the name of your distance file You can
change this once you have opened the script in R

#open angle vector


angledat <- as.numeric(as.matrix(read.csv("angledat.csv", header=FALSE)))
Between the “” must exactly match the name of your angle file

#save the primate step model into a data frame called "newdata"
newdata <- primatestepmodel(distance, angledat, runs=200, initthresh =100,
totdthresinc = 5, totdthresh=500, threshper = 10, plot=FALSE)
You can change the number of runs and other parameters above. Initthresh is
the smallest “target” distance, totdthresinc is the size of increase in target
distance between runs,Totdthresh is the largest target distance, thresshper is
the % within which the sum of steps is considered within target range

#save the data frame as a file named primatestepmodel


write.csv(newdata,file="primatestepmodel.csv")
Between “”is the name of the file the data will be saved in.

You might also like