Writing a function to enclose another function with regression models [duplicate]












0
















This question already has an answer here:




  • Novice needs to loop lm in R

    1 answer



  • R Loop for Variable Names to run linear regression model

    2 answers




Goal: Run three regression models with three different outcome variables, as seen below, but ideally in a more efficient way than seen in the model1, model2, model3 version seen in the last three lines.



Specific question: How can I write a function that iterates over the set of dv's and creates model + # indicator as an object (e.g. model1, model2, etc.) AND switches the dv (e.g. dv1, dv2, etc...)? I assume there is a forloop and function solution to this but I am not getting it...



mydf <- data.frame(dv1 = rnorm(100),
dv2 = rnorm(100),
dv3 = rnorm(100),
iv1 = rnorm(100),
iv2 = rnorm(100),
iv3 = rnorm(100))

mymodel <- function(dv, df) {
lm(dv ~ iv1 + iv2 + iv3, data = df)
}

model1 <- mymodel(dv = mydf$dv1, df = mydf)
model2 <- mymodel(dv = mydf$dv2, df = mydf)
model3 <- mymodel(dv = mydf$dv3, df = mydf)









share|improve this question













marked as duplicate by Jaap r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 6:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Possibly helpful: r-bloggers.com/…

    – MrFlick
    Nov 26 '18 at 21:13
















0
















This question already has an answer here:




  • Novice needs to loop lm in R

    1 answer



  • R Loop for Variable Names to run linear regression model

    2 answers




Goal: Run three regression models with three different outcome variables, as seen below, but ideally in a more efficient way than seen in the model1, model2, model3 version seen in the last three lines.



Specific question: How can I write a function that iterates over the set of dv's and creates model + # indicator as an object (e.g. model1, model2, etc.) AND switches the dv (e.g. dv1, dv2, etc...)? I assume there is a forloop and function solution to this but I am not getting it...



mydf <- data.frame(dv1 = rnorm(100),
dv2 = rnorm(100),
dv3 = rnorm(100),
iv1 = rnorm(100),
iv2 = rnorm(100),
iv3 = rnorm(100))

mymodel <- function(dv, df) {
lm(dv ~ iv1 + iv2 + iv3, data = df)
}

model1 <- mymodel(dv = mydf$dv1, df = mydf)
model2 <- mymodel(dv = mydf$dv2, df = mydf)
model3 <- mymodel(dv = mydf$dv3, df = mydf)









share|improve this question













marked as duplicate by Jaap r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 6:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Possibly helpful: r-bloggers.com/…

    – MrFlick
    Nov 26 '18 at 21:13














0












0








0









This question already has an answer here:




  • Novice needs to loop lm in R

    1 answer



  • R Loop for Variable Names to run linear regression model

    2 answers




Goal: Run three regression models with three different outcome variables, as seen below, but ideally in a more efficient way than seen in the model1, model2, model3 version seen in the last three lines.



Specific question: How can I write a function that iterates over the set of dv's and creates model + # indicator as an object (e.g. model1, model2, etc.) AND switches the dv (e.g. dv1, dv2, etc...)? I assume there is a forloop and function solution to this but I am not getting it...



mydf <- data.frame(dv1 = rnorm(100),
dv2 = rnorm(100),
dv3 = rnorm(100),
iv1 = rnorm(100),
iv2 = rnorm(100),
iv3 = rnorm(100))

mymodel <- function(dv, df) {
lm(dv ~ iv1 + iv2 + iv3, data = df)
}

model1 <- mymodel(dv = mydf$dv1, df = mydf)
model2 <- mymodel(dv = mydf$dv2, df = mydf)
model3 <- mymodel(dv = mydf$dv3, df = mydf)









share|improve this question















This question already has an answer here:




  • Novice needs to loop lm in R

    1 answer



  • R Loop for Variable Names to run linear regression model

    2 answers




