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 22:21:15 浏览: 5
这段代码用于生成一个包含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

钢桁架结构振动特性渐变分析工具

钢桁架结构振动特性渐变分析工具
recommend-type

数据库实战-收集一些常见的 MySQL 死锁案例.zip

数据库实战-收集一些常见的 MySQL 死锁案例.zip 数据库实战-收集一些常见的 MySQL 死锁案例.zip 在工作过程中偶尔会遇到死锁问题,虽然这种问题遇到的概率不大,但每次遇到的时候要想彻底弄懂其原理并找到解决方案却并不容易。这个项目收集了一些常见的 MySQL 死锁案例,大多数案例都来源于网络,并对其进行分类汇总,试图通过死锁日志分析出每种死锁的原因,还原出死锁现场。 实际上,我们在定位死锁问题时,不仅应该对死锁日志进行分析,还应该结合具体的业务代码,或者根据 binlog,理出每个事务执行的 SQL 语句。
recommend-type

Android的移动应用与php服务器交互实例源码.rar

Android的移动应用与php服务器交互实例源码.rar
recommend-type

【滤波跟踪】基于matlab平方根容积卡尔曼滤波机器人手臂运动跟踪【含Matlab源码 4540期】.mp4

Matlab研究室上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描视频QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
recommend-type

计算BMI等一些关于热量和蛋白质摄入的小工具.zip

蛋白质是生物体中普遍存在的一类重要生物大分子,由天然氨基酸通过肽键连接而成。它具有复杂的分子结构和特定的生物功能,是表达生物遗传性状的一类主要物质。 蛋白质的结构可分为四级:一级结构是组成蛋白质多肽链的线性氨基酸序列;二级结构是依靠不同氨基酸之间的C=O和N-H基团间的氢键形成的稳定结构,主要为α螺旋和β折叠;三级结构是通过多个二级结构元素在三维空间的排列所形成的一个蛋白质分子的三维结构;四级结构用于描述由不同多肽链(亚基)间相互作用形成具有功能的蛋白质复合物分子。 蛋白质在生物体内具有多种功能,包括提供能量、维持电解质平衡、信息交流、构成人的身体以及免疫等。例如,蛋白质分解可以为人体提供能量,每克蛋白质能产生4千卡的热能;血液里的蛋白质能帮助维持体内的酸碱平衡和血液的渗透压;蛋白质是组成人体器官组织的重要物质,可以修复受损的器官功能,以及维持细胞的生长和更新;蛋白质也是构成多种生理活性的物质,如免疫球蛋白,具有维持机体正常免疫功能的作用。 蛋白质的合成是指生物按照从脱氧核糖核酸(DNA)转录得到的信使核糖核酸(mRNA)上的遗传信息合成蛋白质的过程。这个过程包括氨基酸的活化、多肽链合成的起始、肽链的延长、肽链的终止和释放以及蛋白质合成后的加工修饰等步骤。 蛋白质降解是指食物中的蛋白质经过蛋白质降解酶的作用降解为多肽和氨基酸然后被人体吸收的过程。这个过程在细胞的生理活动中发挥着极其重要的作用,例如将蛋白质降解后成为小分子的氨基酸,并被循环利用;处理错误折叠的蛋白质以及多余组分,使之降解,以防机体产生错误应答。 总的来说,蛋白质是生物体内不可或缺的一类重要物质,对于维持生物体的正常生理功能具有至关重要的作用。
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

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