简体   繁体   English

如何在R中的cv.glmnet或glmnet函数中找到拟合值?

[英]How to find the fitted value in cv.glmnet or glmnet function in R?

Suppose I have generated the following data set and fitted the model using cv.glmnet function in R . 假设我生成了以下数据集,并使用R cv.glmnet函数拟合了模型。

library(glmnet)
means <- c(-1, 1.3, 0.6, 2, 1.5, -0.7, 2.5)
size <- c(50, 50, 100, 200, 350, 50, 200)


t = list()
  for (i in 1:length(means)) {
   t[[i]] <- c(rnorm(size[i],mean = means[i],sd = 0.5))
  }
y <- c(unlist(t))
plot(y,type = 'l')

lower_tri <- function(i){ 
  a <- matrix(1,i,i)
  a[upper.tri(a)] <- 0
  return(a)
}
x <- lower_tri(sum(size))[,2:sum(size)]

cv_fit <- cv.glmnet(x,y)

How to find the fitted value using cv_fit1 ? 如何使用cv_fit1查找拟合值? I used fitted() function for lm model. 我对lm模型使用了fitted()函数。

Thank you very much. 非常感谢你。

Asking for the fitted values of a cv.glmnet -object from pkg glmnet is wrong headed. 从pkg glmnet询问cv.glmnetfitted值是错误的。 Cross-validation is being done on a relatively wide variety of models with varying structures, so it's not really doing anything equivalent to glm which has a single model and a single result. 交叉验证是在具有不同结构的相对广泛的各种模型上进行的,因此,它实际上并没有执行与具有单个模型和单个结果的glm等效的任何操作。 The goal of cv.glmnet it to give you results that let you choose the optimal level of complexity of the glmnet procedure given the nature of your data. cv.glmnet的目标是为您提供结果,让您根据数据的性质选择glmnet过程的最佳复杂度。 It is a step on the way to choosing a glmnet model, ie choosing lambda. 这是选择glmnet模型(即选择lambda)的一步。 You could use this to get a prediction: 您可以使用它来获得预测:

 predict( glmnet(x,y), s=cv_fit$lambda.min )  

I don't see the example offered as being particularly useful to support further discussion since it appears rather "degenerate" with a minimal level of complexity. 我认为所提供的示例对于支持进一步的讨论不是特别有用,因为它看起来“退化”且具有最低程度的复杂性。

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

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