Creating models for each group in dataframe column in R












0














I would like to create a naiveBayes model per group of values of column TN.
Here is the code:



library(party)
airq <- subset(airquality, !is.na(Ozone))
ct <- ctree(Ozone ~ ., data = airq, controls = ctree_control(maxsurrogate = 3))
airq$TN<-factor (ct@where)


Here is an example:



> tail (airq)
Ozone Solar.R Wind Temp Month Day TN
1 7 49 10.3 69 9 24 2
2 14 20 16.6 63 9 25 5
3 30 193 6.9 70 9 26 3
4 14 191 14.3 75 9 28 2
5 18 131 8.0 76 9 29 5
6 20 223 11.5 68 9 30 3


I want to build a NaiveBayes model per value:



model[i] <- naiveBayes(Ozone ~ ., data = dat[i])


dat[1] will contain lines 1 and 4 (TN=2) for creating first model,dat[2] contains lines 2 and 5 for (TN=5) for the second model, dat[3] contains lines 3 and for third model (TN=3).
So finally, I'll be able to sum it up: for each unique group values in TN I'll have its model.
Like this:



    TN_val  model     Based on Source_Lines
1 2 model[1] 1,4
2 5 model[2] 2,5
3 3 model[3] 3,6









share|improve this question


















  • 1




    lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x))
    – LAP
    Nov 23 at 6:41












  • Hi @LAP, Thanks. How can I get the relevant value of TN per the model?
    – Avi
    Nov 23 at 7:08






  • 1




    The name of the list element will be equal to the value of the TN for each model in the list. So if you'd use mymodels <- lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x)), names(mymodels)[1] would display the TN value of the first model.
    – LAP
    Nov 23 at 7:16












  • Thanks again @LAP. One more question: I have an additional of airq dataframe (for testing). I would like to run (predict) the mymodels on the new data per line per TN value. How can I do it?
    – Avi
    Nov 23 at 7:33
















0














I would like to create a naiveBayes model per group of values of column TN.
Here is the code:



library(party)
airq <- subset(airquality, !is.na(Ozone))
ct <- ctree(Ozone ~ ., data = airq, controls = ctree_control(maxsurrogate = 3))
airq$TN<-factor (ct@where)


Here is an example:



> tail (airq)
Ozone Solar.R Wind Temp Month Day TN
1 7 49 10.3 69 9 24 2
2 14 20 16.6 63 9 25 5
3 30 193 6.9 70 9 26 3
4 14 191 14.3 75 9 28 2
5 18 131 8.0 76 9 29 5
6 20 223 11.5 68 9 30 3


I want to build a NaiveBayes model per value:



model[i] <- naiveBayes(Ozone ~ ., data = dat[i])


dat[1] will contain lines 1 and 4 (TN=2) for creating first model,dat[2] contains lines 2 and 5 for (TN=5) for the second model, dat[3] contains lines 3 and for third model (TN=3).
So finally, I'll be able to sum it up: for each unique group values in TN I'll have its model.
Like this:



    TN_val  model     Based on Source_Lines
1 2 model[1] 1,4
2 5 model[2] 2,5
3 3 model[3] 3,6









share|improve this question


















  • 1




    lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x))
    – LAP
    Nov 23 at 6:41












  • Hi @LAP, Thanks. How can I get the relevant value of TN per the model?
    – Avi
    Nov 23 at 7:08






  • 1




    The name of the list element will be equal to the value of the TN for each model in the list. So if you'd use mymodels <- lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x)), names(mymodels)[1] would display the TN value of the first model.
    – LAP
    Nov 23 at 7:16












  • Thanks again @LAP. One more question: I have an additional of airq dataframe (for testing). I would like to run (predict) the mymodels on the new data per line per TN value. How can I do it?
    – Avi
    Nov 23 at 7:33














0












0








0


1





I would like to create a naiveBayes model per group of values of column TN.
Here is the code:



library(party)
airq <- subset(airquality, !is.na(Ozone))
ct <- ctree(Ozone ~ ., data = airq, controls = ctree_control(maxsurrogate = 3))
airq$TN<-factor (ct@where)


Here is an example:



> tail (airq)
Ozone Solar.R Wind Temp Month Day TN
1 7 49 10.3 69 9 24 2
2 14 20 16.6 63 9 25 5
3 30 193 6.9 70 9 26 3
4 14 191 14.3 75 9 28 2
5 18 131 8.0 76 9 29 5
6 20 223 11.5 68 9 30 3


I want to build a NaiveBayes model per value:



