n <- 10000000 p <- 10 x <- matrix(rnorm(n*p),ncol = p) beta <- matrix(c(1:p),ncol = 1) z <- x %*% beta condprob <- pnorm(z) y <- matrix(rbinom(n,size = 1,prob = condprob),ncol = 1) prob.fit <- glm.fit(x,y,family = binomial(link = "probit"))$coefficients logit.fit <- glm.fit(x,y,family = binomial(link = "logit"))$coefficients linear.fit <- glm.fit(x,y,family = gaussian(link = "identity"))$coefficients coef.mat <- cbind(prob.fit,logit.fit,linear.fit) print(coef.mat) prop.mat <- cbind(prob.fit/logit.fit,prob.fit/linear.fit,logit.fit/linear.fit) print(prop.mat)

时间: 2024-04-26 20:21:12 浏览: 10
这段代码用于生成一个包含n个观测值和p个预测变量的数据集,然后使用不同的广义线性模型(GLM)对y(响应变量)进行建模。具体来说,这里使用logit、probit和线性模型对y进行建模,并输出每个模型的系数矩阵和该矩阵中每个参数的比例矩阵。其中,logit和probit模型是二元响应变量的GLM,而线性模型是用于连续响应变量的GLM。
相关问题

用r语言写程序:Simulate a dataset containing continuous traits with given h^2. Split itinto training and testing datasets with proportion 4 ∶ 1.set.seed(20132014)n <- 5000p <- 1000h <- c(0.2, 0.8)[1]# simulate genotype (not exactly)x_r <- matrix(rnorm(n * p), ncol = p)xmean <- matrix(rep(colMeans(x_r), n), ncol=p, byrow = TRUE)xsd <- matrix(rep(apply(x_r, 2, sd), n), ncol=p, byrow = TRUE)x <- (x_r - xmean)/xsdPlease perform the marginal model.## [1] 526 776 50 557 801 345 298 559 178 214 • Show the locations of significant SNPs.• Replicate above figure, and highlight the significant coefficents in red.

Sure, here is the R code to simulate the dataset, split it into training and testing datasets, and perform the marginal model: ```R # Simulate dataset set.seed(20132014) n <- 5000 p <- 1000 h2 <- 0.2 x_r <- matrix(rnorm(n * p), ncol = p) xmean <- matrix(rep(colMeans(x_r), n), ncol=p, byrow = TRUE) xsd <- matrix(rep(apply(x_r, 2, sd), n), ncol=p, byrow = TRUE) x <- (x_r - xmean)/xsd beta <- rnorm(p, mean = 0, sd = sqrt(h2/p)) y <- x %*% beta + rnorm(n, mean = 0, sd = sqrt(1 - h2)) # Split into training and testing datasets train_idx <- sample(1:n, size = n/5*4, replace = FALSE) x_train <- x[train_idx, ] y_train <- y[train_idx] x_test <- x[-train_idx, ] y_test <- y[-train_idx] # Perform marginal model library(glmnet) fit <- glmnet(x_train, y_train, family = "gaussian") cvfit <- cv.glmnet(x_train, y_train, family = "gaussian") lambda_min <- cvfit$lambda.min beta_hat <- coef(fit, s = lambda_min)[-1, ] ``` To show the locations of significant SNPs, we can plot the absolute values of the estimated coefficients and highlight the significant ones in red: ```R # Plot coefficients library(ggplot2) library(dplyr) beta_df <- data.frame(beta_hat) %>% mutate(SNP = row_number()) %>% gather(key = "beta", value = "value", -SNP) %>% mutate(value = abs(value)) ggplot(beta_df, aes(x = SNP, y = value)) + geom_point() + scale_y_log10() + labs(title = "Estimated Coefficients", x = "SNP", y = "Absolute Value") + theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_blank(), axis.ticks.x = element_blank()) # Highlight significant coefficients significant_snps <- which(beta_hat != 0) significant_df <- beta_df %>% filter(SNP %in% significant_snps) %>% mutate(significant = TRUE) ggplot(beta_df, aes(x = SNP, y = value)) + geom_point() + geom_point(data = significant_df, aes(color = significant), size = 2) + scale_y_log10() + scale_color_manual(values = c("black", "red")) + labs(title = "Estimated Coefficients", x = "SNP", y = "Absolute Value") + theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_blank(), axis.ticks.x = element_blank()) ``` This should give you the desired plot with significant coefficients highlighted in red.

