如何在 R 中添加对数回归
How do I add a logarithmic regression in R
x <- shapefile('42GP.shp')
A <- x$PatchArea1
df <- read.csv("CI.csv",header = T)
R <- df$mean
plot(R~A)
fit <- lm(R~log(A))
summary(fit)
coef(fit)
x=A
y=predict(fit,newdata=list(A)),interval="confidence"))
matlines(x,y,lwd=2)
我的代码在上面,图片如下。
[1]: https://i.stack.imgur.com/gdZ4W.png
我可以修复我的代码,我需要它像下面这样绘制。谢谢
[2]: https://i.stack.imgur.com/0Afnt.png
使用您的示例数据,这对我有用:
library(ggplot2)
x <- 1:20
set.seed(1)
y <- 3*log(x)+5+rnorm(20)
fit <- lm(y~log(x))
df <- data.frame(x = x, y =y ,
predict = fit$coefficients[1] +
fit$coefficients[2] * log(x))
ggplot(data = df) +
geom_point(aes(x=x,y=y)) +
geom_line(aes(x=x,y=predict))
x <- shapefile('42GP.shp')
A <- x$PatchArea1
df <- read.csv("CI.csv",header = T)
R <- df$mean
plot(R~A)
fit <- lm(R~log(A))
summary(fit)
coef(fit)
x=A
y=predict(fit,newdata=list(A)),interval="confidence"))
matlines(x,y,lwd=2)
我的代码在上面,图片如下。 [1]: https://i.stack.imgur.com/gdZ4W.png 我可以修复我的代码,我需要它像下面这样绘制。谢谢 [2]: https://i.stack.imgur.com/0Afnt.png
使用您的示例数据,这对我有用:
library(ggplot2)
x <- 1:20
set.seed(1)
y <- 3*log(x)+5+rnorm(20)
fit <- lm(y~log(x))
df <- data.frame(x = x, y =y ,
predict = fit$coefficients[1] +
fit$coefficients[2] * log(x))
ggplot(data = df) +
geom_point(aes(x=x,y=y)) +
geom_line(aes(x=x,y=predict))