Docsity

Statistics Computing - Final Project Report | STAT 534, Study Guides, Projects, Research of Statistics

Toggle sidebar
bg1
Hil Lyons
STAT 534
Final Project
1) R Code for compute.mst.and.diameter and fit.and.plot.pc
############################################################
#compute.mst.and.diameter
#
#This function calls a C function to do the MST and diameter
#calculations, wrapper_mst_and_diameter.
#As such, compute.mst.and.diameter does very little except
#clean up the output of the C function.
#
############################################################
compute.mst.and.diameter <- function(obs){
X1 <- as.vector(obs[,1])
X2 <- as.vector(obs[,2])
nobs <- length(X1)
MST.edge.vtx1 <- rep(0, nobs - 1)
MST.edge.vtx2 <- rep(0, nobs - 1)
diameter.indeces <- rep(0, nobs)
res <- .C("wrapper_mst_and_diameter", as.double(X1), as.double(X2),
as.integer(nobs), as.integer(MST.edge.vtx1), as.integer(MST.edge.vtx2),
as.integer(diameter.indeces))
MST.edges = cbind(res[[4]], res[[5]])
diameter = as.vector(res[[6]][res[[6]] > 0])
return(list(MST.edges = MST.edges, diameter = diameter))
}
############################################################
#diameter.distance is a simple function calculating the
#Euclidean length of the diameter. It is called from
#fit.and.plot.pc
#
############################################################
diameter.distance <- function(obs, diameter){
numpts <- length(diameter)
diam1 <- diameter[1:(numpts-1)]
diam2 <- diameter[2:numpts]
distance <- sum(sqrt((obs[diam2,1]-obs[diam1,1])^2 +
(obs[diam2,2]-obs[diam1,2])^2))
return(distance)
}
############################################################
#fit.and.plot.pc
#
#This function calls multiple functions that have been
#provided and developed over the course of the quarter.
#
#compute.mst.and.diameter (see above)
#position.inital.vertices
#fit.polyline (from HW1)
#
Hil Lyons Page 1 6/01/2006
pf3
pf4
pf5
pf8

Partial preview of the text

Download Statistics Computing - Final Project Report | STAT 534 and more Study Guides, Projects, Research Statistics in PDF only on Docsity!

Hil Lyons

STAT 534

Final Project

1) R Code for compute.mst.and.diameter and fit.and.plot.pc

#compute.mst.and.diameter

#This function calls a C function to do the MST and diameter

#calculations, wrapper_mst_and_diameter.

#As such, compute.mst.and.diameter does very little except

#clean up the output of the C function.