Goal: Run three regression models with three different outcome variables, as seen below, but ideally in a more efficient way than seen in the model1, model2, model3 version seen in the last three lines.



Specific question: How can I write a function that iterates over the set of dv's and creates model + # indicator as an object (e.g. model1, model2, etc.) AND switches the dv (e.g. dv1, dv2, etc...)? I assume there is a forloop and function solution to this but I am not getting it...



mydf <- data.frame(dv1 = rnorm(100),
dv2 = rnorm(100),
dv3 = rnorm(100),
iv1 = rnorm(100),
iv2 = rnorm(100),
iv3 = rnorm(100))

mymodel <- function(dv, df) {
lm(dv ~ iv1 + iv2 + iv3, data = df)
}

model1 <- mymodel(dv = mydf$dv1, df = mydf)
model2 <- mymodel(dv = mydf$dv2, df = mydf)
model3 <- mymodel(dv = mydf$dv3, df = mydf)




This question already has an answer here:




  • Novice needs to loop lm in R

    1 answer



  • R Loop for Variable Names to run linear regression model

    2 answers








r function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 20:51









tall_tabletall_table

937




937




marked as duplicate by Jaap r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 6:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Jaap r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 6:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Possibly helpful: r-bloggers.com/…

    – MrFlick
    Nov 26 '18 at 21:13



















  • Possibly helpful: r-bloggers.com/…

    – MrFlick
    Nov 26 '18 at 21:13

















Possibly helpful: r-bloggers.com/…

– MrFlick
Nov 26 '18 at 21:13





Possibly helpful: r-bloggers.com/…

– MrFlick
Nov 26 '18 at 21:13












2 Answers
2






active

oldest

votes


















1














Here's another approach using the tidyverse packages, since dplyr has more or less supplanted plyr.





library(tidyverse)

mydf <- data.frame(dv1 = rnorm(100),
dv2 = rnorm(100),
dv3 = rnorm(100),
iv1 = rnorm(100),
iv2 = rnorm(100),
iv3 = rnorm(100))

mymodel <- function(df) {
lm(value ~ iv1 + iv2 + iv3, data = df)
}

mydf %>%
gather("variable","value", contains("dv")) %>%
split(.$variable) %>%
map(mymodel)

#> $dv1
#>
#> Call:
#> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
#>
#> Coefficients:
#> (Intercept) iv1 iv2 iv3
#> -0.04516 -0.04657 0.08045 0.02518
#>
#>
#> $dv2
#>
#> Call:
#> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
#>
#> Coefficients:
#> (Intercept) iv1 iv2 iv3
#> -0.03906 0.16730 0.10324 0.02500
#>
#>
#> $dv3
#>
#> Call:
#> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
#>
#> Coefficients:
#> (Intercept) iv1 iv2 iv3
#> 0.018492 -0.162563 0.002738 0.179366


Created on 2018-11-26 by the reprex package (v0.2.1)






