STAT 415 and STAT 515 Project 2 Fall 2019 Page 1

Instructions:

? Follow the instructions indicated in this project carefully.

? This project should be considered to be an exam. That means that you should do your own work.

? You should not speak about this exam to other students in the class.

? You may use class resources and other R related resources found in the library or internet.

? Any indications of cheating on this project will result in a failure in the course.

? The project should be turned in using blackboard. Note that you will turn in exactly three files:

bloomfunctions.txt This is a text file that contains the R functions that you create as part of the project.

Name the file exactly as specified.

project.pdf This is a PDF file that contains the code and results from the indicated simulation runs, along

with an narrative that answers the question posed in Part 2. Be sure that you gather enough data from

your simulations to justify your conclusions. Any graphics that you use should be imbedded in the PDF

file using the LATEX package graphicx. Name the file exactly as specified.

help.pdf This is a PDF file generated using LATEX that contains a help file for your function written by in in

the same style as the help files used in R. Name the file exactly as specified.

? Relax, take your time, and have fun!

STAT 415 and STAT 515 Project 2 Fall 2019 Page 2

Figure 1: Marie Zdeˇnka Baborov′a-Cih′akov′a (1877–1937) ˇ

As you relax after a difficult day of consulting in your office, and you hear the weak strains of whistling steam

from the break room. You should really replace that old water kettle, but it was given to you many years ago by

Antoine Gombaud instead of payment for your last consulting case with him. The poor man has never really gotten

the hang of games of chance, and the kettle was all he had at the time. Thankfully, after helping him out, he is

finally making progress and his fortunes have revived, though he has never breached the subject of paying for your

services with kitchen appliances. You make a note to speak with him next time you see him across the spinning red

and black wheel of fate in Monte Carlo. But your water is ready to be poured in the warmed tea-pot that awaits

with the finest Earl Grey tea leaves. As you pour and the spicy scent of the tea entangles your senses, the jarring

cadence of the phone extinguishes the last hope of arriving home on time. You have that nagging feeling that it is

going to be a long evening at the office once again. With some trepidation you reach for your phone and see that

the caller ID indicates that the call is from overseas, in fact it is from the Czech Republic.

It is Marie Zdeˇnka Baborov′a-Cih′akov′a, the famous botanist and zoologist calling you from Prague. You are ˇ

delighted as she is an old friend, and you enjoy discussing botany as you are an amateur gardener. She tells you that

ever since she started staying in her vacation home near Liberec, she has been doing intense research on the outer

boundaries of blooms. She is eventually interested in developing an application that could use machine learning to

identify flowers by focusing the camera from a phone on the bloom, and she is convinced that the outer profile of

the blooms may be significant in the identification process. She will begin gathering data, but she would like you to

develop an efficient object oriented method for storing and processing the data in R. You agree to meet with her in

Prague the next week to begin working on the project.

You had a long flight. The internet was not working and so they showed the movie The Rockin’ Magic Flute!, an

adaptation of Mozart’s famous opera starring Dwayne “The Rock” Johnson in the role of Papageno. It was the only

in flight entertainment they had, so they showed it three times in a row. But, your mind and body are refreshed as

you step into the streets of Prague the next morning. After a nice breakfast you meet Marie at her lab.

She explains to you that the outline of a bloom is measured as the radial length from the center for a sequence

of angles in the interval [0, 2π). A typical set of data is given in Table 1 with the corresponding data plotted in

Figure 2. She points out the she always measures the flowers on an equally spaced grid, but that the grid density

may depend on the general condition of the bloom.

Marie asks you to do the following tasks:

1. Using the S3 OO system in R, create a constructor function for an object with a new class named bloom. The

arguments of the function should only be r where r is a k × 1 vector that contains the radial measurements of

the flower. The sequence of θ values will always be assumed to be an equally spaced grid starting at θ = 0 and

ending at 2π(k?1)

k

, but these values should not be stored in the created object. In general the `

th point on the

grid should equal 2π(` ? 1)/k for ` = 1, . . . , k. For example, if k = 10, then the grid would be:

STAT 415 and STAT 515 Project 2 Fall 2019 Page 3

Table 1: An example of the radial data for the bloom of a flower.

Figure 2: Plotted bloom profile for the data given in Table 1

STAT 415 and STAT 515 Project 2 Fall 2019 Page 4

` 1 2 3 4 5

θ 2π(1 ? 1)/10 2π(2 ? 1)/10 2π(3 ? 1)/10 2π(4 ? 1)/10 2π(5 ? 1)/10

Value 0 π/5 2π/5 3π/5 4π/5

Decimal 0.0000 0.6283 1.2566 1.8849 2.5132

