Difference between revisions of "Worksheets/Week9"

From Statistics for Engineering
Jump to navigation Jump to search
(Replaced content with "=== Part 1 === The aim is to find the duration [hours] of the experiments to maximize the yield of a biological product. If you operate the reactor too long some side re...")
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Part 1 ===
=== Part 1 ===


Description here


<html><div data-datacamp-exercise data-lang="r" data-height="auto">
The aim is to find the duration [hours] of the experiments to maximize the yield of a biological product. If you operate the reactor too long some side reactions start to occur which consume your product. If you operate for too short a time, then you miss out on the opportunity of creating more product.
    <code data-type="sample-code">


center = 36
range = 24


rw.x <- c(24, 48)
<html><div data-datacamp-exercise data-lang="r" data-height="500">
coded.x <- (rw.x - center)/(0.5*range)
    <code data-type="sample-code">
y0 <- c(28, 63)
 
model.0 <- lm(y0 ~ coded.x)
summary(model.0)
 
# What is the interpretation of the
# * slope?
# * intercept
# * why are R2 and SE where they are?
 
# Basic plot of everything so far:
raw_data <- data.frame(coded.x = coded.x, y = y0)
library(ggplot2)
p <- ggplot(data=raw_data, aes(x=coded.x, y=y))  + 
  geom_point(size=5) +
  xlab("Coded value for x_A") +
  scale_x_continuous(breaks=seq(-2, 5, 1)) +
  ylab("Outcome variable") +
  scale_y_continuous(breaks=seq(0, 150, 10)) +
  theme_bw() +
  theme(axis.text=element_text(size=18), legend.position = "none") +
  theme(axis.title=element_text(face="bold", size=14))
p
 
 
# Run experiment at center point: predict it first
rw.x <- c(24, 48, 36)
coded.x <- (rw.x - center)/(0.5*range)
x.test <- data.frame(coded.x=coded.x)
predict(model.0, newdata=x.test)
 
# Expect a predicted value of 45.5 in the output. Actual: 54 and 55. So about
# 10 units difference.
 
# Add the linear fit: through the 2 points
plot_data <- data.frame(coded.x = seq(-2, +5, 0.1))
plot_data$y <- predict(model.0, newdata=plot_data)
p <- p + geom_line(data=plot_data, color="blue", size=1)
p
 
 
# Try fitting a linear model now through all the data points:
rw.x <- c(24, 48, 36, 36)
y1  <- c(28, 63, 55, 54)
coded.x <- (rw.x - center)/(0.5*range)
model.1 <- lm(y1 ~ coded.x)
summary(model.1)
 
# Show the linear fit with the extra data point (center points)
raw_data <- data.frame(coded.x = coded.x, y = y1)
plot_data$y <- predict(model.1, newdata=plot_data)
p <- p + geom_point(aes(x=coded.x, y=y, color='darkgreen', size=5), data=raw_data)
p <- p + geom_line(data=plot_data, color="darkgreen", size=1)
p
 
 
 
# A a quadratic component through all the data points:
rw.x <- c(24, 48, 36, 36)
y1  <- c(28, 63, 55, 54)
coded.x <- (rw.x - center)/(0.5*range)
model.1.quad <- lm(y1 ~ coded.x + I(coded.x^2))
summary(model.1.quad)
 
# Show the quadratic fit between -2 and +2 in coded units
# In real-world units this corresonds to _____ and _____
plot_data <- data.frame(coded.x = seq(-2, +3, 0.1))
plot_data$y <- predict(model.1.quad, newdata=plot_data)
p <- p + geom_line(data=plot_data, color="red", size=1)
p
 
# Try a new point at +2: that is RW = coded * 0.5 * range + center; 60 hours
# y = 66.
y0 <- c(28, 63)
rw.x <- c(24, 48, 36, 36, 60)
y2  <- c(28, 63, 55, 54, 66)
coded.x <- (rw.x - center)/(0.5*range)
model.2.quad <- lm(y2 ~ coded.x + I(coded.x^2))
summary(model.2.quad)


# Predict value first.
# Then run the experiment.
# Plot it again:
DO: add the new point and response here
plot_data <- data.frame(coded.x = seq(-2, +3, 0.1))
plot_data$y <- predict(model.2.quad, newdata=plot_data)
p <- p + geom_line(data=plot_data, color="purple", size=1)
p


     </code>
     </code>
</div></html>
</div></html>

Latest revision as of 04:51, 6 May 2019

Part 1

The aim is to find the duration [hours] of the experiments to maximize the yield of a biological product. If you operate the reactor too long some side reactions start to occur which consume your product. If you operate for too short a time, then you miss out on the opportunity of creating more product.