Why can't I fit a curve to this plot?
I am trying to fit a curve to this plot, but am having trouble.
Code:
library(ggplot2)
data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")
lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))
lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")
Result:
r plot curve
add a comment |
I am trying to fit a curve to this plot, but am having trouble.
Code:
library(ggplot2)
data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")
lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))
lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")
Result:
r plot curve
Aredata$volume
andpredict(...)
ordered how you want them?
– mickey
Nov 13 '18 at 14:36
Yes, they're ordered.
– Luís Telles
Nov 13 '18 at 14:53
add a comment |
I am trying to fit a curve to this plot, but am having trouble.
Code:
library(ggplot2)
data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")
lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))
lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")
Result:
r plot curve
I am trying to fit a curve to this plot, but am having trouble.
Code:
library(ggplot2)
data <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
plot(data, ylim = c(min(data[,2]), max(data[,2])), xlim = c(min(data[,1]), max(data[,1])),
pch = 19, col = "firebrick")
lm_fit <- lm(data$pressure ~ poly(data$volume, 2, raw = TRUE))
lines(data$volume, predict (lm_fit, data.frame(x = data$volume)), col = "red")
Result:
r plot curve
r plot curve
edited Nov 13 '18 at 15:04
Ben Bolker
133k11223311
133k11223311
asked Nov 13 '18 at 14:30
Luís TellesLuís Telles
576212
576212
Aredata$volume
andpredict(...)
ordered how you want them?
– mickey
Nov 13 '18 at 14:36
Yes, they're ordered.
– Luís Telles
Nov 13 '18 at 14:53
add a comment |
Aredata$volume
andpredict(...)
ordered how you want them?
– mickey
Nov 13 '18 at 14:36
Yes, they're ordered.
– Luís Telles
Nov 13 '18 at 14:53
Are
data$volume
and predict(...)
ordered how you want them?– mickey
Nov 13 '18 at 14:36
Are
data$volume
and predict(...)
ordered how you want them?– mickey
Nov 13 '18 at 14:36
Yes, they're ordered.
– Luís Telles
Nov 13 '18 at 14:53
Yes, they're ordered.
– Luís Telles
Nov 13 '18 at 14:53
add a comment |
2 Answers
2
active
oldest
votes
Use ggplot2: geom_smooth lm method
library(ggplot2)
df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")
# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()
add a comment |
Your main problem is using data$
inside your regression model: you should use just the names of the variables.
dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
data=dd)
For a smoother curve, I computed the predicted value at a finer sequence of volume
values:
pframe <- data.frame(volume=seq(0,30,length=51))
pframe$pressure <- predict(lm_fit,newdata=pframe)
Now the picture:
## png("SO_poly.png")
par(las=1,bty="l") ## cosmetic
plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
ylim=c(-100,500))
with(pframe, lines(volume, pressure, col="red"))
This doesn't look great, so I tried some other curve-fits.
Log-linear fit:
lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
data=dd)
pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
with(pframe, lines(volume, lpressure, col="purple"))
Exponential fit:
glm_fit <- glm(pressure ~ poly(volume,2),
family=gaussian(link="log"),
data=dd)
pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
with(pframe, lines(volume, gpressure, col="blue"))
## dev.off()
You could also use ggplot2
:
library(ggplot2)
ggplot(dd, aes(volume,pressure))+
geom_point()+
geom_smooth(method="lm",
formula=y~poly(x,2))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283261%2fwhy-cant-i-fit-a-curve-to-this-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use ggplot2: geom_smooth lm method
library(ggplot2)
df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")
# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()
add a comment |
Use ggplot2: geom_smooth lm method
library(ggplot2)
df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")
# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()
add a comment |
Use ggplot2: geom_smooth lm method
library(ggplot2)
df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")
# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()
Use ggplot2: geom_smooth lm method
library(ggplot2)
df <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
# Fit a regression line. Change the method if you want the exponential fitting
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_smooth(method = "lm")
# If you just want to connect the dots
ggplot(data = df, aes(x = pressure, y = volume)) +
geom_point() +
geom_line()
answered Nov 13 '18 at 14:41
kon_ukon_u
1966
1966
add a comment |
add a comment |
Your main problem is using data$
inside your regression model: you should use just the names of the variables.
dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
data=dd)
For a smoother curve, I computed the predicted value at a finer sequence of volume
values:
pframe <- data.frame(volume=seq(0,30,length=51))
pframe$pressure <- predict(lm_fit,newdata=pframe)
Now the picture:
## png("SO_poly.png")
par(las=1,bty="l") ## cosmetic
plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
ylim=c(-100,500))
with(pframe, lines(volume, pressure, col="red"))
This doesn't look great, so I tried some other curve-fits.
Log-linear fit:
lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
data=dd)
pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
with(pframe, lines(volume, lpressure, col="purple"))
Exponential fit:
glm_fit <- glm(pressure ~ poly(volume,2),
family=gaussian(link="log"),
data=dd)
pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
with(pframe, lines(volume, gpressure, col="blue"))
## dev.off()
You could also use ggplot2
:
library(ggplot2)
ggplot(dd, aes(volume,pressure))+
geom_point()+
geom_smooth(method="lm",
formula=y~poly(x,2))
add a comment |
Your main problem is using data$
inside your regression model: you should use just the names of the variables.
dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
data=dd)
For a smoother curve, I computed the predicted value at a finer sequence of volume
values:
pframe <- data.frame(volume=seq(0,30,length=51))
pframe$pressure <- predict(lm_fit,newdata=pframe)
Now the picture:
## png("SO_poly.png")
par(las=1,bty="l") ## cosmetic
plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
ylim=c(-100,500))
with(pframe, lines(volume, pressure, col="red"))
This doesn't look great, so I tried some other curve-fits.
Log-linear fit:
lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
data=dd)
pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
with(pframe, lines(volume, lpressure, col="purple"))
Exponential fit:
glm_fit <- glm(pressure ~ poly(volume,2),
family=gaussian(link="log"),
data=dd)
pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
with(pframe, lines(volume, gpressure, col="blue"))
## dev.off()
You could also use ggplot2
:
library(ggplot2)
ggplot(dd, aes(volume,pressure))+
geom_point()+
geom_smooth(method="lm",
formula=y~poly(x,2))
add a comment |
Your main problem is using data$
inside your regression model: you should use just the names of the variables.
dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
data=dd)
For a smoother curve, I computed the predicted value at a finer sequence of volume
values:
pframe <- data.frame(volume=seq(0,30,length=51))
pframe$pressure <- predict(lm_fit,newdata=pframe)
Now the picture:
## png("SO_poly.png")
par(las=1,bty="l") ## cosmetic
plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
ylim=c(-100,500))
with(pframe, lines(volume, pressure, col="red"))
This doesn't look great, so I tried some other curve-fits.
Log-linear fit:
lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
data=dd)
pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
with(pframe, lines(volume, lpressure, col="purple"))
Exponential fit:
glm_fit <- glm(pressure ~ poly(volume,2),
family=gaussian(link="log"),
data=dd)
pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
with(pframe, lines(volume, gpressure, col="blue"))
## dev.off()
You could also use ggplot2
:
library(ggplot2)
ggplot(dd, aes(volume,pressure))+
geom_point()+
geom_smooth(method="lm",
formula=y~poly(x,2))
Your main problem is using data$
inside your regression model: you should use just the names of the variables.
dd <- data.frame(pressure = c(400, 200, 100, 50, 25, 12.5, 6.25, 3.125),
volume = c(.25, .5, 1, 2, 4, 8, 16, 32))
lm_fit <- lm(pressure ~ poly(volume, 2, raw = TRUE),
data=dd)
For a smoother curve, I computed the predicted value at a finer sequence of volume
values:
pframe <- data.frame(volume=seq(0,30,length=51))
pframe$pressure <- predict(lm_fit,newdata=pframe)
Now the picture:
## png("SO_poly.png")
par(las=1,bty="l") ## cosmetic
plot(pressure~volume, data=dd, pch = 19, col = "firebrick",
ylim=c(-100,500))
with(pframe, lines(volume, pressure, col="red"))
This doesn't look great, so I tried some other curve-fits.
Log-linear fit:
lm_fit2 <- lm(log(pressure) ~ poly(volume, 2, raw = TRUE),
data=dd)
pframe$lpressure <- exp(predict(lm_fit2,newdata=pframe))
with(pframe, lines(volume, lpressure, col="purple"))
Exponential fit:
glm_fit <- glm(pressure ~ poly(volume,2),
family=gaussian(link="log"),
data=dd)
pframe$gpressure <- predict(glm_fit, newdata=pframe, type="response")
with(pframe, lines(volume, gpressure, col="blue"))
## dev.off()
You could also use ggplot2
:
library(ggplot2)
ggplot(dd, aes(volume,pressure))+
geom_point()+
geom_smooth(method="lm",
formula=y~poly(x,2))
answered Nov 13 '18 at 15:04
Ben BolkerBen Bolker
133k11223311
133k11223311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283261%2fwhy-cant-i-fit-a-curve-to-this-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are
data$volume
andpredict(...)
ordered how you want them?– mickey
Nov 13 '18 at 14:36
Yes, they're ordered.
– Luís Telles
Nov 13 '18 at 14:53