Color-coding groups for plots: string.to.colors function in fifer()

Posted in Statistics

I used to hate color-coding plots. 'Twas a big pain. Let's say we're trying to plot the relationship between awesomeness and attractiveness in R versus. First, let's read in the R know-how and awesomeness dataset.

Let's peak under the “head”, shall we?

require(fifer) d = read.csv("Awesomeness_Rknowhow.csv") head(d) ## Number.of.Friends R.Know.how Club ## 1 0.46841 -0.7202 Mat Black Labs ## 2 0.01932 0.3678 Mat Black Labs ## 3 0.68488 -0.9006 Mat Black Labs ## 4 -1.15628 1.8706 Mat Black Labs ## 5 -0.76576 -0.1406 Mat Black Labs ## 6 0.15211 -1.4046 Mat Black Labs

Nice! And let's peak under the “tail.” (Okay, bad joke).

tail(d) ## Number.of.Friends R.Know.how Club ## 95 -0.6766 -1.4407 Rs-R-Us ## 96 -0.3809 -1.0437 Rs-R-Us ## 97 1.5243 2.4358 Rs-R-Us ## 98 0.3377 -0.3047 Rs-R-Us ## 99 -1.3506 -1.0900 Rs-R-Us ## 100 -1.4359 -1.6769 Rs-R-Us

Let's say we're at an uber geek convention where SAS, R, and Matlab users alike meet to…er…mingle and speak of common interests. Being the research-minded student you are, you decide to measure three traits of the convention participants: how many friends they have (okay, I know you can't have negative friends and you can't have a fraction of a friend. Stop being so critical!), how much they know about R, and which club they belong to–the Mat Black Labs or the R-R-Us(es). You then plot the relationship betwixt the two quantitative traits:

plot(d[,1:2], ylab="Number of Friends", xlab="R Know-How", xaxt="n", yaxt="n")

What a jumbled mess! Then you remember that you forgot you measured the two groups…but how to plot them. Why, let's color-code them!

This is where the string.to.color function comes in. It requires a vector of strings as inputs (and an optional vector of colors–one for each unique grouping value) and it will output a string of colors (the same length as the original string). Let's take a look:

#### let's look at that vector of strings (or factors) d\$Club ## [1] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [5] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [9] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [13] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [17] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [21] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [25] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [29] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [33] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [37] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [41] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [45] Mat Black Labs Mat Black Labs Mat Black Labs Mat Black Labs ## [49] Mat Black Labs Mat Black Labs Rs-R-Us Rs-R-Us ## [53] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [57] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [61] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [65] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [69] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [73] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [77] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [81] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [85] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [89] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [93] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## [97] Rs-R-Us Rs-R-Us Rs-R-Us Rs-R-Us ## Levels: Mat Black Labs Rs-R-Us

And now let's see what string.to.colors does

string.to.colors(d\$Club, col=c("red", "blue")) ## colors colors colors colors colors colors colors colors colors colors ## "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" ## colors colors colors colors colors colors colors colors colors colors ## "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" ## colors colors colors colors colors colors colors colors colors colors ## "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" ## colors colors colors colors colors colors colors colors colors colors ## "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" ## colors colors colors colors colors colors colors colors colors colors ## "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" ## colors colors colors colors colors colors colors colors colors colors ## "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" ## colors colors colors colors colors colors colors colors colors colors ## "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" ## colors colors colors colors colors colors colors colors colors colors ## "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" ## colors colors colors colors colors colors colors colors colors colors ## "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" ## colors colors colors colors colors colors colors colors colors colors ## "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue" "blue"

So all it does is replace all the values of “Rs-R-Us” with “blue” and all the values of “Mat Black Labs” with “red.”

Now we can put that into the plot to tell R how we wanna display it:

plot(d[,1:2], ylab="Number of Friends", xlab="R Know-How", xaxt="n", yaxt="n", col = string.to.colors(d\$Club, col=c("orange", "purple"))) legend("topleft", legend=c("Mat Black Labs", "Rs-R-Us"), text.col=c("orange", "purple"), bty="n")

