मैं जानना चाहता हूं कि आर प्रोग्रामिंग में एक नेस्टेड फॉर लूप का एक अधिक संक्षिप्त रूप में प्रतिस्थापन कैसे करें।

मेरे परिवर्तनीय नाम हैं:

names(Auto[-1])

"सिलेंडर" "विस्थापन" "अश्वशक्ति" "वजन" "त्वरण" "वर्ष" "मूल"

तो मैंने एक सरल के लिए लूप किया:

for (var1 in names(Auto[-1]))
    {
      listanova[var] =  summary(lm((paste('mpg ~',var1)), data = Auto))[6]$sigma
      }

मैं अधिक जटिल के लिए लूप कैसे कर सकता हूं लेकिन नेस्टेड किए बिना, मैं लंबे, जटिल के लिए लूप से बचना चाहता हूं। उदाहरण के लिए, मैं var1*var2+var3 की हर संभावना को पुनरावृत्त करना चाहता हूं जहां ये चर ऑटो [-1] वाले हैं। क्या यह संभव है?

0
user799825 14 जून 2020, 20:57

2 जवाब

यहाँ combn और apply के साथ एक दृष्टिकोण दिया गया है:

library(ISLR)
data(Auto)
combinations <- combn(names(Auto[-1]),3)
combinations[,1:5]
     [,1]           [,2]           [,3]           [,4]           [,5]          
[1,] "cylinders"    "cylinders"    "cylinders"    "cylinders"    "cylinders"   
[2,] "displacement" "displacement" "displacement" "displacement" "displacement"
[3,] "horsepower"   "weight"       "acceleration" "year"         "origin"  

apply(combinations,2,function(x)summary(lm((paste("mpg ~",x[1],"*",x[2],"+",x[3])), data = Auto))[6]$sigma)
 [1] 4.150379 4.192360 4.451150 3.628036 4.454612 2.733227 3.897318 3.964971 3.375560 3.992428 2.652409 4.100272
[13] 3.092482 4.151961 2.717629 4.078887 4.759292 2.869345 3.950697 2.580032 2.852769 3.875941 3.848881 3.169038
[25] 3.916857 2.628840 4.056102 3.019469 4.099032 2.698532 3.569871 4.444354 2.708595 3.679281 2.406705 2.719861
[37] 3.923677 2.963053 3.889403 2.648480 3.725775 4.197468 2.555832 3.560784 2.453807 2.682554 3.223483 4.194124
[49] 2.797606 3.140978 2.294686 2.738440 5.012415 2.662019 2.899071 2.630942

जैसा कि @Onyambu टिप्पणियों में बताते हैं, यदि आप चाहें तो आप लैम्ब्डा फ़ंक्शन को apply से सीधे combn में स्थानांतरित कर सकते हैं।

0
Ian Campbell 14 जून 2020, 18:17

मुझे लगता है कि mpg ~ Var1 * Var2 + Var3 को expand.grid के साथ 2 चरों के combn के संयोजन की आवश्यकता होगी, न कि केवल combn में से, क्योंकि Var1 * Var2 == Var2 * Var1, लेकिन Var1 * Var2 + Var3!= Var1 * Var3 + Var2

इस मुद्दे को संबोधित करने वाला एक दृष्टिकोण यहां दिया जाएगा:

library(ISLR)
data(Auto)
cols <- names(Auto)[-1]
res <- expand.grid(combn(cols, 2, simplify=FALSE), cols)
res <- res[lengths(apply(res, 1, 
    function(x) intersect(as.character(x$Var2), unlist(x$Var1)))) == 0,]
res <- setNames(data.frame(
    t(data.frame(res$Var1)), as.character(res$Var2), row.names = NULL), 
    paste0("Var", 1:3))
res$sigma <- apply(res, 1, 
    function(x) sigma(lm(paste0("mpg ~ ", x[1], " * ", x[2], " + ", x[3]), 
        data = Auto)))