` 6 7 8 9 10

θ 2π(6 ? 1)/10 2π(7 ? 1)/10 2π(8 ? 1)/10 2π(9 ? 1)/10 2π(10 ? 1)/10

Value 5π/5 6π/5 7π/5 8π/5 9π/5

Decimal 3.1416 3.7699 4.3982 5.0265 5.6549

The returned object should be a vector corresponding to r with class equal to bloom. Load the bloom profile

data from the files bloom1.txt–bloom7.txt from Blackboard. Demonstrate that your constructor works by

creating bloom objects named bloom1–bloom7. Showing the result of running str on each object in the main

file for the project. Include the function code in your listing file. Write a help file for your function in the

standard format used for R help files. Include this in the help.pdf file.

2. Create a print method for your object that neatly prints out the radial values and the associated angles in

the format given in the sample R session given below:

print(bloom1)

Bloom Radial Profile Object (k = 20)

If there are more than 20 rows in the object then only the fist 20 should be printed followed by the statement

“x more rows” where x is the number of remaining rows not printed. For example, the bloom2 object created

above would print as

print(bloom2)

Bloom Radial Profile Object (k = 50)

Showing the result of running print on each object in the main file for the project. Include the function code

in your listing file.

3. Create a plot method for your object that neatly plots the radial values in the format given in the sample

R session given in Figure 3. The user should be given control over the plotting character, line type, and plot

type. Note that you will have to transform the data in the object, which is in polar coordinates, to Cartesian

coordinates using the transformations x = r cos(θ) and y = r sin(θ). Showing the result of running plot on

each object by including the plots in the main file for the project. Include the function code in your listing file.

4. Marie would like to be able to average two bloom profiles, but as she tells you this must be accomplished very

carefully. First she notes that if one attempts to add two bloom profiles that have a different value of k then

an error should be raised. Secondly, there is not a unique representation for the same profile since the data

points could be simply rotated around the origin. For example, the two profiles shown in Figure 4 have the

same radial values in the same order, the only difference is the one profile is rotated by an angle of π/2 about

the origin. Therefore, prior to averaging two bloom profiles, one of the profiles must be rotated to “coincide”

in some way with the other - but again there is no unique way to do this. Marie suggests that a least squares

criterion should be used to decide how to line up the two blooms, and then the radial profiles can be averaged.

Let θ1, . . . , θk be the common values of the angles between two radial bloom measurements. Let r1, . . . , rk be

the corresponding radial measurements for Bloom 1 and s1, . . . , sk be the corresponding radial measurements

for Bloom 2. Denote the `

th-order rotation of the second bloom as s?1 = s`, s?2 = s`+1, . . . , s?`+k?1 = sk, s? =s1, . . . , s? = sk?1.

To find the least squares rotation compute L(0), . . . ,L(k ? 1) and choose the value of ` that has the smallest

sum of squares. Let the corresponding radial values be denoted as ?s?1, . . . , s??k. The average bloom profile will

then be computed as ˉbm = (rm + ?s?m)/2 for m = 1, . . . , k.

As en example, consider the simple bloom profiles shown in Figure 5 with data given in the table below.

STAT 415 and STAT 515 Project 2 Fall 2019 Page 6

Figure 3: Example output of the plot method for the Bloom2 object

Bloom Radial Profile for Bloom2

STAT 415 and STAT 515 Project 2 Fall 2019 Page 7

Figure 4: A Bloom profile that has been rotated by an angle of π/2 about the origin.

Bloom Radial Profile for Bloom2

Bloom Radial Profile for Bloom2

m θm rm sm

1 0.00 3.83 2.06

2 1.26 3.19 2.75

3 2.51 2.10 3.52

4 3.77 1.86 3.15

5 5.03 2.97 2.18

Each possible rotation, along with the sum of squares is given below.

It is clear that the rotation with ` = 3 is the least squares rotation. The average bloom profile is then computed

as ˉb1 = (3.83 + 3.52)/2 = 3.68, ˉb2 = (3.19 + 3.15)/2 = 3.17, ˉb3 = (2.10 + 2.18)/2 = 2.14, ˉb4 = (1.86 + 2.06)/2 =1.96, and ˉb5 = (2.97 + 2.75)/2 = 2.86.

Wire an R function named avgProfile that has three arguments r, s, and plot that returns a list that contains

r, s, the rotated version of s, and the average bloom profile as defined above. All of the objects in the list

should be of type bloom. The function should check whether or not both objects are of type bloom with

the same value of k. If either of these conditions is violated then an appropriate error should be raised. If

plot=TRUE then a plot showing the bloom profile for r in blue, the rotated bloom profile for s in red, and the

average bloom profile in green should be plotted overlaid on the same plot. See figure 6 for an example plot.

Apply this function to all pairs of bloom profiles contained in bloom1–bloom7, even if the averaging results in

STAT 415 and STAT 515 Project 2 Fall 2019 Page 8

Figure 5: The bloom profiles that are to be averaged.

Bloom Radial Profile for Bloom2 Bloom Radial Profile for Bloom2 an error. Include the interactive session in the project.pdf and the resulting plots (for when there was not

an error). The R code for the function should be included in the bloomfunctions.txt file. Write a help file

for your function in the standard format used for R help files. Include this in the help.pdf file.

STAT 415 and STAT 515 Project 2 Fall 2019 Page 9

Figure 6: Firgure produced by the avgProfile function.

Bloom Profiles: r (blue), s (red), average (green)

版权所有：留学生作业网 2018 All Rights Reserved 联系方式：QQ:99515681 电子信箱：99515681@qq.com

免责声明：本站部分内容从网络整理而来，只供参考！如有版权问题可联系本站删除。