R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Including Plots

You can also embed plots, for example:

#dados<-read.table("dados.txt")
# y<-as.numeric(unlist(dados[1]))
# x<-as.numeric(unlist(dados[2]))
y<-c(14.28,8.80,12.49,9.38,10.89,15.39,13.09,12.35,10.66,8.12,12.61,8.61,10.99,8.65,11.52,7.25,2.63,9.61,20.20,19.68,
     13.46,18.45,18.05,9.76,18.27,14.38,13.27,11.40,12.32,19.06,10.66,13.88,2.53,11.21,13.28,16.79,13.04,10.60,19.41,
     8.56,20.87,9.80,15.33,13.78,11.77,16.65,12.11,14.31,10.38,12.66,15.71,17.66,12.94,12.14,7.11,21.97,9.02,8.90,15.75,11.80,12.52,12.45,18.35,10.81,11.86,9.00,8.54,17.11,17.50,10.74,16.57,16.46,17.24,15.03,14.65,8.66,20.70,8.27,11.99,11.01,13.72,11.15,18.80,21.94,22.35,9.49,11.43,12.84,10.64,14.95,13.29,19.25,7.78,11.37,15.86,6.69,13.34,12.13,14.62,
     7.63,0.51,10.41,11.91,18.88,11.15,9.23,10.65,13.53,16.37,11.45,15.78)

x<-c(15,9,7,4,9,21,11,6,10,7,12,2,6,5,10,3,3,4,20,17,13,30,21,4,15,17,13,10,7,23,7,16,3,3,10,21,5,7,26, 7,22,8,14,11,9,18,10,6,4,13,13,17,13,8,2,33,3,4,25,12,14,6,24,8,12,4,4,14,16,6,16,15,22,12,10,8,21,9, 8,6,7,7,18,23,27,8,8,14,5,14,10,16,8,6,18,6,17,10,8,6,0,8,6,21,9,6,10,11,12,9,15)

plot(jitter(y,6)~x)
regrline<-lm(y~x)
abline(regrline, lwd=3, col='red')

summary(regrline)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.3535 -1.3154  0.0036  1.2405  4.2469 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.86349    0.39863   17.22   <2e-16 ***
## x            0.53327    0.03028   17.61   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.118 on 109 degrees of freedom
## Multiple R-squared:   0.74,  Adjusted R-squared:  0.7376 
## F-statistic: 310.2 on 1 and 109 DF,  p-value: < 2.2e-16
# Plotando gráficos
# Graf1: x versus y
# Graf2: raiz(x) versus y
# Graf3: log(x) versus y



par(mfrow=c(1,3))
# Graf1: x versus y
plot(jitter(y,4)~x, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2,main="Gráfico 1: x versus y")
corr<-round(cor(x,y),dig=3)
text(20,4,paste("Corr:",corr))

# Graf2: raiz(x) versus y
plot(jitter(y,4)~sqrt(x), frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2,main="Gráfico 2: Raiz(x) versus y")
corr<-round(cor(sqrt(x),y),dig=3)
text(4,4,paste("Corr:",corr))


# Graf3: log(x) versus y
plot(jitter(y,4)~log(x), frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2,main="Gráfico 3: Log(x) versus y")
# Ajusta os dados, retirando valor 0(zero), pois não há valor para o log(0).
y2<-y[-101]
x2<-x[-101]

corr<-round(cor(log(x2),y2),dig=3)
text(2,4,paste("Corr:",corr))

# Plotando gráficos
# Graf1: x versus y
# Graf2: x versus raiz(y)
# Graf3: x versus log(y)



par(mfrow=c(1,3))
# Graf1: x versus y
plot(jitter(y,4)~x, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2,main="Gráfico 1: x versus y")
corr<-round(cor(x,y),dig=3)
text(20,4,paste("Corr:",corr))

# Graf2: x versus raiz(y)
plot(jitter(sqrt(y),4)~x, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2,main="Gráfico 2: x versus Raiz(y)")
corr<-round(cor(sqrt(y),x),dig=3)
text(25,1,paste("Corr:",corr))