res
#>             Var1         Var2         Var3    sigma
#> 1   displacement   horsepower    cylinders 3.930608
#> 2   displacement       weight    cylinders 4.101442
#> 3   displacement acceleration    cylinders 4.461698
#> 4   displacement         year    cylinders 3.733483
#> 5   displacement       origin    cylinders 4.520063
#> 6   displacement         name    cylinders 2.422872
#> 7     horsepower       weight    cylinders 3.931893
#> 8     horsepower acceleration    cylinders 4.229184
#> 9     horsepower         year    cylinders 3.650538
#> 10    horsepower       origin    cylinders 4.169378
#> 11    horsepower         name    cylinders 2.558382
#> 12        weight acceleration    cylinders 4.230993
#> 13        weight         year    cylinders 3.195613
#> 14        weight       origin    cylinders 4.209175
#> 15        weight         name    cylinders 2.575035
#> 16  acceleration         year    cylinders 4.108109
#> 17  acceleration       origin    cylinders 4.623440
#> 18  acceleration         name    cylinders 2.752275
#> 19          year       origin    cylinders 3.957220
#> 20          year         name    cylinders 2.373485
#> 21        origin         name    cylinders 2.852769
#> 22     cylinders   horsepower displacement 3.996131
#> 23     cylinders       weight displacement 4.144523
#> 24     cylinders acceleration displacement 4.546081
#> 25     cylinders         year displacement 3.820275
#> 26     cylinders       origin displacement 4.586554
#> 27     cylinders         name displacement 2.824717
#> 28    horsepower       weight displacement 3.927991
#> 29    horsepower acceleration displacement 4.098049
#> 30    horsepower         year displacement 3.511056
#> 31    horsepower       origin displacement 4.130212
#> 32    horsepower         name displacement 2.435430
#> 33        weight acceleration displacement 4.210034
#> 34        weight         year displacement 3.192645
#> 35        weight       origin displacement 4.199256
#> 36        weight         name displacement 2.624553
#> 37  acceleration         year displacement 3.767955
#> 38  acceleration       origin displacement 4.391746
#> 39  acceleration         name displacement 2.763000
#> 40          year       origin displacement 3.828119
#> 41          year         name displacement 2.211958
#> 42        origin         name displacement 2.776667
#> 43     cylinders displacement   horsepower 4.150379
#> 44     cylinders       weight   horsepower 4.003118
#> 45     cylinders acceleration   horsepower 4.361433
#> 46     cylinders         year   horsepower 3.858321
#> 47     cylinders       origin   horsepower 4.411365
#> 48     cylinders         name   horsepower 2.794140
#> 49  displacement       weight   horsepower 3.952595
#> 50  displacement acceleration   horsepower 4.144063
#> 51  displacement         year   horsepower 3.625651
#> 52  displacement       origin   horsepower 4.391267
#> 53  displacement         name   horsepower 2.437027
#> 54        weight acceleration   horsepower 4.141952
#> 55        weight         year   horsepower 3.160076
#> 56        weight       origin   horsepower 4.126864
#> 57        weight         name   horsepower 2.600191
#> 58  acceleration         year   horsepower 4.048410
#> 59  acceleration       origin   horsepower 4.322450
#> 60  acceleration         name   horsepower 2.542587
#> 61          year       origin   horsepower 3.956825
#> 62          year         name   horsepower 2.256631
#> 63        origin         name   horsepower 2.797134
#> 64     cylinders displacement       weight 4.192360
#> 65     cylinders   horsepower       weight 3.897318
#> 66     cylinders acceleration       weight 4.267501
#> 67     cylinders         year       weight 3.288582
#> 68     cylinders       origin       weight 4.253179
#> 69     cylinders         name       weight 2.801128
#> 70  displacement   horsepower       weight 3.875941
#> 71  displacement acceleration       weight 4.216553
#> 72  displacement         year       weight 3.247050
#> 73  displacement       origin       weight 4.248519
#> 74  displacement         name       weight 2.410626
#> 75    horsepower acceleration       weight 4.120773
#> 76    horsepower         year       weight 3.111929
#> 77    horsepower       origin       weight 3.994020
#> 78    horsepower         name       weight 2.491070
#> 79  acceleration         year       weight 3.282754
#> 80  acceleration       origin       weight 4.153596
#> 81  acceleration         name       weight 2.782508
#> 82          year       origin       weight 3.298971
#> 83          year         name       weight 1.842066
#> 84        origin         name       weight 2.766529
#> 85     cylinders displacement acceleration 4.451150
#> 86     cylinders   horsepower acceleration 3.964971
#> 87     cylinders       weight acceleration 4.100272
#> 88     cylinders         year acceleration 4.135679
#> 89     cylinders       origin acceleration 4.761837
#> 90     cylinders         name acceleration 2.890067
#> 91  displacement   horsepower acceleration 3.848881
#> 92  displacement       weight acceleration 4.056102
#> 93  displacement         year acceleration 3.732550
#> 94  displacement       origin acceleration 4.530853
#> 95  displacement         name acceleration 2.205730
#> 96    horsepower       weight acceleration 3.923677
#> 97    horsepower         year acceleration 3.705777
#> 98    horsepower       origin acceleration 4.282692
#> 99    horsepower         name acceleration 2.322535
#> 100       weight         year acceleration 3.156020
#> 101       weight       origin acceleration 4.200064
#> 102       weight         name acceleration 2.633103
#> 103         year       origin acceleration 5.008915
#> 104         year         name acceleration 2.494472
#> 105       origin         name acceleration 2.942866
#> 106    cylinders displacement         year 3.628036
#> 107    cylinders   horsepower         year 3.375560
#> 108    cylinders       weight         year 3.092482
#> 109    cylinders acceleration         year 4.078887
#> 110    cylinders       origin         year 4.005163
#> 111    cylinders         name         year 2.640081
#> 112 displacement   horsepower         year 3.169038
#> 113 displacement       weight         year 3.019469
#> 114 displacement acceleration         year 3.569871
#> 115 displacement       origin         year 3.743455
#> 116 displacement         name         year 2.327629
#> 117   horsepower       weight         year 2.963053
#> 118   horsepower acceleration         year 3.725775
#> 119   horsepower       origin         year 3.819683
#> 120   horsepower         name         year 2.432993
#> 121       weight acceleration         year 3.223483
#> 122       weight       origin         year 3.263234
#> 123       weight         name         year 1.926429
#> 124 acceleration       origin         year 4.978284
#> 125 acceleration         name         year 2.719680
#> 126       origin         name         year 2.659251
#> 127    cylinders displacement       origin 4.454612
#> 128    cylinders   horsepower       origin 3.992428
#> 129    cylinders       weight       origin 4.151961
#> 130    cylinders acceleration       origin 4.759292
#> 131    cylinders         year       origin 3.950697
#> 132    cylinders         name       origin 2.880736
#> 133 displacement   horsepower       origin 3.916857
#> 134 displacement       weight       origin 4.099032
#> 135 displacement acceleration       origin 4.444354
#> 136 displacement         year       origin 3.679281
#> 137 displacement         name       origin 2.419996
#> 138   horsepower       weight       origin 3.889403
#> 139   horsepower acceleration       origin 4.197468
#> 140   horsepower         year       origin 3.560784
#> 141   horsepower         name       origin 2.547681
#> 142       weight acceleration       origin 4.194124
#> 143       weight         year       origin 3.140978
#> 144       weight         name       origin 2.611288
#> 145 acceleration         year       origin 5.012415
#> 146 acceleration         name       origin 2.956412
#> 147         year         name       origin 2.473915
#> 148    cylinders displacement         name 2.733227
#> 149    cylinders   horsepower         name 2.652409
#> 150    cylinders       weight         name 2.717629
#> 151    cylinders acceleration         name 2.869345
#> 152    cylinders         year         name 2.580032
#> 153    cylinders       origin         name 2.852769
#> 154 displacement   horsepower         name 2.628840
#> 155 displacement       weight         name 2.698532
#> 156 displacement acceleration         name 2.708595
#> 157 displacement         year         name 2.406705
#> 158 displacement       origin         name 2.719861
#> 159   horsepower       weight         name 2.648480
#> 160   horsepower acceleration         name 2.555832
#> 161   horsepower         year         name 2.453807
#> 162   horsepower       origin         name 2.682554
#> 163       weight acceleration         name 2.797606
#> 164       weight         year         name 2.294686
#> 165       weight       origin         name 2.738440
#> 166 acceleration         year         name 2.662019
#> 167 acceleration       origin         name 2.899071
#> 168         year       origin         name 2.630942

2020-06-14 को reprex पैकेज द्वारा बनाया गया (v0.3.0)

0
user12728748 14 जून 2020, 18:54