1 Simulate a dataset containing continuous traits with given ℎ 2 . Split it into training and testing datasets with proportion 4 ∶ 1. set.seed(20132014) n <- 5000 p <- 1000 h <- c(0.2, 0.8)[1] # simulate genotype (not exactly) x_r <- matrix(rnorm(n * p), ncol = p) xmean <- matrix(rep(colMeans(x_r), n), ncol=p, byrow = TRUE) xsd <- matrix(rep(apply(x_r, 2, sd), n), ncol=p, byrow = TRUE) x <- (x_r - xmean)/xsd # ... 2 4 Calculate polygenic scores with both the marginnal and joint models on testing data. And compare their performance under low and high ℎ

Sure, here's the code to calculate polygenic scores with both the marginal and joint models on the testing data: ```R # perform Lasso on training set fit_joint <- glmnet(x_train, y_train, alpha = 1) # calculate polygenic scores on testing set using joint model ps_joint <- x_test %*% fit_joint$beta[,1] # perform simple linear regression on training set fit_marginal <- lm(y_train ~ x_train) # calculate polygenic scores on testing set using marginal model ps_marginal <- x_test %*% coef(fit_marginal)[-1] # compare performance under low and high h^2 h_low <- c(0.2, 0.8)[1] h_high <- c(0.2, 0.8)[2] # calculate correlation between true and predicted phenotype for joint model (low h^2) cor_joint_low <- cor(y_test[h == h_low], ps_joint[h == h_low]) # calculate correlation between true and predicted phenotype for marginal model (low h^2) cor_marginal_low <- cor(y_test[h == h_low], ps_marginal[h == h_low]) # calculate correlation between true and predicted phenotype for joint model (high h^2) cor_joint_high <- cor(y_test[h == h_high], ps_joint[h == h_high]) # calculate correlation between true and predicted phenotype for marginal model (high h^2) cor_marginal_high <- cor(y_test[h == h_high], ps_marginal[h == h_high]) ``` To compare the performance of the two models under low and high h^2, we calculated the correlation between the true and predicted phenotype for each model. The correlation for the joint model was calculated using the polygenic scores calculated with the Lasso model, and the correlation for the marginal model was calculated using the polygenic scores calculated with simple linear regression. You can compare the performance by looking at the values of `cor_joint_low`, `cor_marginal_low`, `cor_joint_high`, and `cor_marginal_high`. The higher the correlation, the better the model's performance at predicting the phenotype. I hope this helps! Let me know if you have any further questions.

相关推荐

在运行以下R代码时:library(glmnet) library(ggplot2) # 生成5030的随机数据和30个变量 set.seed(1111) n <- 50 p <- 30 X <- matrix(runif(n * p), n, p) y <- rnorm(n) # 生成三组不同系数的线性模型 beta1 <- c(rep(1, 3), rep(0, p - 3)) beta2 <- c(rep(0, 10), rep(1, 3), rep(0, p - 13)) beta3 <- c(rep(0, 20), rep(1, 3), rep(0, p - 23)) y1 <- X %*% beta1 + rnorm(n) y2 <- X %*% beta2 + rnorm(n) y3 <- X %*% beta3 + rnorm(n) # 设置交叉验证折数 k <- 10 # 设置不同的lambda值 lambda_seq <- 10^seq(10, -2, length.out = 100) # 执行交叉验证和岭回归,并记录CV error和Prediction error cv_error <- list() pred_error <- list() for (i in 1:3) { # 交叉验证 cvfit <- cv.glmnet(X, switch(i, y1, y2, y3), alpha = 0, lambda = lambda_seq, nfolds = k) cv_error[[i]] <- cvfit$cvm # 岭回归 fit <- glmnet(X, switch(i, y1, y2, y3), alpha = 0, lambda = lambda_seq) pred_error[[i]] <- apply(X, 2, function(x) { x_mat <- matrix(x, nrow = n, ncol = p, byrow = TRUE) pred <- predict(fit, newx = x_mat) pred <- t(pred) # 转置 mean((x_mat %*% fit$beta - switch(i, y1, y2, y3))^2, na.rm = TRUE) # 修改此处 }) } # 绘制图形 par(mfrow = c(3, 2), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0)) for (i in 1:3) { # CV error plot cv_plot_data <- cv_error[[i]] plot(log10(lambda_seq), cv_plot_data, type = "l", xlab = expression(log10), ylab = "CV error", main = paste0("Model ", i)) abline(v = log10(cvfit$lambda.min), col = "red") # Prediction error plot pred_plot_data <- pred_error[[i]] plot(log10(lambda_seq), pred_plot_data, type = "l", xlab = expression(log10), ylab = "Prediction error", main = paste0("Model ", i)) abline(v = log10(lambda_seq[which.min(pred_plot_data)]), col = "red") }。发生了以下问题:Error in xy.coords(x, y, xlabel, ylabel, log) : 'x'和'y'的长度不一样。请对原代码进行修正