# Graf3: x versus log(y)
plot(jitter(log(y),4)~x, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2,main="Gráfico 3: x versus Log(y)")
# Ajusta os dados, retirando valor 0(zero), pois não há valor para o log(0).
# y2<-y[-101]
# x2<-x[-101]

corr<-round(cor(log(y),x),dig=3)
text(25,1.5,paste("Corr:",corr))

# Calculando parâmetros e intervalos
# Graf1: x versus y
# Graf2: raiz(x) versus y
# Graf3: log(x) versus y

y<-c(14.28,8.80,12.49,9.38,10.89,15.39,13.09,12.35,10.66,8.12,12.61,8.61,10.99,8.65,11.52,7.25,2.63,9.61,20.20,19.68,
     13.46,18.45,18.05,9.76,18.27,14.38,13.27,11.40,12.32,19.06,10.66,13.88,2.53,11.21,13.28,16.79,13.04,10.60,19.41,
     8.56,20.87,9.80,15.33,13.78,11.77,16.65,12.11,14.31,10.38,12.66,15.71,17.66,12.94,12.14,7.11,21.97,9.02,8.90,15.75,11.80,12.52,12.45,18.35,10.81,11.86,9.00,8.54,17.11,17.50,10.74,16.57,16.46,17.24,15.03,14.65,8.66,20.70,8.27,11.99,11.01,13.72,11.15,18.80,21.94,22.35,9.49,11.43,12.84,10.64,14.95,13.29,19.25,7.78,11.37,15.86,6.69,13.34,12.13,14.62,
     7.63,0.51,10.41,11.91,18.88,11.15,9.23,10.65,13.53,16.37,11.45,15.78)

x<-c(15,9,7,4,9,21,11,6,10,7,12,2,6,5,10,3,3,4,20,17,13,30,21,4,15,17,13,10,7,23,7,16,3,3,10,21,5,7,26, 7,22,8,14,11,9,18,10,6,4,13,13,17,13,8,2,33,3,4,25,12,14,6,24,8,12,4,4,14,16,6,16,15,22,12,10,8,21,9, 8,6,7,7,18,23,27,8,8,14,5,14,10,16,8,6,18,6,17,10,8,6,0,8,6,21,9,6,10,11,12,9,15)

par(mfrow=c(1,3))
# Graf1: x versus y
n<-length(y)
beta1 <- cor(y, x) * sd(y) / sd(x)
beta0 <- mean(y) - beta1 * mean(x)
erros <- y - beta0 - beta1 * x
sigma <- sqrt(sum(erros^2) / (n-2))
ssx <- sum((x - mean(x))^2)
fit<-lm(y~x)
plot(x, y, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2)
abline(fit, lwd = 2)
xVals <- seq(min(x), max(x), by = .01)
yVals <- beta0 + beta1 * xVals
se1 <- sigma * sqrt(1 / n + (xVals - mean(x))^2/ssx)
se2 <- sigma * sqrt(1 + 1 / n + (xVals - mean(x))^2/ssx)
lines(xVals, yVals + 2 * se1)
lines(xVals, yVals - 2 * se1)
lines(xVals, yVals + 2 * se2)
lines(xVals, yVals - 2 * se2)


# Graf2: raiz(x) versus y
x<-c(15,9,7,4,9,21,11,6,10,7,12,2,6,5,10,3,3,4,20,17,13,30,21,4,15,17,13,10,7,23,7,16,3,3,10,21,5,7,26, 7,22,8,14,11,9,18,10,6,4,13,13,17,13,8,2,33,3,4,25,12,14,6,24,8,12,4,4,14,16,6,16,15,22,12,10,8,21,9, 8,6,7,7,18,23,27,8,8,14,5,14,10,16,8,6,18,6,17,10,8,6,0,8,6,21,9,6,10,11,12,9,15)

x<-sqrt(x)


