Author Archives: jeredh

Module # 1 – What is statistics?

This is my first post for Advanced Statistics and Analytics for Fall of 2018 at USF. This is the same blog I used for Intro to R Programming and Web Design Technologies, but posts for this class will have their own tag to keep them separate.

I still have R Studio and R installed from those courses this past Spring, and I used them in Intro to Data Science over the summer, so I have no problem running them now. I rather enjoyed R programming, so it will be nice to work with it again.

Posted in Advanced Statistics and Analytics | Comments Off

Module 9 – Connection to MySQL and PHP for Web Programming Group

The assignment for web programming for this module was to make two connections from PHP to MySQL.

The web page I created for the assignment is here, containing links to pages for the first and second connections:

http://jered.0fees.us/module9/index.php

Connection 1

For the first connection, just pasting the code into a page and loading it naturally failed. After that I fixed a bad quote and added the closing “?>”, to get:

<?php
$db = mysql_connect("sql211.0fees.us","username","password");
if (!$db) {
	die("Database connection failed miserably: " . mysql_error());
}
?>

I replaced the MySQL server, username, and password in the code with the login information for my 0fees account.

After that, the page was blank when it loaded (because it didn’t throw an error). I added a sentence in HTML to the page just so I’d know for certain that the page had loaded.

That was pretty straightforward. I just needed to get my MySQL login information from the 0fees cPanel.

Connection 2

I had to tweak the first quotation mark in this code too, as well as set information like the login information and database info. I tried loading the page before creating the database and saw the expected error, with a notice about a failure to connect to the database. Then I made the database and no table, and got the third error in the PHP code, also as expected. Then I created the table and added some data, and the PHP code ran with no error messages (or output, so I added a line of text to the second page too). The code I used was (with my username and password removed):

<?php

//Step1

$db = mysql_connect("sql211.0fees.us","username","password");
if (!$db) {
	die("Database connection failed miserably: " . mysql_error());
}

//Step2

$db_select = mysql_select_db("username_module9",$db);
if (!$db_select) {
	die("Database selection also failed miserably: " . mysql_error());
}
?>
<html>
	<head>
		<title>Step 3</title>
	</head>
	<body>
		<?php

		//Step3

		$result = mysql_query("SELECT * FROM testdata", $db);
		if (!$result) {
		 die("Database query failed: " . mysql_error());
		}
		?>
		<p>If there were any errors, they will appear above this sentence.</p>
 </body>
</html>

The code ran fine, and I didn’t have any problems connecting to the database or creating data through PHPMyAdmin. The instructions in the slides for working with cPanel and PHPMyAdmin worked well.

Posted in Web Design Technologies | Comments Off

Module 9 – Web Design for Design Group

The assignment this week was to develop a blueprint for the website that my partner and I will create for the semester project. The assignment title suggested that the member of the team who will be primarily responsible for web design should create the blueprint. I will be primarily responsible for the web programming side.

The blueprint from my partner, Christian Williams, is reproduced below.

1. The websites objective is to sell vegetables and include nutritional information.

2. The audience is the consumers who would like to purchase vegetables.

3. The content of the site is a shopping area that displays vegetables and nutritional information. As well as information about the local farm.

4. The site will provide form functionality and a shopping cart.

5. Design elements are not finalized as of yet but as far as navigation the site will be very minimalistic and clear. It will offer a very clean user interface to buy vegetables. Images will be of fresh vegetables that accurately portray them. colors will be soft and rustic to match the theme of the farm.

Posted in Web Design Technologies | Comments Off

Module 11 – Debugging and defensive programming in R

Our assignment this week was to debug a block of code.

The original code was:

tukey_multiple <- function(x) {
   outliers <- array(TRUE,dim=dim(x))
   for (j in 1:ncol(x))
    {
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
    }
outlier.vec <- vector(length=nrow(x))
    for (i in 1:nrow(x))
    { outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) }

To start debugging, I pasted the code into RStudio to see what kind of error it would throw. I got:

Error: unexpected symbol in:
"    for (i in 1:nrow(x))
    { outlier.vec[i] <- all(outliers[i,]) } return"

The “unexpected symbol” error made me think that some brackets or parentheses weren’t closed or were misplaced. That wasn’t it, but it turned out to be something similar – looking at the last line, I noticed that the “return” looked out of place. It should have been on a separate line, by itself. That would certainly make it an unexpected symbol. I moved return to the next line, making the code look like:

tukey_multiple <- function(x) {
  outliers <- array(TRUE,dim=dim(x))
  for (j in 1:ncol(x))
  {
    outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])
  }
  outlier.vec <- vector(length=nrow(x))
  for (i in 1:nrow(x))
  { outlier.vec[i] <- all(outliers[i,]) }
  return(outlier.vec) }

At that point, the code ran successfully and the function was created.

Using the function was a different story – I passed it a built-in list as an argument, and got a new error:

Error in tukey.outlier(x[, j]) : could not find function "tukey.outlier"

I’m guessing that tukey.outlier() is a function that returns TRUE or FALSE depending on whether the value is an outlier. The code runs fine without that function (albeit it makes tukey.multiple() always return a bunch of TRUEs), so I assume this code will work provided tukey.outlier() is defined.

Posted in R Programming | Comments Off

Module 10 – Building an R package

This week’s assignment was to create a DESCRIPTION file for our final project package.

The DESCRIPTION file I created is on GitHub here:

https://github.com/jered0/lis4930-rpackage/blob/master/queryNIBRS/DESCRIPTION

Getting the DESCRIPTION format right was interesting, and I ended up looking at a few packages on CRAN to see what package names typically look like and how they describe themselves. For example, I thought at first that the Title field was just an expanded name for the package, but I see that it does, as you said in the module, act more as a descriptive subtitle.

Because my package will involve retrieving data from the FBI’s Crime Data API, I added an Imports section for the dependency the package will need, RCurl. I’ll look into whether that’s strictly required – I might be able to use R’s built-in web queries instead of relying on RCurl’s fancy capabilities.

Posted in R Programming | Comments Off

Module 8 – PHP Strings and Web Design documentation

This week we covered PHP strings and documenting web sites.

Web Programming Section

The assignment was to create a string with PHP code. My web page is located here:

http://jered.0fees.us/module8/index.php

I created some strings using both single quotes and double quotes, then output them as one sentence using concatenation. In the process of concatenating the strings I used str_replace() to replace a word, for the sake of including a string function.

The relevant code is:

$one_string = "<p>This is a waffle that's been";
$two_string = 'concatenated with PHP.</p>';
$output_string = str_replace("waffle", "sentence", $one_string)." $two_string";
echo $output_string;

Strings are values that can be manipulated with the instructions in functions. There are a number of functions associated with strings – that makes sense, given that strings comprise most of the information that will be communicated to a user from a web page or program.

Web Design Section

For this assignment we are to document the web site we’ll be creating for our final project.

Step 1. Define key stakeholders’ goals

The stakeholder is the farmer the site is created for. He wants to display his wares (vegetables), provide information about them to site visitors, and give those visitors the opportunity to purchase his vegetables.

Step 2. Identify your users’ goals and expectations

The users are site visitors who are interested in purchasing vegetables online. They’ll expect a site that lets them readily find information about what vegetables are for sale and purchase them easily.

Step 3. Define your site’s content areas

Pending a discussion with my eventual partner, I expect the site would be divided into a landing page, a main page to list the vegetables, pages for each vegetable on offer (turnips, radishes, and spinach), a page to check order status, and a page with information about the farmer.

Step 4. Organize the content areas

The landing page would be the first site in the hierarchy, and would link directly to the vegetable page and to the farmer information page. The vegetable page would in turn link to individual pages for each vegetable.

Posted in Web Design Technologies | Comments Off

This week the assignment was to plot graphs for a data set using a built-in R function, a function from the lattice package, and a function from the ggplot2 package.

The source code I created for the task can be found on GitHub here:

https://github.com/jered0/lis4930-rpackage/blob/master/module9/lattice-ggplotting.r

I used the SeaSlugs data set. A description of the set is on this page, and the data set itself can be downloaded via this link.

Given that there were two categories of data (time and metamorphose percentage), and there were multiple percentages for each time entry, I decided to represent the data with a boxplot.

Built-in function

I used the built-in boxplot() function to create the first graph.

Graph created with the built-in boxplot() function in R
Built-in boxplot() function

I’ve used this function before, but I did have to fiddle with it to work out how to get the y-axis labels horizontal (I thought it looked better that way), and to apply colors from the rainbow() function. It’s a simple graph, nothing fancy.

Lattice function

I used the bwplot() function from the lattice package to make the next box plot graph.

Graph created with the bwplot() function from the lattice package
The lattice package's bwplot() function

The syntax is pretty close to the built-in function, which made it easier to work with than I expected. I had to tweak settings to get it to use vertical boxes, and had to find the parameter that would set the colors. Unfortunately I didn’t find an option to draw a line instead of use a plot to mark the median, to match the appearance of the other graphs.