基于以下代码:# ①建立50×30的随机数据和30个变量 set.seed(123) X <- matrix(rnorm(50*30), ncol=30) y <- rnorm(50) # ②生成三组不同系数的线性模型 beta1 <- rnorm(30, mean=1, sd=0.5) beta2 <- rnorm(30, mean=2, sd=0.5) beta3 <- rnorm(30, mean=3, sd=0.5) # 定义一个函数用于计算线性回归的CV值 cv_linear <- function(X, y, k=10, lambda=NULL) { n <- nrow(X) if (is.null(lambda)) { lambda <- seq(0, 1, length.out=100) } mse <- rep(0, length(lambda)) folds <- sample(rep(1:k, length.out=n)) for (i in 1:k) { X_train <- X[folds!=i, ] y_train <- y[folds!=i] X_test <- X[folds==i, ] y_test <- y[folds==i] for (j in 1:length(lambda)) { fit <- glmnet(X_train, y_train, alpha=0, lambda=lambda[j]) y_pred <- predict(fit, newx=X_test) mse[j] <- mse[j] + mean((y_test - y_pred)^2) } } mse <- mse / k return(mse) } # ③(线性回归中)分别计算这三组的CV值 lambda <- seq(0, 1, length.out=100) mse1 <- cv_linear(X, y, lambda=lambda) mse2 <- cv_linear(X, y, lambda=lambda) mse3 <- cv_linear(X, y, lambda=lambda) # ④(岭回归中)分别画出这三组的两张图,两张图均以lambd为横坐标,一张图以CV error为纵坐标,一张图以Prediction error为纵坐标,两张图同分开在Plots位置 library(glmnet) par(mfrow=c(1,2)) # 画CV error图 plot(lambda, mse1, type="l", xlab="lambda", ylab="CV error", main="Beta1") points(lambda, mse2, type="l", col="red") points(lambda, mse3, type="l", col="blue") # 画Prediction error图 fit1 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse1)]) fit2 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse2)]) fit3 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse3)]) y_pred1 <- predict(fit1, newx=X) y_pred2 <- predict(fit2, newx=X) y_pred3 <- predict(fit3, newx=X) pred_error1 <- mean((y - y_pred1)^2) pred_error2 <- mean((y - y_pred2)^2) pred_error3 <- mean((y - y_pred3)^2) plot(lambda, pred_error1, type="l", xlab="lambda", ylab="Prediction error", main="Beta1") points(lambda, pred_error2, type="l", col="red") points(lambda, pred_error3, type="l", col="blue")。按以下要求修改R代码:将三组的分别以CV error和Prediction error为纵坐标的图,每次Plots位置只会出现同一个组的两张分别以CV error和Prediction error为纵坐标的图

基于以下R代码:library(glmnet) library(ggplot2) # 生成5030的随机数据和30个变量 set.seed(1111) n <- 50 p <- 30 X <- matrix(runif(n * p), n, p) y <- rnorm(n) # 生成三组不同系数的线性模型 beta1 <- c(rep(1, 3), rep(0, p - 3)) beta2 <- c(rep(0, 10), rep(1, 3), rep(0, p - 13)) beta3 <- c(rep(0, 20), rep(1, 3), rep(0, p - 23)) y1 <- X %% beta1 + rnorm(n) y2 <- X %% beta2 + rnorm(n) y3 <- X %% beta3 + rnorm(n) # 设置交叉验证折数 k <- 10 # 设置不同的lambda值 lambda_seq <- 10^seq(10, -2, length.out = 100) # 执行交叉验证和岭回归,并记录CV error和Prediction error cv_error <- list() pred_error <- list() for (i in 1:3) { # 交叉验证 cvfit <- cv.glmnet(X, switch(i, y1, y2, y3), alpha = 0, lambda = lambda_seq, nfolds = k) cv_error[[i]] <- cvfit$cvm # 岭回归 fit <- glmnet(X, switch(i, y1, y2, y3), alpha = 0, lambda = lambda_seq) pred_error[[i]] <- apply(X, 2, function(x) { x_mat <- matrix(x, nrow = n, ncol = p, byrow = TRUE) pred <- predict(fit, newx = x_mat) pred <- t(pred) mean((x_mat %% fit$beta - switch(i, y1, y2, y3))^2) }) } # 绘制图形 par(mfrow = c(3, 2), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0)) for (i in 1:3) { # CV error plot cv_plot_data <- cv_error[[i]] plot(log10(lambda_seq), cv_plot_data, type = "l", xlab = expression(lambda), ylab = "CV error", main = paste0("Model ", i)) abline(v = log10(cvfit$lambda.min), col = "red") # Prediction error plot pred_plot_data <- pred_error[[i]] plot(log10(lambda_seq[1:fit$df]), pred_plot_data[1:fit$df], type = "l", xlab = expression(lambda), ylab = "Prediction error", main = paste0("Model ", i)) abline(v = log10(lambda_seq[which.min(pred_plot_data)]), col = "red") },给出以下问题的代码:基于一倍标准差准则给出参数值上限