compute.mst.and.diameter <- function(obs){

X1 <- as.vector(obs[,1])

X2 <- as.vector(obs[,2])

nobs <- length(X1)

MST.edge.vtx1 <- rep(0, nobs - 1)

MST.edge.vtx2 <- rep(0, nobs - 1)

diameter.indeces <- rep(0, nobs)

res <- .C("wrapper_mst_and_diameter", as.double(X1), as.double(X2),

as.integer(nobs), as.integer(MST.edge.vtx1), as.integer(MST.edge.vtx2),

as.integer(diameter.indeces))

MST.edges = cbind(res[[4]], res[[5]])

diameter = as.vector(res[[6]][res[[6]] > 0])

return(list(MST.edges = MST.edges, diameter = diameter))

#diameter.distance is a simple function calculating the

#Euclidean length of the diameter. It is called from

#fit.and.plot.pc

diameter.distance <- function(obs, diameter){

numpts <- length(diameter)

diam1 <- diameter[1:(numpts-1)]

diam2 <- diameter[2:numpts]

distance <- sum(sqrt((obs[diam2,1]-obs[diam1,1])^2 +

(obs[diam2,2]-obs[diam1,2])^2))

return(distance)

#fit.and.plot.pc

#This function calls multiple functions that have been

#provided and developed over the course of the quarter.

#compute.mst.and.diameter (see above)

#position.inital.vertices

#fit.polyline (from HW1)

#The result are plots with some summary statistics.

fit.and.plot.pc <- function(obs, true.curve, nvert, tension, nit){

mst.and.diameter <- compute.mst.and.diameter(obs)

mst <- mst.and.diameter$MST.edges

diam <- mst.and.diameter$diameter

dist <- diameter.distance(obs, diam)

initial.vertices <- position.initial.vertices(obs, diam, nvert)

fitted.results <- fit.polyline(obs, initial.vertices, tension, nit)

fitted.vertices <- fitted.results$new.vertices

total.energy <- fitted.results$energy.dist[nit+1] +

fitted.results$energy.spring[nit+1]

windows()

par(mfrow = c(1, 3))

#Plot 1 - obs, MST, diameter

plot(obs, xlab = "x1", ylab = "x2",

main = paste("Obs, MST, and Diameter\nDiameter length = ", round(dist, 5)),

cex = .7, cex.axis = .8, cex.lab = .8, cex.main = .9)

for (i in 1:length(mst[,1])){

lines(obs[mst[i,],])

lines(obs[diam,], lwd = 2, col = "red")

#Plot 2 - obs and initial polyline

plot(obs, xlab = "x1", ylab = "x2",

main = "Obs and initial polyline",

cex = .7, cex.axis = .8, cex.lab = .8, cex.main = .9)

lines(initial.vertices, lwd = 2, col = "blue")

#Plot 3 - obs, true curve, and fitted polyline

plot(obs, xlab = "x1", ylab = "x2",

main = paste("Obs, true curve, and fitted polyline\n",

paste("nvert = ",nvert), paste(", tension = ", round(tension, 1)),

paste(", nit = ", nit), paste(",\ntotal energy = ", round(total.energy, 5))),

cex = .7, cex.axis = .8, cex.lab = .8, cex.main = .9)

plot.curve(true.curve)

lines(fitted.vertices, lwd = 2, col = "blue")

2) Required Plots โ€“ this will be printed on 2 pages (1 page each) in order to maximize the

view.

a. nit = 10, nvert = 10, tension = 0

b. nit = 10, nvert = 20, tension = 2

Obs, true curve, and fitted polyline

 - 0.2 0.4 0.6 0.8 1. 
      • Diameter length = 2.
        • x
  • x - 0.2 0.4 0.6 0.8 1. - 0. - 0. - 0. - 0. - x Obs and initial polyline - x - 0.2 0.4 0.6 0.8 1. - 0. - 0. - 0. - 0. - total energy = 0. nvert = 10 , tension = 0 , nit = 10 , - x - x - 0.2 0.4 0.6 0.8 1.
        • Diameter length = 2. Obs, MST, and Diameter
          • x
  • x - 0.2 0.4 0.6 0.8 1. - 0. - 0. - 0. - 0. - x Obs and initial polyline - x - 0.2 0.4 0.6 0.8 1. - 0. - 0. - 0. - 0. - total energy = 0. nvert = 20 , tension = 2 , nit = 10 , - x - x

3) C code: wrapper_mst_and_diameter()

#include<stdio.h>

#include<assert.h>

#include<stdlib.h>

#include "global-definitions.h"

#include "set.h"

#include "graph.h"

#include "mst.h"

#include "diameter.h"

void wrapper_mst_and_diameter(double *X1, double *X2, int *nobs,

int *MST_edge_pt1_index, int *MST_edge_pt2_index, int *diameter_indeces){

Point *p, *current_pt;

Graph *the_mst;

int i = 0, j = 0;

Set_iterator *edge_iterator;

Set *point_set = set_make(point_eq);

Edge *current_edge;

struct Path *the_diameter;

Link current_node, past_node;

for (i = 0; i < (*nobs); i++){

p = point_make(X1[i],X2[i],i+1);

set_add_element((void*) p, point_set);

the_mst = mst(point_set);

i = 0;

edge_iterator = graph_edge_iterator_make(the_mst);

while (set_iterator_next(&current_edge, edge_iterator)){

MST_edge_pt1_index[i] = current_edge->vertex1->id;

MST_edge_pt2_index[i] = current_edge->vertex2->id;

i++;

the_diameter = find_diameter(the_mst);

i = 0;

current_node = the_diameter->vertex_list;

current_pt = current_node->item;

diameter_indeces[i] = current_pt->id;

while (current_node->next != NULL){

i++;

past_node = current_node;

current_node = current_node->next;

current_pt = current_node->item;

diameter_indeces[i] = current_pt->id;

b. Diameter output (four columns)

Index 198 91 147 49 11 95 76 36 92 27 2 100 183 157 26 33 7 162 23 175 112 51 101 122 47