R in lieu of a Graphing Calculator: doing it wrong?
July 11, 2015 6:01 PM Subscribe
I'm studying calculus. I don't have a graphing calculator but I do have R and decent amount of experience using it to do light data analysis. It seems like R ought to be able to do anything a calculator can, but so far that hasn't panned out.
Where by "studying calculus" I mean that I've got a copy of Stewart's Calculus 6e and I'm hoping to work through it on my own this summer. A first round of questions in the first chapter has the student plot out a series of data sets, and then use the graphing calculator to find the model for a line that fits each. A linear model was pretty straightforward -- I can use
But a cubic model ... I'm stumped. Given a table of data like:
q25 <- read.table(text = "year pop.in.millions
1900 1650
1910 1750
1920 1860
1930 2070
1940 2300
1950 2560
1960 3040
1970 3710
1980 4450
1990 5280
2000 6070
",header = TRUE, sep = "")
I can use
I even found instructions for using
But can't find an equivalent function in R.
I did find some promising walk throughs but they all seem way, way more complex than breaking out ye olde TI-83. Unless you consider that don't have an old graphing calculator to break out.
I dropped all of the first set of graphing questions at:
https://gist.github.com/anonymous/5ceeecd87ace0ba749cf1
Is there a package I should be using in R? A guide for someone who is learning statistics (vs. applying their already vast comprehension of the subtleties of statistical modeling to new software)? Is this a ridiculous endeavor? In which case, is there another software I can run on Ubuntu to approximate the core functionality of a graphing calculator until I get a little farther down this road?
Where by "studying calculus" I mean that I've got a copy of Stewart's Calculus 6e and I'm hoping to work through it on my own this summer. A first round of questions in the first chapter has the student plot out a series of data sets, and then use the graphing calculator to find the model for a line that fits each. A linear model was pretty straightforward -- I can use
lm
to model it. But a cubic model ... I'm stumped. Given a table of data like:
q25 <- read.table(text = "year pop.in.millions
1900 1650
1910 1750
1920 1860
1930 2070
1940 2300
1950 2560
1960 3040
1970 3710
1980 4450
1990 5280
2000 6070
",header = TRUE, sep = "")
I can use
plot()
to see that it appears to be a cubic function. In any old math class TI-83 you'd use stat > calc > cubicreg
to spit out an equation that approximates the trend. There's no shortage of guides online to walk you through modelling a cubic function like that. I even found instructions for using
=LINEST
in Excel (though I can't get that to work in LibreOffice Calc). But can't find an equivalent function in R.
I did find some promising walk throughs but they all seem way, way more complex than breaking out ye olde TI-83. Unless you consider that don't have an old graphing calculator to break out.
I dropped all of the first set of graphing questions at:
https://gist.github.com/anonymous/5ceeecd87ace0ba749cf1
Is there a package I should be using in R? A guide for someone who is learning statistics (vs. applying their already vast comprehension of the subtleties of statistical modeling to new software)? Is this a ridiculous endeavor? In which case, is there another software I can run on Ubuntu to approximate the core functionality of a graphing calculator until I get a little farther down this road?
If you cube the X-axis (appears to be years), then the y variable will be a linear function of those values.
posted by SemiSalt at 6:10 PM on July 11, 2015
posted by SemiSalt at 6:10 PM on July 11, 2015
In which case, is there another software I can run on Ubuntu to approximate the core functionality of a graphing calculator until I get a little farther down this road?
You should be able to use Wolfram Alpha for anything you'd need a graphing calculator for.
posted by phunniemee at 6:18 PM on July 11, 2015
You should be able to use Wolfram Alpha for anything you'd need a graphing calculator for.
posted by phunniemee at 6:18 PM on July 11, 2015
Best answer: "If you cube the X-axis (appears to be years), then the y variable will be a linear function of those values."
This won't work - it assumes you have no quadratic or linear terms. A cubic fit should have 4 terms:
a * x^3 + b * x^2 + c *x + d
I don't know R well, but I think you want something like:
lm(y ~ poly(x, 3))
posted by NoDef at 6:23 PM on July 11, 2015 [2 favorites]
This won't work - it assumes you have no quadratic or linear terms. A cubic fit should have 4 terms:
a * x^3 + b * x^2 + c *x + d
I don't know R well, but I think you want something like:
lm(y ~ poly(x, 3))
posted by NoDef at 6:23 PM on July 11, 2015 [2 favorites]
Best answer: R absolutely can do this, but i don't use it myself. a little googling turns up this. maybe you were searching for help with "cubic" rather than "polynomial"?
posted by andrewcooke at 6:51 PM on July 11, 2015
posted by andrewcooke at 6:51 PM on July 11, 2015
The simplest way seems to be just
year3 <- year^3
year2 <- year^2
modeloutput <- lm(population ~ year3 + year2 + year)
But if you were actually analyzing this, you'd almost certainly run output <- lm(log(population) ~ year)
posted by ROU_Xenophobe at 7:59 PM on July 11, 2015 [1 favorite]
year3 <- year^3
year2 <- year^2
modeloutput <- lm(population ~ year3 + year2 + year)
But if you were actually analyzing this, you'd almost certainly run output <- lm(log(population) ~ year)
posted by ROU_Xenophobe at 7:59 PM on July 11, 2015 [1 favorite]
Google "graphing calculator in javascript" and find several.
posted by Obscure Reference at 8:26 PM on July 11, 2015
posted by Obscure Reference at 8:26 PM on July 11, 2015
In which case, is there another software I can run on Ubuntu to approximate the core functionality of a graphing calculator until I get a little farther down this road?
SageMath looks like it's only an apt-get away.
tour
calculus tutorial
integral calculus with sage pdf textook,
etc
posted by sebastienbailard at 8:44 PM on July 11, 2015
SageMath looks like it's only an apt-get away.
tour
calculus tutorial
integral calculus with sage pdf textook,
etc
posted by sebastienbailard at 8:44 PM on July 11, 2015
The typical way you do a 3rd-degree polynomial regression in R is like so:
The thing with
posted by en forme de poire at 2:10 AM on July 12, 2015 [1 favorite]
model <- lm(population ~ I(year**3) + I(year ** 2) + year)
. Under the hood I think this is exactly the same as what ROU_Xenophobe wrote, but without having to make any dummy variables.The thing with
lm
(and you may already know this bit, sorry if it's obvious) is that some of the basic arithmetic operators have different, specific meanings -- like, a * b
means you're adding a, b, and their interaction to the linear model, not that you're literally multiplying a and b. When you actually want those operators to be used in their "literal" arithmetic way you need to wrap them in a function call to I()
, which specifies that whatever's in the parentheses should be treated "as is." So for example, lm(y ~ a * b)
fits a linear model with terms for a, b, and their interaction, but lm(y ~ I(a * b))
fits a linear model with one term, which is the product of a and b.posted by en forme de poire at 2:10 AM on July 12, 2015 [1 favorite]
Best answer: Oh yeah, if you want to plot it, after plotting the original data, you'd do something like
To get interpolation in an lm you'd do, e.g.:
posted by en forme de poire at 2:41 AM on July 12, 2015
lines(year, predict(model))
(assuming the year vector is sorted).To get interpolation in an lm you'd do, e.g.:
new.years = seq(1900, 2000, 2)
lines(new.years, predict(model, data.frame(year = new.years))
posted by en forme de poire at 2:41 AM on July 12, 2015
Response by poster: Awesome. This totally works. For a bonus, since one of the (many, many) things I read said I should be using lattice rather than the built in graphing tools, I've got this working fabulously:
posted by amandabee at 11:07 AM on July 14, 2015
model <> xyplot(pop.in.millions ~ year, data = q25, panel = function(x, y) { panel.xyplot(x, y) panel.lines(x, predict(model,list(year = x) )) }, xlab = "Year", ylab = "Population (in Millions)")>
posted by amandabee at 11:07 AM on July 14, 2015
This thread is closed to new comments.
posted by lownote at 6:05 PM on July 11, 2015