Why I Teach R Programming to my AP Statistics Class

AP Statistics and R Programming

Should AP Statistics students learn to program in R? Definitely yes. R is a powerful programming language and an important skill to have entering college. In my class, we use the TI-Nspire CX graphing calculator for most of our course work during the year but there are times when I feel it would be better to use R.

We usually have some time to explore data and I think learning some of the basics of R is worth the time and effort. In my school, the computer lab is just down the hall and we are also wired for WI-Fi throughout the school. I have loaded both R and RStudio on our school computers so we are ready to go from day one.

I think it’s important for students to think deeply about their data and using R with RStudio helps a great deal. Plus there is a ton of good data on the internet that becomes available to analyze using tools like R.

Using data from our textbook

We use The Practice of Statistics 3rd Edition, Starnes, Yates, and Moore during the year and I also have a copy of Stats Modeling the World, 3rd Edition (Bock, Velleman, De Veaux)

In Bock’s book, in the beginning of chapter three, they give three rules for data analysis:

  1. Make a picture
  2.  Make a picture
  3. Make a picture

The reason given for this is:

These days, technology makes drawing pictures of data easy, so there is no reason not to follow the three rules. Bock, p21

At the beginning of the semester, it’s a good time to introduce the students to the R and RStudio environment. This is where they can play with R’s base graphics, and for the more advanced student, work with ggplot2. It takes time to introduce R and some of the basics, but all of my students have a laptop that they can use at home or at school. I encourage them to download both R and RStudio on their laptop.

Once they have done that, I show them how to download different packages. We will work mostly with ggplot2 and the dplyr package as dplyr makes it easier to subset data.

To begin, I would show my students some code in vector form and how to make a simple bar graph. I will try to recreate the graphs on pages 22 and 23 from Stats, Modeling The World.

I usually put the code on the front SmartBoard for the students to copy. I encourage them to read and to look at code examples on the internet and to bring in any reference material they find in addition to the material I hand out.

Learning to code in R for a bar plot

First I make sure they are in the correct working directory, or at least know what directory they are saving their work to. I then try to recreate some of the bar plots in the text so the students can get interested right away. Also, my students feel that they are doing “real” statistics now by actively creating plots instead of just looking at them in the book.

I also tell them at this point they have to take more ownership of their code when they are stuck. I can help with some of the error messages, but they need to Google any error code to try and figure out the problem. I found this to be a very rewarding part of my teaching by not knowing all the answers and figuring out the solution with the “team”.

So here would be a first attempt at plotting a bar graph.

And this is the plot.

 

bar plot1The teaching part here is to show that this plot is not complete and we need to put more information for the x and y labels;  xlab and ylab.

Here is the new plot.

bar plot2

 

 

Another example from the book gives the count for each class.

The plot looks like this.

 

 

Bar plot 3

Extension: Working with ggplot2

Some of my students will be able to grasp the base graphics in a short time but may need additional help to continue. I usually give them a PDF document for the base graphics and also one for R commands. Other students will run with this and ask for additional information on how to graph. This is when I introduce ggplot2 for those students that are more self directed and motivated. Last year, all my students wanted to learn more about using ggplor2 when they saw the results of a few other students’ plots using ggpolt2.

 

I will usually spend the last ten minutes in class giving the basics of ggplot2 and how to make a simple plot. I try to avoid using the qplot2 because by the end of the school year, my students will have enough time to internalize the basics of the grammar of graphics. Some of them also used their knowledge of ggplot2 in their AP Economics class when exploring data.

Some would explore with the color =  and fill = arguments to come up with different plots.

Here is one beginning plot.

 

ggplot2 bar plot

With a little different code, we get something better.

Which gives this nice plot.

 

ggplot2 bar plot colorAt this point, it take a while for all this information to sink in and many of my students will tell me they will play with the graphs at home to try to impress the class. We will also have experience using our TI Nspire CX to make bar plots as there are clear instructions in the back of the Starnes book.

Other lessons using R in the beginning of the year include converting vector data to a data.frame and using the dplyr package. I’ll share more about these topics in future posts.

 

 

How To Print x Label Vertical In Ggplot2

I was working with some boxplots last month and I needed to plot twelve months of air quality data. The problem was that the twelve months over lapped each other and the plot didn’t look good. If I could only draw the x labels vertical.

