R-向观测矩阵添加外推(lm)值
我正在尝试向R中的矩阵添加一组外推的“观察值”。我知道如何使用常规编程技术(读取;一堆嵌套的循环和函数)来实现这一点,但我觉得通过使用内置的R功能,必须以更干净的方式实现这一点 下面的代码说明了这一点,以及它的故障点 非常感谢您的帮助 致以亲切的问候 西尔万R-向观测矩阵添加外推(lm)值,r,matrix,lm,extrapolation,R,Matrix,Lm,Extrapolation,我正在尝试向R中的矩阵添加一组外推的“观察值”。我知道如何使用常规编程技术(读取;一堆嵌套的循环和函数)来实现这一点,但我觉得通过使用内置的R功能,必须以更干净的方式实现这一点 下面的代码说明了这一点,以及它的故障点 非常感谢您的帮助 致以亲切的问候 西尔万 library(dplyr) # The idea is that i have a table of observations for e.g. x=5, 6, 7, 8, 9 and 10. The observations (in
library(dplyr)
# The idea is that i have a table of observations for e.g. x=5, 6, 7, 8, 9 and 10. The observations (in this example 2)
# conform fairly decently to sets of 2nd order polynomials.
# Now, I want to add an extrapolated value to this table (e.g. x=4). I know how to do this programmically
# but I feel there must be a cleaner solution to do this.
#generate dummy data table
x <- 5:10
myData <- tibble(x, a = x^2 * 2 + x * 3 + 4 + rnorm(1,0,0.01), b = x^2 * 3 + x * 4 + 5 + rnorm(1,0,0.01) )
#Gather (put in Data-Key format)
myDataKeyFormat <- gather(myData,key = "someLabel", value = "myObservation", -x)
fitted_models <- myDataKeyFormat %>% group_by(someLabel) %>% do(model = lm(myObservation ~ poly(x,2), data = .))
myExtrapolatedDataPointx <- tibble(x = 4)
#Add the x=4 field
fitted_points <- fitted_models %>% group_by(someLabel) %>% do(predict(.$model,myExtrapolatedDataPointx)) #R really doesnt like this bit
#append the fitted_points to the myDataKeyFormat
myDataKeyFormatWithExtrapolation <- union(myDataKeyFormat,fitted_points)
#use spread to
myDataWithExtrapolation <- myDataKeyFormatWithExtrapolation %>% spread(someLabel,myObservation)
库(dplyr)
#我的想法是,我有一个观察表,例如x=5、6、7、8、9和10。观察结果(在本例2中)
#完全符合二阶多项式的集合。
#现在,我想给这个表添加一个外推值(例如x=4)。我知道如何通过编程实现这一点
#但我觉得必须有一个更干净的解决方案来做到这一点。
#生成虚拟数据表
x这是tidyverse中的一个解决方案,使用purr
创建不同的模型。其思想是嵌套(使用tidyr::nest
),然后purr::map
来训练模型。然后,我将添加新值并使用modeler::add_predictions
计算预测。在这里,所有的数据都在同一个地方:训练数据、模型、测试数据和预测,通过变量someLabel
。我还提供了一种可视化数据的方法。
您可以查看Hadley Wickham&Garrett Grolemund,尤其是关于模型的部分,以了解更多信息
library(dplyr)
library(tibble)
library(tidyr)
library(purrr)
library(modelr)
library(ggplot2)
set.seed(1) # For reproducibility
x <- 5:10
myData <- tibble(x,
a = x^2 * 2 + x * 3 + 4 + rnorm(1,0,0.01),
b = x^2 * 3 + x * 4 + 5 + rnorm(1,0,0.01))
#Gather (put in Data-Key format)
myDataKeyFormat <- gather(myData,key = "someLabel", value = "myObservation", -x)
myModels <- myDataKeyFormat %>%
nest(-someLabel) %>%
mutate(model = map(data, ~lm(myObservation ~ poly(x,2), data = .x)))
我添加预测:add_predictions
将数据帧和模型作为参数,因此我使用map2
映射新数据和模型
fitted_models <- new_data %>%
mutate(new = map2(new, model, ~add_predictions(.x, .y)))
fitted_models
# A tibble: 2 × 4
someLabel data model new
<chr> <list> <list> <list>
1 a <tibble [6 × 2]> <S3: lm> <tibble [4 × 2]>
2 b <tibble [6 × 2]> <S3: lm> <tibble [4 × 2]>
非常感谢@FlorianGD,这正是我想要做的!
# New data
new_data <- myModels %>%
mutate(new = map(data, ~tibble(x = c(3, 4, 11, 12))))
fitted_models <- new_data %>%
mutate(new = map2(new, model, ~add_predictions(.x, .y)))
fitted_models
# A tibble: 2 × 4
someLabel data model new
<chr> <list> <list> <list>
1 a <tibble [6 × 2]> <S3: lm> <tibble [4 × 2]>
2 b <tibble [6 × 2]> <S3: lm> <tibble [4 × 2]>
my_points <- bind_rows(unnest(fitted_models, data),
unnest(fitted_models, new))
ggplot(my_points)+
geom_point(aes(x = x, y = myObservation), color = "black") +
geom_point(aes(x = x, y = pred), color = "red")+
facet_wrap(~someLabel)