We can also “cheat” and use the string.to.colors function to use different symbols!

plot(d[,1:2], ylab="Number of Friends", xlab="R Know-How", xaxt="n", yaxt="n", pch = as.numeric(string.to.colors(d\$Club, col=c(11, 16)))) legend("topleft", legend=c("Mat Black Labs", "Rs-R-Us"), pch=c(11, 16), bty="n")

Neato, eh?

\$1 DIY Marble Dispenser (kinda)

Posted in DIY, Misc

As an undergraduate psych student, I remember sitting in Dr. Paul Robinson’s class. The man had a full head of dark black wavy hair with streaks of gray and gesticulated wildly with his hands when he spoke, pushing his thick-rimmed glasses higher on his nose while concentrating on the floor before him. I loved the guy. In his career, had written about seventy thousand parenting books (okay…that’s an exaggeration), and had fostered over 350 children! (That’s not an exaggeration…like, seriously. He really did that.). And that jolly old man was quite a story-teller and had an anecdote for every principal in behaviorism. (He even taught a fish to read once…seriously).

The man insisted that raising obedient children was easy–all that was required was a little know-how about how to manipulate rewards and punishments–reward the good, punish the bad, simple as that!

Ten years later…ha! I couldn’t get my kids to eat ice cream if I commanded it! One day, after a seventy-seven hour whine-athon, I decided I’d had it. A spark of memory came from the time I’d listened to “Doc” (as we called him)–a clever way to implement rewards and punishments–a token economy.

So here was my idea–we’d have marbles that the kids could cash in for TV, games, desert, costume time, whatever. They’d earn marbles for chores (and other good behavior), but they’d “lose their marbles” (ha!) if they whined about it.

The only problem…how to dispense the marbles? I wanted a system that was easy and would require little maintenance on my part (I’m kinda lazy, ya know). So here’s what I built.

I started with my first ever box joint. Aren’t your proud of me?! 100% solid walnut with a tung oil finish that’d make yer grandfather proud.

I made four slots (future expandability, you see). The idea is that when the kids earn a marble, they push a spring-loaded button in the front and out comes the marble.

The problem–they get stuck!

If you look at the pic below, you see the problem. Like my two boys, this pair of marbles is fighting! They’s trying to get inta the same slot at the same time! So, I thought I might try building a funnel. (And since I don’t have a lathe, that required a bit of voodoo with the router table).

Nope! Same problem–stuck marble. I even posted a question on diy.stackexchange.com.

After some experimentation, I thought that a smaller “marble” would fix it. So I bought a bunch of beads at the dollar store. Alas! They didn’t have enough weight to work consistently (though they did better than the bigger marbles).

But, I found a solution! The perfect solution. One that rarely fails!

Are you excited yet?

(putting blank space so you can’t peek)

Here it goes!

Yeah. Lame. While searching the dollar store for heavier beads, I found this. So, my 15+ hours of labor, with my sophisticated joinery, expensive hardwood, and checking/rechecking of squareness resulted in a \$1 bubblegum dispenser.

Now what in the world will I use that old one for? Any ideas?

Query writing: Knowing what details to add

Posted in for writers, Writing

Last we spoke (digitally, that is), we developed the bones from a query template. Now your query is a walking-talking skeleton, with enough form to terrify readers, but not land a book deal. Feeling good yet? Got some direction? Hopefully your informal critique group (those friends, family members, and sentient plants you’re so fond of) has an idea of the main character’s central conflict, goal, and stakes. Now what are we going to do? We’re going to add details, but only if they:

1. Raise the stakes,

2. Reveal the character arc, and/or

3. Show obstacles.

1. Raise the stakes.

We hear that a lot in writing, neh? What do we mean?

Raise the stakes. /rāz T͟Hə stāks/ Sentence. A term used among writing wonks to indicate an escalation of cost associated with the protagonist losing.

In non-technical terms, “what does the main character lose if (s)he fails?” A love? A life? A basketball? Tickets to a rodeo? A toe nail? (I hear you gasping already).

Here’s one secret to a great query–the higher the stakes, the better the query. Does that mean lives must be in danger? No! But something meaningful to us and the character must be at stake.

