install.packages(“glmnet“) 问题

 因为我的版本是C++11 但最新版本需要到C++14所以降版本就行

packageurl = 'https://cran.r-project.org/src/contrib/Archive/glmnet/glmnet_3.0.tar.gz'
install.packages(packageurl, repos = NULL, type = 'source')

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Turn your R code into packages that others can easily download and use. This practical book shows you how to bundle reusable R functions, sample data, and documentation together by applying author Hadley Wickham’s package development philosophy. In the process, you’ll work with devtools, roxygen, and testthat, a set of R packages that automate common development tasks. Devtools encapsulates best practices that Hadley has learned from years of working with this programming language. Ideal for developers, data scientists, and programmers with various backgrounds, this book starts you with the basics and shows you how to improve your package writing over time. You’ll learn to focus on what you want your package to do, rather than think about package structure. Learn about the most useful components of an R package, including vignettes and unit tests Automate anything you can, taking advantage of the years of development experience embodied in devtools Get tips on good style, such as organizing functions into files Streamline your development process with devtools Learn the best way to submit your package to the Comprehensive R Archive Network (CRAN) Learn from a well-respected member of the R community who created 30 R packages, including ggplot2, dplyr, and tidyr Table of Contents Part I. Getting Started Chapter 1. Introduction Chapter 2. Package Structure Part II. Package Components Chapter 3. R Code Chapter 4. Package Metadata Chapter 5. Object Documentation Chapter 6. Vignettes: Long-Form Documentation Chapter 7. Testing Chapter 8. Namespace Chapter 9. External Data Chapter 10. Compiled Code Chapter 11. Installed Files Chapter 12. Other Components Part III. Best Practices Chapter 13. Git and GitHub Chapter 14. Automated Checking Chapter 15. Releasing a Package
这段代码使用了一些R语言和函数来进行数据处理和线性回归分析。下面是对每个步骤的解释: 1. `install.packages('glmnet')` 和 `library(glmnet)`: 这些代码用于安装和加载名为 `glmnet` 的R,该提供了实现Lasso和弹性网络等回归方法的函数。 2. `graphics.off()`: 这个函数用于关闭图形设备,以便在之后重新绘制新的图形。 3. `rm(list = ls())`: 这个命令用于清空当前环境中的所有对象。 4. `install.packages('readxl')` 和 `library(readxl)`: 这些代码用于安装和加载名为 `readxl` 的R,该提供了读取Excel文件的函数。 5. `data <- read_excel("C:\\Users\\cora\\Desktop\\吉林省上市\\data1.xlsx")`: 这行代码读取名为 "data1.xlsx" 的Excel文件,并将其内容存储在名为 `data` 的变量中。 6. `X = read_excel("C:\\Users\\cora\\Desktop\\吉林省上市\\zbl.xlsx")`: 这行代码读取名为 "zbl.xlsx" 的Excel文件,并将其内容存储在名为 `X` 的变量中。 7. `x <- as.matrix(X)`: 这行代码将变量 `X` 转换为矩阵,并将结果存储在名为 `x` 的变量中。 8. `Y = read.csv("C:\\Users\\cora\\Desktop\\吉林省上市\\ybl.xlsx")`: 这行代码读取名为 "ybl.xlsx" 的CSV文件,并将其内容存储在名为 `Y` 的变量中。 9. `y <- as.matrix(Y)`: 这行代码将变量 `Y` 转换为矩阵,并将结果存储在名为 `y` 的变量中。 10. `X = scale(X, center = T, scale = T)`: 这行代码对变量 `X` 进行标准化处理,使其均值为0,标准差为1。 11. `colMeans(X)`: 这个函数计算矩阵 `X` 的每一列的平均值。 12. `apply(X, 2, sd)`: 这个函数对矩阵 `X` 的每一列应用 `sd` 函数,计算标准差。 13. `beta = c(0.15, -0.33, 0.25, -0.25, 0.05, rep(0, p/2-5), -0.25, 0.12, -0.125, rep(0, p/2-3))`: 这行代码定义了一个名为 `beta` 的向量,其中含了一系列数值。 14. `y = x %*% beta + rnorm(180, sd = 0.5)`: 这行代码使用矩阵乘法将矩阵 `x` 和向量 `beta` 相乘,并加上一个服从正态分布的随机噪声。 15. `y = scale(y)`: 这行代码对向量 `y` 进行标准化处理,使其均值为0,标准差为1。 16. `lambda <- 0.01`: 这行代码将变量 `lambda` 赋值为0.01。 17. `la.eq <- glmnet(x, y, intercept = F, alpha = 1)`: 这行代码使用 `glmnet` 函数进行Lasso回归分析,其中 `x` 是自变量矩阵,`y` 是因变量向量,`intercept = F` 表示不括截距项,`alpha = 1` 表示使用Lasso方法。 18. `plot(la.eq, xvar = "lambda", label = F, lwd = 2)`: 这个函数用于绘制Lasso回归模型的系数路径图。 19. `mod_cv <- cv.glmnet(x = x, y = y, intercept = F, alpha = 1)`: 这行代码使用 `cv.glmnet` 函数进行交叉验证,选择最佳的正则化参数。 20. `plot(mod_cv)`: 这个函数用于绘制交叉验证结果的图形。 21. `print(paste(mod_cv$lambda.min, log(mod_cv$lambda.min)))`: 这行代码打印最小的正则化参数及其对数值。 22. `print(paste(mod_cv$lambda.lse, log(mod_cv$lambda.lse)))`: 这行代码打印最小平方误差的正则化参数及其对数值。 23. `best_lambda <- mod_cv$lambda.min`: 这行代码将最佳正则化参数赋值给变量 `best_lambda`。 24. `best_model <- glmnet(X, y, alpha = 1, lambda = best_lambda)`: 这行代码使用最佳正则化参数进行Lasso回归模型的拟合。 25. `coef(best_model)`: 这个函数用于获取Lasso回归模型的系数估计值。 这段代码的目标是根据给定的数据进行线性回归分析,并使用Lasso方法进行特征选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值