# [R] spaghetti plot - urgent

Rosa Oliveira rosita21 at gmail.com
Wed Jul 19 16:51:04 CEST 2017

```Hi everyone,

I’m trying to do a spaghetti plot and I know I’m doing all wrong, It must be.

What I need:

15 subjects, each with measurements over 5 different times (t1, ..., t5), and the variable that I need to represent in the spaguetti plot is given by:

PCR = b0 + b1 * ti + epsilon

B0, - baseline of each subject
B1 - trajectory of each subject over time (so multiply by t)
Epsilon - error associated with each subject

Regression model with mixed effects.

Thus, I generated b0, b1, epsilon and time created sequence.

But I need to do spaguetti plot of the outcome and I can not understand how much I search the publications.

Sorry for the stupidity, but I do not even know how to do it and I need it with the utmost urgency to finish a publication proposal :(

Follows what I tried to do :( :( :(

library(ggplot2)
library(reshape)
library(lattice)
library(gtable)
library(grid)

set.seed(9027)

n.longitudinal.observations  = 5 			  # number of PCR measures (per subject) in the hospital period
subjects                    	     = 15                      # Number of simulations (1 per subject in the study)

beta0_7_gerar                      = rnorm(subjects, mean = 1, sd = .5)
beta0_7                                = as.data.frame(matrix(beta0_7_gerar,nrow=subjects,ncol=1))      # beta 0 - input variable used to calculate PCR (the outcome)
beta1_7_gerar                     = rnorm(subjects, mean = -1, sd = .5)
beta1_7                               = as.data.frame(matrix(beta1_7_gerar,nrow=subjects,ncol=1) )      # beta 1 - input variable used to calculate PCR (the outcome)

tj_gerar                                = seq.int(1, n.longitudinal.observations, 1)
epsilon_7_gerar                  = rnorm(5*subjects, mean = 0, sd = .1)
epsilon_7                             = as.data.frame(matrix(epsilon_7_gerar,nrow=subjects,ncol=1) )   # epsilon_7 - input variable used to calculate PCR (the outcome) - associated with each subject

tj                                          = as.data.frame(matrix(tj_gerar,nrow=subjects,ncol=1) )		      # time

point7 <- cbind(beta0_7, beta1_7, tj, epsilon_7)
point7
point7 <- as.data.frame(point7)

colnames(point7) = c("beta0_7","beta1_7","time", "epsilon_7")

y_point7 <- point7\$beta0_7 + point7\$beta1_7*point7\$time + point7 \$epsilon_7		(the outcome of the study - PCR)
y_point7

require(ggplot2)

png('test.png')
p = ggplot(y_point7, aes(time, y_point7)) + geom_line()
print(p)
dev.off()
savehistory()

OR:

In the last part I also tried:

ID = rep(1:3, each = 5)

point7 <- cbind(ID,beta0_7, beta1_7, tj, epsilon_7)
point7
point7 <- as.data.frame(point7)

colnames(point7) = c("ID","beta0_7","beta1_7","time", "epsilon_7")

y_point7 <- point7\$beta0_7 + point7\$beta1_7*point7\$time + point7 \$epsilon_7
y_point7

crp7 <- y_point7

ggplot(aes(x = tj_gerar, y = crp7), data = point7) +
geom_line(aes(group = ID), color = "gray") +
geom_smooth(aes(group = 1), method = "lm", size = 3, color = "red", se = FALSE) +
theme_bw()

But none of these worked :(

I was looking to have something like:

Being the outcome PCR and the year the times (1, 2, 3, 4, 5).