简体   繁体   English

尝试使用 R 中的 model 预测列中的值

[英]Trying to predict values in a column using a model in R

I'll state up front that I have little experience in R, and really appreciate any help folks can provide.我会预先说明 state 我在 R 方面的经验很少,并且非常感谢人们可以提供的任何帮助。

I have a data frame (I think) of values:我有一个值的数据框(我认为):

enter image description here在此处输入图像描述

I am trying to predict each value of the "PredBFwdth" column, using a model I've created from other data and the values of "ThatwgFAC" and "ThalwgSlop".我正在尝试使用我从其他数据创建的 model 以及“ThatwgFAC”和“ThalwgSlop”的值来预测“PredBFwdth”列的每个值。

The name of the model is "Model"; model的名称为“型号”; it is a generalized additive model, so I would be using predict.gam.它是一个广义的添加剂 model,所以我会使用 predict.gam。 I thought the code would look something like:我认为代码看起来像:

PredBFwdth = predict.gam(Model, ThalwgFAC, ThalwgSlop)

This doesn't work, obviously.这显然行不通。 I'll need to make predictions for every row in the data, using corresponding values from the ThalwgFAC and ThalwgSlop columns and the model.我需要使用来自 ThalwgFAC 和 ThalwgSlop 列以及 model 的相应值对数据中的每一行进行预测。 Can someone assist?有人可以帮忙吗? I've poked around looking for answers, but the code on other folks questions is indecipherable to me.我四处寻找答案,但其他人问题的代码对我来说是难以理解的。 :( :(

I strongly recommend giving enough data and code to create a reproducible example.我强烈建议提供足够的数据和代码来创建可重现的示例。 That means a sample of what your data could look like and what code you are trying in order to facilitate help.这意味着您的数据可能是什么样的示例以及您正在尝试使用哪些代码以促进帮助。

First I'll start with some fake data that is in a data.frame首先,我将从 data.frame 中的一些假数据开始

dat <- data.frame(

ThalwgFAC = rnorm(100,3,5),
ThalwgSlop = rnorm(100,3,2),
ThalwgEv = rnorm(100,2,2)
)

Next, you fit your model接下来,您安装您的 model

library(mgcv)

fit <- gam(ThalwgEv ~ s(ThalwgFAC) + s(ThalwgSlop), data = dat)

Now to your question about predicting new data, you can just feed the new data (as a data frame) to the predict function and specify which model you are going to use (the fit object from above).现在关于预测新数据的问题,您可以将新数据(作为数据框)提供给predict function 并指定您要使用的 model (上面的fit ZA8CFDE6331BD59EB2AC96F8911C46)。 This presumes the data frame contains the same variable names that were used for the model (eg ThalwgFAC and ThalwgSlop).这假定数据帧包含用于 model 的相同变量名称(例如 ThalwgFAC 和 ThalwgSlop)。

dat2 <- data.frame(
  
  ThalwgFAC = rnorm(100,3,5),
  ThalwgSlop = rnorm(100,3,2),
  ThalwgEv = rnorm(100,2,2)
)

pred <- predict(fit, newdata = dat2)

The predict function will pick up the class of the fit object, so no need to specify the method to use (ie predict.gam). predict function 将选取fit object 的 class,因此无需指定使用的方法(即 predict.gam)。

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

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