R 如何平滑数据,同时将平滑后的数据值输出到新的数据帧中?

R 如何平滑数据,同时将平滑后的数据值输出到新的数据帧中?,r,ggplot2,dplyr,plotly,R,Ggplot2,Dplyr,Plotly,我有一组我重新取样的数据,是否有一个函数的命令,我可以在R中使用它来首先平滑数据,然后才从创建的数据帧创建图形 我的数据有很多噪声,在我重新采样数据后,现在我想平滑数据,我使用geom_smooth生成数据的图形,但是命令只创建平滑数据的图形表示,而没有给出它表示的点的值 use ggplot library(ggplot2) library(dplyr) library(plotly) df <- read.csv("data.csv", header = T) str(df)

我有一组我重新取样的数据,是否有一个函数的命令,我可以在R中使用它来首先平滑数据,然后才从创建的数据帧创建图形

我的数据有很多噪声,在我重新采样数据后,现在我想平滑数据,我使用
geom_smooth
生成数据的图形,但是命令只创建平滑数据的图形表示,而没有给出它表示的点的值


use ggplot
library(ggplot2)
library(dplyr)
library(plotly)

df <- read.csv("data.csv", header = T)

str(df)

rs <- sample_n(df,715)

q <- 
  ggplot(df,aes(x,y)) + 
  geom_line() + 
  geom_smooth(method = "loess", formula = y~log(x), span = 0.05)

我也试过了

ml <- with(rs, loess(formula = y~log(x), span = 0.5))

mp <- predict(ml)

ml这两行不一样:

ml <- with(rs, loess(formula = y~log(x), span = 0.5))

ml <- loess(formula = y~log(x), with(rs), span = 0.5)

如上所述,
leash
函数在平滑数据方面的作用与
geom_平滑(method=“leash”)
相同,然后当模型传递到
predict
函数时,您会得到新因变量的向量。您可以在图形上绘制这些内容以检查:

library(dplyr)
library(ggplot2)

a <- rnorm(100)
b <- rnorm(100, mean = 4, sd = 20)*a

df <- tibble(a,b)

df_predict <- df[,"a"]

df_predict[,"b"] <- df %>%
  loess(b ~ a, data = ., span = 0.5) %>% 
  predict()

df %>%
  ggplot(aes(a,b)) +
  geom_point(col = "blue") +
  geom_smooth(method = "loess", span = 0.5, col = "red") +
  geom_point(data = df_predict, col = "red")

df_predict

# A tibble: 100 x 2
        a       b
    <dbl>   <dbl>
 1  0.116   0.502
 2  0.870  -3.44 
 3  0.336   1.16 
 4 -1.16   -9.32 
 5  1.73    8.88 
 6  0.236   0.756
 7  0.485   0.302
 8 -1.13   -9.58 
 9 -0.778 -10.1  
10 -2.76   11.9  
# ... with 90 more rows
库(dplyr)
图书馆(GG2)
a
考虑到我只需要知道一个变量的平滑值,我使用:
#仅平滑一个变量

ml这可能会有帮助:尝试
lusk(公式=y~log(x),数据=rs,span=0.5)
我这样做是因为stack一直在说“您的帖子似乎包含未正确格式化为代码的代码。请使用代码工具栏按钮或CTRL+K键盘快捷键将所有代码缩进4个空格。有关更多编辑帮助,请单击[?]工具栏图标。”就我的一生而言,我无法找出我的格式哪里错了
ml <- with(rs, loess(formula = y~log(x), span = 0.5))

ml <- loess(formula = y~log(x), with(rs), span = 0.5)
ml <- loess(formula = y~log(x), with(rs, rs), span = 0.5)
library(dplyr)
library(ggplot2)

a <- rnorm(100)
b <- rnorm(100, mean = 4, sd = 20)*a

df <- tibble(a,b)

df_predict <- df[,"a"]

df_predict[,"b"] <- df %>%
  loess(b ~ a, data = ., span = 0.5) %>% 
  predict()

df %>%
  ggplot(aes(a,b)) +
  geom_point(col = "blue") +
  geom_smooth(method = "loess", span = 0.5, col = "red") +
  geom_point(data = df_predict, col = "red")

df_predict

# A tibble: 100 x 2
        a       b
    <dbl>   <dbl>
 1  0.116   0.502
 2  0.870  -3.44 
 3  0.336   1.16 
 4 -1.16   -9.32 
 5  1.73    8.88 
 6  0.236   0.756
 7  0.485   0.302
 8 -1.13   -9.58 
 9 -0.778 -10.1  
10 -2.76   11.9  
# ... with 90 more rows
Considering I only needed to know the smooth value of one variable, I used:

#smoothing out only one variable
ml <- loess(formula = rs$MCO2~log(rs$Num),  span = 0.5)

#predicting the values of the smooth data
mp <- predict(ml)

#insert predicted data values into resampled data frame
rs$pre <- mp

I added a new column to my data consisting of a number series of my data (1-the end), so I can insert my data into the `y~log(x)` formula, because when I entered the `t` variable which is a `as.POSIXct` date and time argument, it resulted in an error.

To keep the values of the predicted data, I used:

write.csv(rs,"newdata.csv", row.names = FALSE)

Thank you for all the help and anwers.

@Andrew Baxter
@S Robidoux
@Jon Spring
@Jimbou