2016-05-24 96 views
2

這個玩具示例允許我針對線性迴歸的mtcars數據集反應更新兩個我感興趣的矢量的R平方值。但是,我需要能夠計算數據中幾個不同組的R平方值(例如,使用「cyl」的級別)。添加按組計算多個R平方值

selectInput("group","Group",""),​​和

detco.lm <- lm(mtcars[,input$xcol] ~ mtcars[,input$ycol], data=mtcars[,input$group]) 

沒有產生預期的結果 - 我一直得到只是單一的R平方值。我在尋找R的表或列表的平方與輸入組相應值就像

#Having selected "mpg" and "drat" as my x/y variables 
    cyl(4.00)=.4591  
    cyl(6.00)=.6679  
    cyl(8.00)=.1422 

回答

3

我覺得這RStudio博客,https://blog.rstudio.org/2015/09/29/purrr-0-1-0/,可能有幾乎正是你在「地圖功能尋找代碼「 部分。你需要purrr包。

編輯:粘貼來自該部分的代碼以防萬一,以後出於任何原因,該鏈接會發生變化。別忘了install.packages('purrr')

mtcars %>% 
    split(.$cyl) %>% 
    map(~lm(mpg ~ wt, data = .)) %>% 
    map(summary) %>% 
    map_dbl("r.squared") 
#>  4  6  8 
#> 0.509 0.465 0.423 
+0

你,我的朋友,剛剛度過我的一天。非常感謝,指導我嗚嗚! – Scott