For this example, I’ll show you how to plot the x labels vertical. It’s just a matter of using the theme() function.

Here is the R code using ggplot to plot the Iris data of Species and Sepal.Width

Here is the graph of this plot.

X horizontal

To make the x label vertical, add the theme() function:

The new plot will look like this:

X 60 degrees

You can change it to 90 degrees by adjusting the angle.

 

The new 90 degrees plot now looks like this:

X 90 degrees

I am working on more plots using air quality data that plots month verses AQI2.5 and I’ll have them for my next post.

Working with Rmd files using knitr and WordPress

 

Author Kevin

Here is a pairs plot.

Here is a regression model with the output below


 

What Story Telling Can Teach Us About The Learning Process

16 Scientists and Mathematicians
16 Scientists and Mathematicians that my students learned about.

In my Physical Science class this year I taught 16 scientists and mathematicians to my students. Most of these scientists are well known and a few less so .

To engage the student, I try to tell a story about each scientist because as research shows, students can remember a story much better than just a list of facts alone.

For each scientist, I also have them remember the year they were born and one or two key facts about an important aspect of their lives as a scientists.

Now for some of these scientists I am generalizing as some are mathematicians, but there is usually a scientific link from their work to sciences.

One good example is G.H. Hardy. He proudly exclaimed in his book that Continue Reading

The Electroscope Demo

In class this week we built a simple electroscope to demonstrate static electricity. The students built it with things we had in the classroom. In the video, you can see the two pieces of aluminum foil move apart as the static charge is applied.

Here is a nice video showing how to make an electroscope.

 

Get Your Numbers Ready For Pi Day

 

Are you ready for Pi day? Only one week to go. I’ve been doing the Major System for a few years now and it’s time for a little mental math and for me to demonstrate what this system can do.

I got pi to 100 digits as of today. My goal for pi day is 200 digits.

In. Front. Of. The. Class. On the Smart board.

Anybody want to ride the number wave with me?

 

3/7/2016 Update: I have a challenger! One of my students asked me for the 200 digits list. We’re ready to rumble.

3/14/2016: I successfully wrote 200 digits of pi. My challenger did over 100.

Chengdu Food You Just Have To Try

Chengdu1I was on my way to the grocery store yesterday and I stopped for lunch at my favorite place to eat. It’s a small shop that serves the hot sauce wonton’s and the round veggie wraps.

While I was waiting, I realized I never took any video or pictures of this place. So today, I share with you some good food if you happen to be in Chengdu. This is right across for the Carefour store in the Tonzilin section of town.

The video below shows how they make the food. It’s fun to just watch as they can make them pretty fast.

 

 

 

Shopping in Chengdu China

Here is a short video of the Raffles Mall where we do some of our food shopping. We take the 78 bus both ways. The bus costs the equivalent of about 30 cents.

As you can see the city is very modern looking. This mall is about twenty minutes from our apartment.

 

The Bullet Train to Chongqing China

I recently took my family to the Philippine Consulate in Chongqing China from Chengdu. It’s more than 300 kilometers distance from where we live in Chengdu. The train trip was about two hours and it made just a few stops. We has some business to complete at the Consulate so we decided to stay for the night to see the city.

The food here in Sichuan providence is not the typical Chinese food you would expect if you live in the US. The food here is spicy and hot and they eat lots of noodles and pork. They had the spiciest noodles at the 7-11 right around the corner of the consulate.

On the return trip, we got the nonstop and we were back in an hour and 45 minutes.

Below is a short video of our train ride to to Chongqing.

 

6 Educational Quotes that I Live By

Every teacher needs some inspiration from time to time. I have come across these quotes over the years and have had time to appreciate the deep meaning in all of them. Anyone who has taught for a while will see the value in these quotes. Here I offer these quotes and a bit of my teaching philosophy. 

vessel“The mind is not a vessel to be filled but a fire to be kindled.” Plutarch

Background knowledge is important but to reach the higher level thinking skills one needs to tap into the student’s inner motivation. I try to help them find their path and then watch as they construct their own future. I always try to show the beauty and excitement of how science and mathematics can enhance their lives. Make connections to students and be passionate about your subject and the fire will be lit.

Continue Reading