model[i] <- naiveBayes(Ozone ~ ., data = dat[i])


dat[1] will contain lines 1 and 4 (TN=2) for creating first model,dat[2] contains lines 2 and 5 for (TN=5) for the second model, dat[3] contains lines 3 and for third model (TN=3).
So finally, I'll be able to sum it up: for each unique group values in TN I'll have its model.
Like this:



    TN_val  model     Based on Source_Lines
1 2 model[1] 1,4
2 5 model[2] 2,5
3 3 model[3] 3,6









share|improve this question













I would like to create a naiveBayes model per group of values of column TN.
Here is the code:



library(party)
airq <- subset(airquality, !is.na(Ozone))
ct <- ctree(Ozone ~ ., data = airq, controls = ctree_control(maxsurrogate = 3))
airq$TN<-factor (ct@where)


Here is an example:



> tail (airq)
Ozone Solar.R Wind Temp Month Day TN
1 7 49 10.3 69 9 24 2
2 14 20 16.6 63 9 25 5
3 30 193 6.9 70 9 26 3
4 14 191 14.3 75 9 28 2
5 18 131 8.0 76 9 29 5
6 20 223 11.5 68 9 30 3


I want to build a NaiveBayes model per value:



model[i] <- naiveBayes(Ozone ~ ., data = dat[i])


dat[1] will contain lines 1 and 4 (TN=2) for creating first model,dat[2] contains lines 2 and 5 for (TN=5) for the second model, dat[3] contains lines 3 and for third model (TN=3).
So finally, I'll be able to sum it up: for each unique group values in TN I'll have its model.
Like this:



    TN_val  model     Based on Source_Lines
1 2 model[1] 1,4
2 5 model[2] 2,5
3 3 model[3] 3,6






r dataframe grouping naivebayes party






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 6:36









Avi

9971529




9971529








  • 1




    lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x))
    – LAP
    Nov 23 at 6:41












  • Hi @LAP, Thanks. How can I get the relevant value of TN per the model?
    – Avi
    Nov 23 at 7:08






  • 1




    The name of the list element will be equal to the value of the TN for each model in the list. So if you'd use mymodels <- lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x)), names(mymodels)[1] would display the TN value of the first model.
    – LAP
    Nov 23 at 7:16












  • Thanks again @LAP. One more question: I have an additional of airq dataframe (for testing). I would like to run (predict) the mymodels on the new data per line per TN value. How can I do it?
    – Avi
    Nov 23 at 7:33














  • 1




    lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x))
    – LAP
    Nov 23 at 6:41












  • Hi @LAP, Thanks. How can I get the relevant value of TN per the model?
    – Avi
    Nov 23 at 7:08






  • 1




    The name of the list element will be equal to the value of the TN for each model in the list. So if you'd use mymodels <- lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x)), names(mymodels)[1] would display the TN value of the first model.
    – LAP
    Nov 23 at 7:16












  • Thanks again @LAP. One more question: I have an additional of airq dataframe (for testing). I would like to run (predict) the mymodels on the new data per line per TN value. How can I do it?
    – Avi
    Nov 23 at 7:33








1




1




lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x))
– LAP
Nov 23 at 6:41






lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x))
– LAP
Nov 23 at 6:41














Hi @LAP, Thanks. How can I get the relevant value of TN per the model?
– Avi
Nov 23 at 7:08




Hi @LAP, Thanks. How can I get the relevant value of TN per the model?
– Avi
Nov 23 at 7:08




1




1




The name of the list element will be equal to the value of the TN for each model in the list. So if you'd use mymodels <- lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x)), names(mymodels)[1] would display the TN value of the first model.
– LAP
Nov 23 at 7:16






The name of the list element will be equal to the value of the TN for each model in the list. So if you'd use mymodels <- lapply(split(airq, airq$TN), function(x) naiveBayes(Ozone ~ ., data = x)), names(mymodels)[1] would display the TN value of the first model.
– LAP
Nov 23 at 7:16














Thanks again @LAP. One more question: I have an additional of airq dataframe (for testing). I would like to run (predict) the mymodels on the new data per line per TN value. How can I do it?
– Avi
Nov 23 at 7:33




Thanks again @LAP. One more question: I have an additional of airq dataframe (for testing). I would like to run (predict) the mymodels on the new data per line per TN value. How can I do it?
– Avi
Nov 23 at 7:33

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441676%2fcreating-models-for-each-group-in-dataframe-column-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441676%2fcreating-models-for-each-group-in-dataframe-column-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)