ggplot2 function

For the final graph, I used the ggplot2 package’s ggplot(), geom_boxplot(), labs(), and theme_classic() functions in concert.

Graph created with the ggplot2 package
The ggplot2 package's geom_boxplot() function

It took some time to get the syntax and everything sorted out, since it uses a different syntax from the other two functions. I also had to convert the Time column to factors – without that change, all the percentage data and time data were collected into one giant boxplot. Definitely not what I wanted.

The ggplot2 graph came out looking the best, I think, and next time there would be less fuss given that I’m more familiar with the package.

Posted in R Programming | Comments Off

Module 7 – PHP Functions and Rollover Images

This week we covered PHP functions and rollover images. My web page for the assignment is here:

http://jered.0fees.us/module7/index.php

Web programming section

I used a simple function and if/else block for the web code. The syntax was fairly similar to other languages.

One thing that struck me right off the bat was that an error in PHP code could cause the page to fail to load – omitting a semicolon yielded a 500 server error. JavaScript is more forgiving, just skipping that code instead of blocking the whole page.

Another difference is the way variable scope is handled. As I recall, JavaScript will use a variable from outside a function simply by referencing it, while in PHP you have to explicitly import a global variable into a function.

The if statement seems to work similarly in both languages. Function definitions are also similar between the two, with both languages being loosely typed and not defining return types.

Web design section

For the web design portion, we needed to add an image to a page that would change when someone hovered the cursor over it. I went looking for a way to do it with CSS, using :hover, and that started looking a bit complicated. Then I came across some simple JavaScript to do the same thing (using onmouseover and onmouseout to change the image displayed via this.src). Then I kicked myself for not thinking to check JavaScript first, and went with that approach. The HTML is much easier to read with the JavaScript approach.

Posted in Web Design Technologies | Comments Off

Module 8 – Input/Output, string manipulation and plyr package

This module’s assignment was to read data from a file, alter or filter that data, then write the new data to new files.

The code I used is on my GitHub repository here:

https://github.com/jered0/lis4930-rpackage/blob/master/module8/studentsorting.r

I was generally familiar with the concepts involved in reading from files thanks to other coding languages, and the format of read.table is pretty intuitive, so I had no problems there. The file.chooser() function was easy to use, and being able to read the data in all at once without looping through lines one at a time was nice.

It did take me a bit to wrap my head around the ddply function, but running it a few times and tweaking the parameters cleared it up. Being able to fiddle with a data set and pick it apart like that definitely seems like it would come in handy.

I’ve used the grep command on Unix often, so using grepl() was straightforward – though I did have to look over the documentation to see how grep() and grepl() differ, and why grepl needed to be used with subset().

Posted in R Programming | Comments Off

Module 6 – Introduction to PHP and CSS

Web Programming Section

This week the first assignment was to create a PHP page and upload it to my web space. The URL of the page is:

http://jered.0fees.us/module6/index.php

Next we were presented with a list of questions and directed to choose the correct answers. My questions and answers are:

Question # 1
A. PHP server scripts are surrounded by delimiters, which one?
A3. <?php…?>

Question # 2
B. How do you write “Hello World” in PHP
B1. echo “Hello World”;

Question # 3
C. All variables in PHP start with which symbol?
C2. $

Question # 4
D. What is the correct way to end a PHP statement?
D1. ;

Web Design Section

This week we were asked to create a CSS style sheet. I created mine at the following URL:

http://jered.0fees.us/module6/module6style.css

Cascading style sheets are useful in a number of ways. For one, it separates style from content – the main section of a web page can consist primarily of content to be presented to the user, making it easier to read in HTML form. Using CSS also provides stylistic consistency, as several pages can import style information from a single CSS file. On top of that, website design changes become easier to make because you can make a style change in a single file, then every HTML page on the site that uses that stylesheet will automatically use the new style. Pages styled with CSS will usually load faster than pages formatted with frames and tables. And because CSS can be easily overridden by a web browser, using CSS aid accessibility by allowing users to use larger font sizes or more dyslexia-friendly fonts.

The following are my questions and answers for the web design part of the week’s assignment:

Question # 1
A. What is the correct CSS syntax for making all the elements bold?
A2. span {font-weight:bold}

Question # 2
B. What property is used to change the text color of an element?
B3. color:

Question # 3
C. The # symbol specifies that the selector is?
C4. id

Posted in Web Design Technologies | Comments Off