How to fit a smooth curve to my data in R?

前端 未结 8 1909
无人共我
无人共我 2020-11-29 15:59

I\'m trying to draw a smooth curve in R. I have the following simple toy data:

> x
 [1]  1  2  3  4  5  6  7  8  9 10
> y
 [1]  2  4  6  8         


        
8条回答
  •  误落风尘
    2020-11-29 16:31

    I didn't see this method shown, so if someone else is looking to do this I found that ggplot documentation suggested a technique for using the gam method that produced similar results to loess when working with small data sets.

    library(ggplot2)
    x <- 1:10
    y <- c(2,4,6,8,7,8,14,16,18,20)
    
    df <- data.frame(x,y)
    r <- ggplot(df, aes(x = x, y = y)) + geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs"))+geom_point()
    r
    

    First with the loess method and auto formula Second with the gam method with suggested formula

提交回复
热议问题