如何使用功能预测GLM以经过多年数据组合?
对于背景,我几周前问了这个问题:如何创建一个用于循环的循环以通过r?的GLM多年
组合7年的数据,并试图使用1年的数据,2年的数据(七年组合),3年的数据(每一个组合的七年组合)等创建逻辑回归GLM,等等,直到7年数据。
用户@Parfait帮助我创建了代码的基础,以完成每年的每年组合(总计7年)并找到模型的偏差等。我现在想查看不同的指标,而不是AIC,偏差等。 具体来说,我想使用测试和培训数据,并进行预测过程和混乱矩阵以获得总体精度值。
这是一些示例数据:
Blue_allyears <- data.frame(
Survey_Yea = sample(2005:2014, 500, replace=TRUE),
Pres_Abs = sample(0:1, 500, replace=TRUE),
TestData = sample(0:1, 500, replace=TRUE),
ca_10mbath = runif(500),
ca_10m_cur = runif(500),
ca_10m_eas = runif(500),
ca10_bpi30 = runif(500),
ca10_bpi24 = runif(500)
)
Blue_allyears
这是我一直试图调整的代码。 设置函数:
run_model <- function(vec, yr) {
# subset data by years
sub <-blue_test_train[blue_test_train$Survey_Yea %in% vec,]
# dynamically generate formula
fmla <- Pres_Abs~ca_10mbath+ca_10m_cur+ca_10m_eas+ca10_bpi30+ca10_bpi24
# fit glm model
fit<-glm(fmla,data=sub[sub$TestData=="0",],family=binomial(link=logit))
#get predictions
trainpredict <- predict(fit, newdata=sub[sub$TestData=="1",], type="response")
# confusion matrix
cm<- confusionMatrix( trainpredict,reference=sub$Pres_Abs[sub$TestData=="1",])
overall.accuracy <- cm$overall['Accuracy']
# create temporary data frame
df <- data.frame(
Survey_Yea = paste(vec, collapse=", "),
overall.accuracy=overall.accuracy,
stringsAsFactors = F)
return(df)
}
运行函数:
years <- sort(unique(blue_test_train$Survey_Yea))
# RETURN NESTED LIST OF MANY DATA FRAMES
results_df_list <- lapply(1:7, function(i) combn(
years, i, run_model, simplify=FALSE, yr=i)
)
# RETURN FLATTENED LIST OF DATA FRAMES AND
# RENAME ELEMENTS
results_df_list <- setNames(
lapply(results_df_list, function(dfs) do.call(rbind, dfs)),
c("years_1", "years_2", "years_3", "years_4","years_5","years_6","years_7")
)
# REVIEW EMBEDDED DATA FRAMES
b1<-(results_df_list$years_1)
b2<-(results_df_list$years_2)
b3<-(results_df_list$years_3)
b4<-(results_df_list$years_4)
b5<-(results_df_list$years_5)
b6<-(results_df_list$years_6)
b7<-(results_df_list$years_7)
blue_inyears<-rbind(b1,b2,b3,b4,b5,b6,b7)
blue_inyears
这是当前错误代码I et: 中的错误[。默认
(sub $ pres_abs,sub $ testdata ==“ 1”,): 不正确的尺寸数量
我也尝试将Sub Sub Sub Sub Sub Sub Sub 2分别仅使用训练和测试数据,以及用于混淆矩阵的不同方法。我也遇到了某种类型的错误。
任何帮助都非常感谢。
谢谢你!
For background, I asked this questions a couple of weeks ago: How to create a for loop to go through multiple year combinations for a glm in R?
In summary, I have 7 years of data and am trying to create logistic regression glms using 1 year of data, 2 years of data (every combination of the seven years), 3 years of data (every combination of the seven years), etc. until 7 years of data.
User @Parfait helped me a ton in creating the basis of a code to go through every combination of years (7 years total) and to find the deviance, etc. of the model. I would now like to look at different metrics rather than AIC, deviance, etc.
Specifically, I would like to use testing and training data and go through the process of prediction and a confusion matrix to get an overall accuracy value.
Here is some example data:
Blue_allyears <- data.frame(
Survey_Yea = sample(2005:2014, 500, replace=TRUE),
Pres_Abs = sample(0:1, 500, replace=TRUE),
TestData = sample(0:1, 500, replace=TRUE),
ca_10mbath = runif(500),
ca_10m_cur = runif(500),
ca_10m_eas = runif(500),
ca10_bpi30 = runif(500),
ca10_bpi24 = runif(500)
)
Blue_allyears
And here is the code I have been trying to adapt.
Setting up the function:
run_model <- function(vec, yr) {
# subset data by years
sub <-blue_test_train[blue_test_train$Survey_Yea %in% vec,]
# dynamically generate formula
fmla <- Pres_Abs~ca_10mbath+ca_10m_cur+ca_10m_eas+ca10_bpi30+ca10_bpi24
# fit glm model
fit<-glm(fmla,data=sub[sub$TestData=="0",],family=binomial(link=logit))
#get predictions
trainpredict <- predict(fit, newdata=sub[sub$TestData=="1",], type="response")
# confusion matrix
cm<- confusionMatrix( trainpredict,reference=sub$Pres_Abs[sub$TestData=="1",])
overall.accuracy <- cm$overall['Accuracy']
# create temporary data frame
df <- data.frame(
Survey_Yea = paste(vec, collapse=", "),
overall.accuracy=overall.accuracy,
stringsAsFactors = F)
return(df)
}
running the function:
years <- sort(unique(blue_test_train$Survey_Yea))
# RETURN NESTED LIST OF MANY DATA FRAMES
results_df_list <- lapply(1:7, function(i) combn(
years, i, run_model, simplify=FALSE, yr=i)
)
# RETURN FLATTENED LIST OF DATA FRAMES AND
# RENAME ELEMENTS
results_df_list <- setNames(
lapply(results_df_list, function(dfs) do.call(rbind, dfs)),
c("years_1", "years_2", "years_3", "years_4","years_5","years_6","years_7")
)
# REVIEW EMBEDDED DATA FRAMES
b1<-(results_df_list$years_1)
b2<-(results_df_list$years_2)
b3<-(results_df_list$years_3)
b4<-(results_df_list$years_4)
b5<-(results_df_list$years_5)
b6<-(results_df_list$years_6)
b7<-(results_df_list$years_7)
blue_inyears<-rbind(b1,b2,b3,b4,b5,b6,b7)
blue_inyears
Here is the current error code I et:
Error in [.default
(sub$Pres_Abs, sub$TestData == "1", ) :
incorrect number of dimensions
I've also tried subsetting sub into sub2 and sub3 with just the training and testing data, respectively, as well as using different methods for the confusion matrix. I've also gotten some type of error.
Any help is much appreciated.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我已经弄清楚了...这是更新的代码:
我从一开始就将数据征服了,这有所帮助。我正在努力确定“最佳”阈值,而不仅仅是使用0.5,但是想我会共享代码,因为它现在可以使用!
Alrighty I think I figured it out... here is the updated code:
I subsetted my data from the beginning which helped. I'm playing around with determining the "optimal" threshold rather than just using 0.5, but figured I'd share the code since it works now!