r Creating models on subsets with data.table inside a function

后端 未结 1 1508
南笙
南笙 2021-01-22 14:38

Using data.table, I am trying to write a function that takes a data table, a formula object, and a string as arguments, and creates and stores multiple model objects.

         


        
相关标签:
1条回答
  • 2021-01-22 15:35

    You've to evaluate it within the environment .SD (as lm can not "see" V2 and V3 otherwise):

    SectRegress <- function (df,eq,sectors) {
        Output <- df[, list(model=list(lm(eq, .SD))), by=sectors]
        return(Output)
    }
    Test <- SectRegress(myData,formula(V2~V3),sectors="V1")
    
    0 讨论(0)
提交回复
热议问题