beta1 <- cor(y, x) * sd(y) / sd(x)
beta0 <- mean(y) - beta1 * mean(x)
erros <- y - beta0 - beta1 * x
sigma <- sqrt(sum(erros^2) / (n-2))
ssx <- sum((x - mean(x))^2)
fit<-lm(y~x)
plot(x, y, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2)
abline(fit, lwd = 2)
xVals <- seq(min(x), max(x), by = .01)
yVals <- beta0 + beta1 * xVals
se1 <- sigma * sqrt(1 / n + (xVals - mean(x))^2/ssx)
se2 <- sigma * sqrt(1 + 1 / n + (xVals - mean(x))^2/ssx)
lines(xVals, yVals + 2 * se1)
lines(xVals, yVals - 2 * se1)
lines(xVals, yVals + 2 * se2)
lines(xVals, yVals - 2 * se2)



# Graf3: log(x) versus y

#x<-log(as.numeric(unlist(dados[2])))
x<-c(15,9,7,4,9,21,11,6,10,7,12,2,6,5,10,3,3,4,20,17,13,30,21,4,15,17,13,10,7,23,7,16,3,3,10,21,5,7,26, 7,22,8,14,11,9,18,10,6,4,13,13,17,13,8,2,33,3,4,25,12,14,6,24,8,12,4,4,14,16,6,16,15,22,12,10,8,21,9, 8,6,7,7,18,23,27,8,8,14,5,14,10,16,8,6,18,6,17,10,8,6,0,8,6,21,9,6,10,11,12,9,15)

x<-log(x)

# Ajustando valor zero do vetor, pois não há log(0)
x<-x[-101]
y<-y[-101]
beta1 <- cor(y, x) * sd(y) / sd(x)
beta0 <- mean(y) - beta1 * mean(x)
erros <- y - beta0 - beta1 * x
sigma <- sqrt(sum(erros^2) / (n-2))
ssx <- sum((x - mean(x))^2)
fit<-lm(y~x)
plot(x, y, frame=FALSE,xlab="Tamanho lote de produção",ylab="Tempo de produção",pch=21,col="black", bg="lightblue", cex=2)
abline(fit, lwd = 2)
xVals <- seq(min(x), max(x), by = .01)
yVals <- beta0 + beta1 * xVals
se1 <- sigma * sqrt(1 / n + (xVals - mean(x))^2/ssx)
se2 <- sigma * sqrt(1 + 1 / n + (xVals - mean(x))^2/ssx)
lines(xVals, yVals + 2 * se1)
lines(xVals, yVals - 2 * se1)
lines(xVals, yVals + 2 * se2)
lines(xVals, yVals - 2 * se2)

#  Mensurando  a força da relação linear entre as variáveis 




# beta1 <- cor(y, x) * sd(y) / sd(x)
# beta0 <- mean(y) - beta1 * mean(x)
# e <- y - beta0 - beta1 * x
# sigma <- sqrt(sum(e^2) / (n-2))
# ssx <- sum((x - mean(x))^2)
# seBeta0 <- (1 / n + mean(x) ^ 2 / ssx) ^ .5 * sigma
# seBeta1 <- sigma / sqrt(ssx)
# tBeta0 <- beta0 / seBeta0; tBeta1 <- beta1 / seBeta1
# pBeta0 <- 2 * pt(abs(tBeta0), df = n - 2, lower.tail = FALSE)
# pBeta1 <- 2 * pt(abs(tBeta1), df = n - 2, lower.tail = FALSE)
# coefTable <- rbind(c(beta0, seBeta0, tBeta0, pBeta0), c(beta1, seBeta1, tBeta1, pBeta1))
# colnames(coefTable) <- c("Estimate", "Std. Error", "t value", "P(>|t|)")
# rownames(coefTable) <- c("(Intercept)", "x")




# Verificando pressupostos

# We'll verify these claims now. We've defined for you two R functions, est and sqe. Both take two inputs, a slope and # an intercept. The function est calculates a child's height (y-coordinate) using the line defined by the two parameters,(slope and intercept), and the parents' heights in the Galton data as x-coordinates.

#  The function sqe calculates the sum of the squared residuals, the differences between the actual children's heights # and the estimated heights specified by the line defined by the given parameters (slope and intercept).  R provides # # the function deviance to do exactly this using a fitted model (e.g., fit) as its argument. However, we provide sqe # # because we'll use it to test regression lines different from fit.

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.