简体   繁体   English

如何在 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)

My code is above, and the picture is as follow.我的代码在上面,图片如下。 [1]: https://i.stack.imgur.com/gdZ4W.png What can I fix my code, I need it draw like as follow. [1]: https : //i.stack.imgur.com/gdZ4W.png我可以修复我的代码,我需要绘制如下。 Thanks [2]: https://i.stack.imgur.com/0Afnt.png感谢 [2]: https : //i.stack.imgur.com/0Afnt.png

With your sample data, this work for me:使用您的示例数据,这对我有用:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM