mercredi 18 mars 2020

Program that inputs observation into a vector based on intersection of three observations

Hello I am having difficulty with finding a solution to a program I have been writing and could use some support.

The objective is to create a new parameter called Lambda for each person in my data set. Lambda is a function of 3 observations found in Df_obvs: Row, OAlpha, OSigma.

Row determines the data.frame used to find Lambda while the OSigma and OAlpha observations identify the row and column that identifies the individuals Lambda parameter. There are 3 possible "Row" options: 1, 2, and 3 so there are 3 possible data frames: Row1, Row2, and Row3. It is important to state in my real problem Row1[1,1]!=Row2[1,1] however for this example you will notice that Row1=Row2=Row3.

#create observation matrix
Names<-c('Bob','Jane','Sue')
Row<-c(1,2,3)
OAlpha<-c(.05,.75,1.5)
OSigma<-c(.1,.9,1.4)
Df_obvs<-cbind.data.frame(Names,OAlpha,OSigma,Row)

Creation of the data frames used to identify the Lambda parameters.

Switch<-data.frame(replicate(30,sample(0:8,30,rep=TRUE)))
alpha<-seq(.05,1.5,.05)
sigma<-seq(.05,1.5,.05)
colnames(Switch)<-alpha
rownames(Switch)<-sigma

Row1<-Switch
Row2<-Switch
Row3<-Switch

This is where my challenge lies. I would like to have the program check the individuals observation for Row in order to identify which data frame (Row1, Row 2, or Row 3) to use to determine Lambda. Then use the observations OSigma and OAlpha to locate the exact cell in the data frame and set Lambda[j] equal to that.

final_lambda<- matrix(NA,3,1)
for (j in 1:length(Df_obvs[,1])){
       if (Df_obvs$Row[j]==1) {Lambda[j]<-sapply(1:nrow(Df_obvs), function(i){Row1[Df_obvs$OSigma[i],Df_obvs$OAlpha[i]]})}
  else if (Df_obvs$Row[j]==2) {Lambda[j]<-sapply(1:nrow(Df_obvs), function(i){Row2[Df_obvs$OSigma[i],Df_obvs$OAlpha[i]]})}
  else                        {Lambda[j]<-sapply(1:nrow(Df_obvs), function(i){Row3[Df_obvs$OSigma[i],Df_obvs$OAlpha[i]]})}}

I appreciate your time and look forward to seeing where I am going wrong. Please let me know if I should clarify anything.

Aucun commentaire:

Enregistrer un commentaire