请在以下R代码基础上:# ①建立50×30的随机数据和30个变量 set.seed(123) X <- matrix(rnorm(50*30), ncol=30) y <- rnorm(50) # ②生成三组不同系数的线性模型 beta1 <- rnorm(30, mean=1, sd=0.5) beta2 <- rnorm(30, mean=2, sd=0.5) beta3 <- rnorm(30, mean=3, sd=0.5) # 定义一个函数用于计算线性回归的CV值 cv_linear <- function(X, y, k=10, lambda=NULL) { n <- nrow(X) if (is.null(lambda)) { lambda <- seq(0, 1, length.out=100) } mse <- rep(0, length(lambda)) folds <- sample(rep(1:k, length.out=n)) for (i in 1:k) { X_train <- X[folds!=i, ] y_train <- y[folds!=i] X_test <- X[folds==i, ] y_test <- y[folds==i] for (j in 1:length(lambda)) { fit <- glmnet(X_train, y_train, alpha=0, lambda=lambda[j]) y_pred <- predict(fit, newx=X_test) mse[j] <- mse[j] + mean((y_test - y_pred)^2) } } mse <- mse / k return(mse) } # ③(线性回归中)分别计算这三组的CV值 lambda <- seq(0, 1, length.out=100) mse1 <- cv_linear(X, y, lambda=lambda) mse2 <- cv_linear(X, y, lambda=lambda) mse3 <- cv_linear(X, y, lambda=lambda) # ④(岭回归中)分别画出这三组的两张图,每组两张图均以lambda为横坐标: library(glmnet) par(mfrow=c(2,3)) # 画Beta1的CV error图 plot(lambda, mse1, type="l", xlab="lambda", ylab="CV error", main="Beta1 CV error") # 画Beta1的Prediction error图 fit1 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse1)]) y_pred1 <- as.vector(predict(fit1, newx=X)) pred_error1 <- mean((y - y_pred1)^2) lambda <- as.vector(lambda) pred_error1 <- as.vector(pred_error1) if (length(lambda) != length(pred_error1)) { if (length(lambda) > length(pred_error1)) { pred_error1 <- rep(pred_error1, length.out = length(lambda)) } else { lambda <- rep(lambda, length.out = length(pred_error1)) } } plot(lambda, pred_error1, type="l", xlab="lambda", ylab="Prediction error", main="Beta1 Prediction error") # 画Beta2的CV error图 plot(lambda, mse2, type="l", xlab="lambda", ylab="CV error", main="Beta2 CV error") # 画Beta2的Prediction error图 fit2 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse2)]) y_pred2 <- predict(fit2, newx=X) pred_error2 <- mean((y - y_pred2)^2) plot(lambda, pred_error2, type="l", xlab="lambda", ylab="Prediction error", main="Beta2 Prediction error") # 画Beta3的CV error图 plot(lambda, mse3, type="l", xlab="lambda", ylab="CV error", main="Beta3 CV error") # 画Beta3的Prediction error图 fit3 <- glmnet(X, y, alpha=0, lambda=lambda[which.min(mse3)]) y_pred3 <- predict(fit3, newx=X) pred_error3 <- mean((y - y_pred3)^2) plot(lambda, pred_error3, type="l", xlab="lambda", ylab="Prediction error", main="Beta3 Prediction error")。对每组数据绘制纵坐标为Prediction error的图的代码进行修改

最新推荐

recommend-type

###对华为OD分布式操作系统的详细介绍

华为OD
recommend-type

2110220116吴骏博.py

2110220116吴骏博.py
recommend-type

基于Java的ApplicationPower快速项目生成脚手架设计源码

ApplicationPower项目生成脚手架设计源码:该项目基于Java开发,包含284个文件,主要使用Java和Shell语言。ApplicationPower是一个快速的项目生成脚手架,旨在帮助开发者快速搭建项目框架,包括创建项目结构、配置文件、开发环境等,提高开发效率。
recommend-type

基于MATLAB实现的OFDM经典同步算法之一Park算法仿真,附带Park算法经典文献+代码文档+使用说明文档.rar

CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的OFDM经典同步算法之一Park算法仿真,附带Park算法经典文献+代码文档+使用说明文档.rar 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于MATLAB实现的imu和视觉里程计 kalman滤波器 进行融合+使用说明文档.rar

CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的imu和视觉里程计 kalman滤波器 进行融合+使用说明文档.rar 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。