Home > Software design >  R: Error in set (x, j=name, value=value): supplied x items to be assigned to x items of column "
R: Error in set (x, j=name, value=value): supplied x items to be assigned to x items of column "

Time:08-16

I have a dataframe (df, see below for the head of the df) that includes the proportions of predicted behaviour using four different models.

  • Total number of individuals: 12
  • Total number of days: 6
  • Total number of models: 4
  • Total number of behaviours: 9

ID_Day  Model PropActive PropEating    PropGroom      PropLit   PropOther  PropRest   PropScrat   PropSit   PropStand
Cho_1  C.RF5 0.01685185 0.02296296 7.053241e-02 1.157407e-05 0.009745370 0.5154745 0.001944444 0.3519097 0.010567130
Cho_1 C.SOM5 0.00000000 0.02565972 8.262731e-02 0.000000e 00 0.008217593 0.5735301 0.000000000 0.3000926 0.009872685
Cho_1  H.RF5 0.02368056 0.01856481 5.685185e-02 6.712963e-04 0.027395833 0.3424769 0.001446759 0.5040046 0.024907407
Cho_1 H.SOM5 0.04878472 0.02337963 1.157407e-05 0.000000e 00 0.048784722 0.3364352 0.000000000 0.5208681 0.021736111
Cho_2  C.RF5 0.01115741 0.02149306 5.105324e-02 6.944444e-05 0.008460648 0.6110648 0.001064815 0.2876852 0.007951389
Cho_2 C.SOM5 0.00000000 0.19230324 9.521991e-02 0.000000e 00 0.006284722 0.5040625 0.000000000 0.1916204 0.010509259

*ID_Day is a combination of individual and day.


I would like to do a Dirichlet regression to compare the proportions of the predicted behaviour between the models.

When I run the following code:

df$Y <- DR_data(df[, c("PropActive", "PropEating", "PropGroom", "PropLit", "PropOther","PropRest", "PropScrat", "PropSit", "PropStand")], base=1)

I get the following error:

Error in set(x, j = name, value = value) : Supplied 2592 items to be assigned to 288 items of column 'Y'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

Even if I select only one behaviour, I get the same error:

df$Y <- DR_data(df[, c("PropActive")], base=1)

Error in set(x, j = name, value = value) : Supplied 576 items to be assigned to 288 items of column 'Y'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code

I am not quite sure how to solve this error and was hoping someone could help me with this.

Library: DirichletReg

CodePudding user response:

I don't think you can add the DR_data results to your original dataframe as the results are a class 'DirichletRegData' object. If you save that object to another variable (e.g. "Y") it should work as expected:

#install.packages("DirichletReg")
library(DirichletReg)
#> Loading required package: Formula

df <- read.table(text = "ID_Day  Model PropActive PropEating    PropGroom      PropLit   PropOther  PropRest   PropScrat   PropSit   PropStand
1:  Cho_1  C.RF5 0.01685185 0.02296296 7.053241e-02 1.157407e-05 0.009745370 0.5154745 0.001944444 0.3519097 0.010567130
2:  Cho_1 C.SOM5 0.00000000 0.02565972 8.262731e-02 0.000000e 00 0.008217593 0.5735301 0.000000000 0.3000926 0.009872685
3:  Cho_1  H.RF5 0.02368056 0.01856481 5.685185e-02 6.712963e-04 0.027395833 0.3424769 0.001446759 0.5040046 0.024907407
4:  Cho_1 H.SOM5 0.04878472 0.02337963 1.157407e-05 0.000000e 00 0.048784722 0.3364352 0.000000000 0.5208681 0.021736111
5:  Cho_2  C.RF5 0.01115741 0.02149306 5.105324e-02 6.944444e-05 0.008460648 0.6110648 0.001064815 0.2876852 0.007951389
6:  Cho_2 C.SOM5 0.00000000 0.19230324 9.521991e-02 0.000000e 00 0.006284722 0.5040625 0.000000000 0.1916204 0.010509259",
header = TRUE)

Y <- DR_data(df[,c("PropActive", "PropEating", "PropGroom", "PropLit", "PropOther","PropRest", "PropScrat", "PropSit", "PropStand")], base=1)
#> Warning in DR_data(df[, c("PropActive", "PropEating", "PropGroom", "PropLit", : not all rows sum up to 1 => normalization forced
#>   some entries are 0 or 1 => transformation forced
str(Y)
#>  'DirichletRegData' num [1:6, 1:9] 0.0326 0.0185 0.0383 0.0592 0.0278 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:6] "1:" "2:" "3:" "4:" ...
#>   ..$ : chr [1:9] "PropActive" "PropEating" "PropGroom" "PropLit" ...
#>  - attr(*, "Y.original")='data.frame':   6 obs. of  9 variables:
#>   ..$ PropActive: num [1:6] 0.0169 0 0.0237 0.0488 0.0112 ...
#>   ..$ PropEating: num [1:6] 0.023 0.0257 0.0186 0.0234 0.0215 ...
#>   ..$ PropGroom : num [1:6] 7.05e-02 8.26e-02 5.69e-02 1.16e-05 5.11e-02 ...
#>   ..$ PropLit   : num [1:6] 1.16e-05 0.00 6.71e-04 0.00 6.94e-05 ...
#>   ..$ PropOther : num [1:6] 0.00975 0.00822 0.0274 0.04878 0.00846 ...
#>   ..$ PropRest  : num [1:6] 0.515 0.574 0.342 0.336 0.611 ...
#>   ..$ PropScrat : num [1:6] 0.00194 0 0.00145 0 0.00106 ...
#>   ..$ PropSit   : num [1:6] 0.352 0.3 0.504 0.521 0.288 ...
#>   ..$ PropStand : num [1:6] 0.01057 0.00987 0.02491 0.02174 0.00795 ...
#>  - attr(*, "dims")= int 9
#>  - attr(*, "dim.names")= chr [1:9] "PropActive" "PropEating" "PropGroom" "PropLit" ...
#>  - attr(*, "obs")= int 6
#>  - attr(*, "valid_obs")= int 6
#>  - attr(*, "normalized")= logi TRUE
#>  - attr(*, "transformed")= logi TRUE
#>  - attr(*, "base")= num 1
Y
#>    PropActive PropEating  PropGroom    PropLit  PropOther  PropRest  PropScrat
#> 1: 0.03256173 0.03765432 0.07729553 0.01852816 0.02663966 0.4480806 0.02013889
#> 2: 0.01851852 0.03990162 0.08737461 0.01851852 0.02536651 0.4964603 0.01851852
#> 3: 0.03825232 0.03398919 0.06589506 0.01907793 0.04134838 0.3039159 0.01972415
#> 4: 0.05917245 0.03800154 0.01852816 0.01851852 0.05917245 0.2988812 0.01851852
#> 5: 0.02781636 0.03642940 0.06106288 0.01857639 0.02556906 0.5277392 0.01940586
#> 6: 0.01851852 0.17877121 0.09786844 0.01851852 0.02375579 0.4385706 0.01851852
#>      PropSit  PropStand
#> 1: 0.3117766 0.02732446
#> 2: 0.2685957 0.02674576
#> 3: 0.4385223 0.03927469
#> 4: 0.4525752 0.03663194
#> 5: 0.2582562 0.02514468
#> 6: 0.1782022 0.02727623

Created on 2022-08-16 by the reprex package (v2.0.1)

  • Related