0

I'm trying to do an multiple regression that looks like this:

Y_ij=a1_i+X_ja2_i+Z_ja3_i+e_ij

The dependent variable Y_ij consists of a vector, which is composed of j variables. These variables differ, depending on the subject i. Both independent vectors X_j and Z_j are also composed of j variables each. These vectors are constant, independent from the subject i.

I do the analyses by creating a vector Y using the variables Y_i1-6, but then I have problems with the person/group specific evaluation. I do not know what I am doing wrong. I hope, that anybody here can help me. Thank you!

Data example:

library(dplyr)
library(psych)
library(mice)
library(miceadds)
library(tidyr)
library(purrr)
library(broom)
library(plyr)

dput(data[1:4, ])
#> structure(list(ID = c(1, 2, 3, 4), group = c(1, 1, 2, 2), Y1 = c("4,2", 
#> "4,2", "4,0", "3,0"), Y2 = c("3,8", "3,0", "4,5", "3,0"), Y3 = c("3,0", 
#> "2,0", "3,8", "3,1"), Y4 = c("1,8", "4,0", "3,8", "4,0"), Y5 = c("1,5", 
#> "1,5", "4,2", "3,9"), Y6 = c("3,5", "1,8", "4,0", "2,8")), row.names = c(NA, 
#> -4L), class = c("tbl_df", "tbl", "data.frame"))

#Regression

Y<-as.vector(t(cbind(data$Y1, data$Y2, data$Y3, data$Y4, data$Y5, data$Y6)))#dependent variable: making vector from different variables
cos<-c(cos(0:5 * pi / 3)) #independent variable 1 (the same for each person)
sin<-c(sin(0:5 * pi / 3)) #independent variable 2 (the same for each person)
group<-factor(rep(c(data$group),each=6),levels=c(1,2),labels=c("A", "B"))
dat<-data.frame(Y, cos, sin, group)

dat[,coef(lm(Y~cos+sin)),by=group]
#> Error in `[.data.frame`(dat, , coef(lm(Y ~ cos + sin)), by = group): unbenutztes Argument (by = group)

Created on 2021-09-23 by the reprex package (v0.3.0)

Andreas
  • 11
  • 1
  • 1
    We don't have your `data` object. Could you make your question reproducible? – user2554330 Sep 21 '21 at 13:26
  • Minor comment, you can simplify your `cos` and `sin` definitions with vectorization, `cos <- do.call(cbind, cos(0:5 * pi / 3))`... though I'm not really sure why you're using `unlist(cbind())`. I think you'd get the same result with `cos_DP = cos(0:5 * pi / 3)`, skipping the `cbind` and `unlist` entirely. – Gregor Thomas Sep 21 '21 at 13:30
  • @GregorThomas Thank you for simplify [cos] and [sin] definitions. [cbind] was because of the vectorform. Is this approach wrong? – Andreas Sep 21 '21 at 15:20
  • `cbind` is for binding together columns in matrices or data frames. (Analogous to `rbind`, which is for rows). By using `cbind`, you are creating a 1x6 row vector, which is unusual. `cos(0:5 * pi / 3)` is a 6x1 standard column vector. – Gregor Thomas Sep 21 '21 at 15:45
  • If your 1x6 vector row vector was a data frame (which are built on lists), then `unlist()` would covert it back to a standard 6x1 column vector. However, since the objects are the same type your `cbind` creates a matrix row vector, and `unlist()` does nothing, because it is already not a list. – Gregor Thomas Sep 21 '21 at 15:48
  • Generally, we use `c()` to create vectors, so you could use `c(cos(0), cos(pi/3), cos(2*pi/3),cos(pi),cos(4*pi/3), cos(5*pi/3))`, but for nice sequences we don't have to type out values, `0:5` is shorthand for `c(0, 1, 2, 3, 4, 5)`, and pretty much all math operations, like multiplying, dividing, and taking a cosine, are vectorized, so they operate on the whole vector, which is why `cos(0:5 * pi / 3)` is the same as `c(cos(0), cos(pi/3), cos(2*pi/3),cos(pi),cos(4*pi/3), cos(5*pi/3))` – Gregor Thomas Sep 21 '21 at 15:50
  • @GregorThomas Thanks for the support and sorry for the basic problems in using R. I am not working with R that long (as probably can be seen). – Andreas Sep 22 '21 at 07:28
  • Just like we don't have your `data` object, we also don't have your `"202109_reprex.sav"` file, so this is still not reproducible. It looks like none of your code is working because you never define an object called `imp_r`, but you try to use it a lot. Hence all the errors, which is R telling you it cannot run your code. You need to start debugging at the first error, not the last one. – Gregor Thomas Sep 22 '21 at 13:44
  • Please see [this link for guidance in making a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). We don't need to see the code you use to read in data - as long as you think that code is working. Instead, we need a small, copy/pasteable version of your data, which you can produce with, e.g., `dput(data[1:10, ])` for the first 10 rows. Share just enough data to demonstrate the problem. – Gregor Thomas Sep 22 '21 at 13:45
  • @GregorThomas Thanks for the tip, I'll start implementing it right away. – Andreas Sep 22 '21 at 15:34

0 Answers0