# Error: \$ operator is invalid for atomic vectors

• Operating System: Microsoft Windows [Version 10.0.17134.345]
• brms Version: 2.5.0

I want to fit a binomial brms model containing the weight option. I have followed the instructions discussed here but when I try using the code and data below I get the error:

`Error: \$ operator is invalid for atomic vectors`

n r r/n group treat c2 c1 w
62 3 0.048387097 1 0 0.1438 1.941115288 1.941115288
96 1 0.010416667 1 0 0.237 1.186583128 1.186583128
17 0 0 0 0 0.2774 1.159882668 3.159882668
41 2 0.048780488 1 0 0.2774 1.159882668 3.159882668
212 170 0.801886792 0 0 0.2093 1.133397521 1.133397521
143 21 0.146853147 1 1 0.1206 1.128993008 1.128993008
143 0 0 1 1 0.1707 1.128993008 2.128993008
143 33 0.230769231 0 1 0.0699 1.128993008 1.128993008
73 62 1.260273973 0 1 0.1351 1.121927228 1.121927228
73 17 0.232876712 0 1 0.1206 1.121927228 1.121927228")

mod <- brm(r|trials(n)|weights(w) ~ treat*c2+(1|group), data=dt, family=binomial(link=logit))

I would appreciate any help to solve this error.

Can we get a `traceback()` after you trigger that error message?

`r|trials(n)|weights(w)` should rather be `r | trials(n) + weights(w)`

Thanks a lot for your help @paul.buerkner and @bgoodri.

The specification

r | trials(n) + weights(w)

works.

Just another, related question, if you don’t mind:
I would like to get proportions r/n of the expected values of the response and the posterior samples of the response, instead of N*p from the output of fitted() and predict() functions as discussed here.

Any help would be much appreciated.

In `newdata` set your `n` variable to `1`.

When I set n=1 in `newdata` I get this error:

`Error: Number of trials must be positive integers.`

``````newdata <- data.frame(treat = factor(c("0", "1")),
c2= c(0,1),
group= factor(c("0", "1"),
n = 1)
predict(mod, newdata = newdata)
``````

You should double check your `newdata` for typos. When I write

``````newdata <- data.frame(
treat = c(0, 1), c2=c(0,1),
group= factor(c("0", "1")), n = 1
)
``````

it works for me.

1 Like

You are right, @paul.buerkner

There was a typo in the sample data.

It works for me too.

Thanks a lot for this.

Hi,

I am doing multinomial logit regression of survey data.

My dependent variable has three levels 1,2,3. I divied the data set 1 as reference value that named as training and 2,3 as testing value. When I go for prediction of training it Works. But when I go for testing values,

pred2<- predict(m1,data=testing)
tab2<- table(pred,(testing\$CCS))
(CCS is DV of my model)

Then, it comes like this
Error in testing\$CCS : \$ operator is invalid for atomic vectors

Its coming the same error!
I checked
is.vector(testing)

It is showing FALSE
Then why?

hy everyone.
I’m new on Rstudio and i’m using it for some econometrics stuff.
I have a error message : “Error in result\$V2 : \$ operator is invalid for atomic vectors”
I’m trying to do a linear regression on a data that I have simulate myself using Var-covariance matrix.
When a try to run the regression I have the error message above in " ".
there is my code:

rom <- set.seed(24)
summary(“rom”)

install.packages(“MASS”)
install.packages(“rockchalk”)
install.packages(“mvtnorm”)

library(MASS)
library(rockchalk)
library(mvtnorm)

mu=c(63,93)
sigma=matrix(c(17,2,2,16),2,2)
result=rmvnorm(1000,mu,sigma)
summary(result)
linearRegResult = lm(result\$v2 ~ result\$v1)

Hi Romain,

Two issues here. When specifying regressions in `lm`, you only need to specify the variable name (without the `result\$` prefix) and then specify which dataset the variables belong to. Additionally, the `rmvnorm` function returns a matrix, whereas `lm` only accepts a `data.frame`.

The following code will work for you:

``````library(mvtnorm)

mu=c(63,93)
sigma=matrix(c(17,2,2,16),2,2)
result=data.frame(rmvnorm(1000,mu,sigma))
summary(result)
linearRegResult = lm(X2 ~ X1, result)
``````

In the future, questions like this are better suited to the RStudio Community forums: https://community.rstudio.com/

This forum is for Stan-related questions

Ooh did not notice that for the misliding community, sorry and ty for the info :)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7355027,"math_prob":0.90304977,"size":1544,"snap":"2022-05-2022-21","text_gpt3_token_len":379,"char_repetition_ratio":0.09415584,"word_repetition_ratio":0.025,"special_character_ratio":0.23510362,"punctuation_ratio":0.11935484,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9875488,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-19T17:49:03Z\",\"WARC-Record-ID\":\"<urn:uuid:5b7d5ccd-f943-480e-9491-b23e63804e0f>\",\"Content-Length\":\"52388\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a997eb87-2ec7-433d-bb6e-6942f6bf1fa0>\",\"WARC-Concurrent-To\":\"<urn:uuid:3dbba314-d756-466b-80ea-84306fbf4e4e>\",\"WARC-IP-Address\":\"64.62.250.111\",\"WARC-Target-URI\":\"https://discourse.mc-stan.org/t/error-operator-is-invalid-for-atomic-vectors/6156\",\"WARC-Payload-Digest\":\"sha1:OBMXHGFCMNNGMQDMOYL7TNAW66SNPGFN\",\"WARC-Block-Digest\":\"sha1:JYTFZKJFNWHUKT3CR23H7L67DRFADNXK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662529658.48_warc_CC-MAIN-20220519172853-20220519202853-00000.warc.gz\"}"}