share|improve this answer































    0














    You could convert your data.frame to long form, with all the dv values in one column and then use plyr's dlply to create the lms. This splits the data.frame on the specified column ("dvN") and applys the function to each and returns a list of lms. I have changed the function slightly to make it just take a data.frame, not the column separately.



    Hope this gives what you need.



    library(plyr)
    library(tidyr)

    mydf_l <- gather(mydf, dvN, Value, 1:3)

    mymodel2 <- function(df) {
    lm(Value ~ iv1 + iv2 + iv3, data = df)
    }

    allmodels <- dlply(mydf_l, .(dvN), mymodel2)





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Here's another approach using the tidyverse packages, since dplyr has more or less supplanted plyr.





      library(tidyverse)

      mydf <- data.frame(dv1 = rnorm(100),
      dv2 = rnorm(100),
      dv3 = rnorm(100),
      iv1 = rnorm(100),
      iv2 = rnorm(100),
      iv3 = rnorm(100))

      mymodel <- function(df) {
      lm(value ~ iv1 + iv2 + iv3, data = df)
      }

      mydf %>%
      gather("variable","value", contains("dv")) %>%
      split(.$variable) %>%
      map(mymodel)

      #> $dv1
      #>
      #> Call:
      #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
      #>
      #> Coefficients:
      #> (Intercept) iv1 iv2 iv3
      #> -0.04516 -0.04657 0.08045 0.02518
      #>
      #>
      #> $dv2
      #>
      #> Call:
      #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
      #>
      #> Coefficients:
      #> (Intercept) iv1 iv2 iv3
      #> -0.03906 0.16730 0.10324 0.02500
      #>
      #>
      #> $dv3
      #>
      #> Call:
      #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
      #>
      #> Coefficients:
      #> (Intercept) iv1 iv2 iv3
      #> 0.018492 -0.162563 0.002738 0.179366


      Created on 2018-11-26 by the reprex package (v0.2.1)






      share|improve this answer




























        1














        Here's another approach using the tidyverse packages, since dplyr has more or less supplanted plyr.





        library(tidyverse)

        mydf <- data.frame(dv1 = rnorm(100),
        dv2 = rnorm(100),
        dv3 = rnorm(100),
        iv1 = rnorm(100),
        iv2 = rnorm(100),
        iv3 = rnorm(100))

        mymodel <- function(df) {
        lm(value ~ iv1 + iv2 + iv3, data = df)
        }

        mydf %>%
        gather("variable","value", contains("dv")) %>%
        split(.$variable) %>%
        map(mymodel)

        #> $dv1
        #>
        #> Call:
        #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
        #>
        #> Coefficients:
        #> (Intercept) iv1 iv2 iv3
        #> -0.04516 -0.04657 0.08045 0.02518
        #>
        #>
        #> $dv2
        #>
        #> Call:
        #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
        #>
        #> Coefficients:
        #> (Intercept) iv1 iv2 iv3
        #> -0.03906 0.16730 0.10324 0.02500
        #>
        #>
        #> $dv3
        #>
        #> Call:
        #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
        #>
        #> Coefficients:
        #> (Intercept) iv1 iv2 iv3
        #> 0.018492 -0.162563 0.002738 0.179366


        Created on 2018-11-26 by the reprex package (v0.2.1)






        share|improve this answer


























          1












          1








          1







          Here's another approach using the tidyverse packages, since dplyr has more or less supplanted plyr.





          library(tidyverse)

          mydf <- data.frame(dv1 = rnorm(100),
          dv2 = rnorm(100),
          dv3 = rnorm(100),
          iv1 = rnorm(100),
          iv2 = rnorm(100),
          iv3 = rnorm(100))

          mymodel <- function(df) {
          lm(value ~ iv1 + iv2 + iv3, data = df)
          }

          mydf %>%
          gather("variable","value", contains("dv")) %>%
          split(.$variable) %>%
          map(mymodel)

          #> $dv1
          #>
          #> Call:
          #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
          #>
          #> Coefficients:
          #> (Intercept) iv1 iv2 iv3
          #> -0.04516 -0.04657 0.08045 0.02518
          #>
          #>
          #> $dv2
          #>
          #> Call:
          #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
          #>
          #> Coefficients:
          #> (Intercept) iv1 iv2 iv3
          #> -0.03906 0.16730 0.10324 0.02500
          #>
          #>
          #> $dv3
          #>
          #> Call:
          #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
          #>
          #> Coefficients:
          #> (Intercept) iv1 iv2 iv3
          #> 0.018492 -0.162563 0.002738 0.179366


          Created on 2018-11-26 by the reprex package (v0.2.1)






          share|improve this answer













          Here's another approach using the tidyverse packages, since dplyr has more or less supplanted plyr.





          library(tidyverse)

          mydf <- data.frame(dv1 = rnorm(100),
          dv2 = rnorm(100),
          dv3 = rnorm(100),
          iv1 = rnorm(100),
          iv2 = rnorm(100),
          iv3 = rnorm(100))

          mymodel <- function(df) {
          lm(value ~ iv1 + iv2 + iv3, data = df)
          }

          mydf %>%
          gather("variable","value", contains("dv")) %>%
          split(.$variable) %>%
          map(mymodel)

          #> $dv1
          #>
          #> Call:
          #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
          #>
          #> Coefficients:
          #> (Intercept) iv1 iv2 iv3
          #> -0.04516 -0.04657 0.08045 0.02518
          #>
          #>
          #> $dv2
          #>
          #> Call:
          #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
          #>
          #> Coefficients:
          #> (Intercept) iv1 iv2 iv3
          #> -0.03906 0.16730 0.10324 0.02500
          #>
          #>
          #> $dv3
          #>
          #> Call:
          #> lm(formula = value ~ iv1 + iv2 + iv3, data = df)
          #>
          #> Coefficients:
          #> (Intercept) iv1 iv2 iv3
          #> 0.018492 -0.162563 0.002738 0.179366


          Created on 2018-11-26 by the reprex package (v0.2.1)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 21:31









          Jake KauppJake Kaupp

          5,64721428




          5,64721428

























              0














              You could convert your data.frame to long form, with all the dv values in one column and then use plyr's dlply to create the lms. This splits the data.frame on the specified column ("dvN") and applys the function to each and returns a list of lms. I have changed the function slightly to make it just take a data.frame, not the column separately.



              Hope this gives what you need.



              library(plyr)
              library(tidyr)

              mydf_l <- gather(mydf, dvN, Value, 1:3)

              mymodel2 <- function(df) {
              lm(Value ~ iv1 + iv2 + iv3, data = df)
              }

              allmodels <- dlply(mydf_l, .(dvN), mymodel2)





              share|improve this answer




























                0














                You could convert your data.frame to long form, with all the dv values in one column and then use plyr's dlply to create the lms. This splits the data.frame on the specified column ("dvN") and applys the function to each and returns a list of lms. I have changed the function slightly to make it just take a data.frame, not the column separately.



                Hope this gives what you need.



                library(plyr)
                library(tidyr)

                mydf_l <- gather(mydf, dvN, Value, 1:3)

                mymodel2 <- function(df) {
                lm(Value ~ iv1 + iv2 + iv3, data = df)
                }

                allmodels <- dlply(mydf_l, .(dvN), mymodel2)





                share|improve this answer


























                  0












                  0








                  0







                  You could convert your data.frame to long form, with all the dv values in one column and then use plyr's dlply to create the lms. This splits the data.frame on the specified column ("dvN") and applys the function to each and returns a list of lms. I have changed the function slightly to make it just take a data.frame, not the column separately.



                  Hope this gives what you need.



                  library(plyr)
                  library(tidyr)

                  mydf_l <- gather(mydf, dvN, Value, 1:3)

                  mymodel2 <- function(df) {
                  lm(Value ~ iv1 + iv2 + iv3, data = df)
                  }

                  allmodels <- dlply(mydf_l, .(dvN), mymodel2)





                  share|improve this answer













                  You could convert your data.frame to long form, with all the dv values in one column and then use plyr's dlply to create the lms. This splits the data.frame on the specified column ("dvN") and applys the function to each and returns a list of lms. I have changed the function slightly to make it just take a data.frame, not the column separately.



                  Hope this gives what you need.



                  library(plyr)
                  library(tidyr)

                  mydf_l <- gather(mydf, dvN, Value, 1:3)

                  mymodel2 <- function(df) {
                  lm(Value ~ iv1 + iv2 + iv3, data = df)
                  }

                  allmodels <- dlply(mydf_l, .(dvN), mymodel2)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 21:21









                  user2738526user2738526

                  716410




                  716410















                      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)