Examples in Literature

What happens if Katniss loses the Hunger Games? Her life, of course. But Suzanne Collins raised the stakes by having her family rely on her success. If Katniss fails, who will take care of Prim? Who will gather food for their family? And what of her budding romance with Gale?

What happens if Harry Potter fails to defeat Valdemort? Harry will die, but of course there’s more than that–muggles will live under the rule of a horrible tyrant, friends and family will be executed, Dumbledore’s death will remain unavenged, and he’d never get to be with that fox Ginny Weasley.

(And I was so going to put an example from the romance genre, but I don’t read romance. Care to comment? with an example?)

Application

So what does that mean for a query? After last post, our query was a bit skimpy. (I did warn you it was the bones of a query). Here’s where we’re going to do–we will add details that raise the stakes.

Let’s think about how to raise the stakes. Here’s where we left off:

Tommy the goat is the oldest of two older brothers who all enjoy feasting on grass in the meadow. Yet when Bibbledy-Basty McFladigan, the neighborhood troll sets their meadow on fire, Tommy and his two brothers’ very existence is threatened.

Now, Tommy and his brothers embark on a journey across the bridge that will lead them to another pasture. As they attempt to cross, Bibbledy-Basty threatens to eat them. Forced to choose between certain death by starvation and probable death by troll, Tommy must decide whether to face his fears or starve.

How can we make it cost more if Tommy fails to defeat the troll? For one, we can make it so he’s not living for himself anymore. Like Katniss, let’s give him someone who relies on him:

Tommy the goat is a proud new father of who dreams of teaching his baby boy to trim his goatee (you saw that coming, didn’t you?).

Okay…that’s better. How can we raise the stakes even more? Well, why not have the evil troll kidnap his wifey-pooh. Now we’re talking!

As they prepare to cross, Bibbledy-Basty kidnaps Tommy’s wife and threatens her life. Either he must forfeit the green meadows and his wife to the troll, or risk her death to save his son from starvation.

Again, it’s still not good writing, but the stakes are clear.

(As an aside, this example’s a bit contrived. In reality, your novel will already have high stakes and you will simply choose which details will heighten the tension, but you get the idea.)

2. Character arc. Character arc. /ˈkerəktər ärk/ Term. The process of change the main character experiences during the novel.

Put differently–what sort of transformation occurs in the main character?

In addition to conveying what the character’s arc is, we must also convey what resources the MC uses to do that.

Example: Dorothy’s friend the lion is a coward. By the end of the novel, he realizes he’s overcome his cowardice through his propensity to helping Dorothy.

Another way to put it–the character goes from A to B using C.

So what’s our character arc? (Again, this should be known before we write the query). Let’s say, in our story, the MC has always been the youngest, smallest, weakest of the three brothers (point A). By the end, he becomes a leader (point B), using his paternal and familial instincts.

So now, let’s add those details to the query:

Tommy the goat is the youngest of three brothers. Long considered the “runt” of the family, it’s a surprise he’s the only brother who finds a woman who loves him. When his wife gives birth to a baby boy, he dreams of …

Cool. That looks good. Now we need to talk about his resources:

Driven to protect his son, Tommy must convince his older brothers and his village to cross the bridge that will lead them to another pasture

Nice, dude! (Yep, I’m congratulating myself). At this point, I’m okay with not telling the complete character arc because that will give away the ending. But, we’ve already included details that show elements of his character arc (e.g., he’s actively advocating a journey to the meadow).

3. Convey obstacles

A novel cannot exist without obstacles. Nor can your query work without telling us of the obstacles. We’ve already mentioned some (e.g., the troll stops them from crossing the bridge and holds his wife ransom), but let’s make that agent’s grip on your query letter tighten and tremble with anxiety for you MC!

So…maybe the other meadow is far away. Cool. Obstacle #1–check. And, they have to cross a river filled with deadly snakes. Nice. Obstacle #2–check. And, his brothers keep insisting they go back. Woot woot! Obstacle #3–check. Now let’s put that into our query:

Yet his brothers refuse to embark on the two week journey across mountains, snake-filled rivers, and barren landscapes.

Okay…so I embellished a bit 🙂

Bringing it all together

So now with those details, let’s take a look at our new query:

Tommy the goat is the youngest of three brothers. Long considered the “runt” of the family, it’s a surprise he’s the only brother who finds a woman who loves him. When his wife gives birth to a baby boy, he dreams of teaching him to trim his goatee. Yet when Bibbledy-Basty McFladigan, the neighborhood troll sets their meadow on fire, Tommy and his infant son’s very existence is threatened.

Driven to protect his son, Tommy must convince his older brothers and his village to cross the bridge that will lead them to another pasture. Yet his brothers refuse to embark on the two week journey across mountains, snake-filled rivers, and barren landscapes. As they prepare to cross, Bibbledy-Basty kidnaps Tommy’s wife and threatens her life. Either Tommy must forfeit the green meadows and his wife to the troll, or risk his family’s death to save his son from starvation.

Again, it’s not the best writing, but we’ll worry about that in the next round of revisions when we punch-ify our verbs.

Happy writing!

The contents() function in fifer

Posted in Statistics
Introduction

I’m ashamed to admit it. I often forget the functions in my own package. You’d think that after spending hours creating it, hours more debugging it, and even more hours getting it to pass CRAN’s militant package checklist, I’d remember. But no, sadly. Was it string.to.color? Or strings.to.colors? Or StringsToColors? At first I assigned aliases (e.g., string.to.color and string.to.colors all point to the same documentation), but sometimes that wasn’t enough. I’d say something like, “I remember creating a function that….er…maybe it like rotates a plot or something? What was its name?”I used to type

ls("package:fifer")

But that got annoying (and I could never remember the command). So I created one called contents().

How to use it

To know what functions a package contains, type the following:

contents("packagename")

For example, to see what’s in MASS, type

contents("MASS")

To see what’s in fifer, type

contents("fifer")

or just

contents()

And bam! Yer done.

Murphy’s Law Has Struck!!!

Posted in Misc

You should be shocked that I’m saying this. As a psychologist, I know better than to participate in confirmation bias (only paying attention to details that confirm a hypothesis).  As a statistician, I should recognize that crap happens, and sometimes simultaneously. But this??

So my youngest boy is up all night vomiting (probability ~ 1/500). My wife has already signed up to chaperone a field-trip (p ~ 2/365). I’ve got a paper draft due to the boss today (p ~ 1/30). AND the internet stops working. Seriously!?!?!?! (p ~ 1/100…stupid COX). So as I’m bathing a vomit-covered toddler, the stench is making me gag, and I’ve gotta get the other kiddos ready for their fieldtrip while the misses fetches pedialyte popsicles from the store, and fix the internet, and clean up the mess before the entire household starts barfing.

And then my carpet vacuum stops working (p~1/1095). So now the stench will linger until Amazon can deliver a new one.

So let’s compute the probabilities, shall we?

p(not murphy) = 1/500 x 2/365 x 1/30 x 1/100 = 3.65e-09

For the mathematically disinclined, that’s a 3 preceded by nine zeroes!

Yup, Murphy wins this one.

So here’s the email I sent to my boss:

So my wife is scheduled to go with the kids on a field trip. Last night my youngest started vomiting. So while the misses is fieldtripping, I’m home with the barf faucet. Between potent splatters, I’m working on getting the paper draft to you. THEN THE INTERNET GOES OUT!!!! Seriously??? I need a new modem (apparently) for the third time in as many years (stupid cox) and that won’t happen til tomorrow.

So, until the misses gets home with the non-barfers, I’m staying connected via cellular. (And typing typos with my fat thumbs). When they get back, I’ll find me a McDonald’s or a Starbucks to get that draft to you. Does that work?

Okay. Rant over 🙂

Dustin

Ps–my shampoo vac also gave up the ghost this morning, so my house will smell like this until Cox’s competitors come (att) so I can get internet so I can research vacuums so I can clean up the stench.

PPS–now my rant is over. 🙂

And to top it off, I now have a cold. Blah.