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)