—
title: ”
output:
word_document: default
pdf_document: default
—
# 1
“`{r, message=FALSE, warning=FALSE}
fLikert <- read.table('fLikert.dat.txt', head=T)
dat <- fLikert[seq(1, 20, by=5)]
descripstat <- function(x){
x <- sort(x)
N <- length(x)
Med <- ifelse(N %% 2 == 1, x[(N+1)/2], x[N/2]/2 + x[N/2+1]/2)
Max <- x[N]
Min <- x[1]
Range <- Max - Min
Mean <- round(sum(x) / N, 2)
Std <- round(sqrt(sum((x - Mean)^2) / (N-1)), 2)
return(c(N=N, Median=Med, Minimum=Min, Maximum=Max, Range=Range, Mean=Mean, StdDev=Std))
}
apply(dat, 2, descripstat)
library(psych)
apply(dat, 2, describe)
```
The results are consistent with the resluts of describe() function in the 'psych' package.
```{r}
cor(dat, method='pearson')
cor(dat, method='spearman')
cor(dat, method='kendall')
```
It does not matter here. The difference between the two correlation coefficients is not significant.
The difference between the median and the mean is more significant.
# 2
```{r}
potroy <- read.table('potroy.dat.txt', head=T)
dep4.n <- (potroy$dep4 - mean(potroy$dep4)) / sd(potroy$dep4)
dep1.n <- (potroy$dep1 - mean(potroy$dep1)) / sd(potroy$dep1)
dep2.n <- (potroy$dep2 - mean(potroy$dep2)) / sd(potroy$dep2)
dep3.n <- (potroy$dep3 - mean(potroy$dep3)) / sd(potroy$dep3)
lm(dep4.n ~ dep1.n + dep2.n + dep3.n)
lm(scale(potroy$dep4) ~ scale(potroy$dep1) + scale(potroy$dep2) + scale(potroy$dep3))
```
# 3
```{r}
fLikert <- read.table('fLikert.dat.txt', head=T)
id <- rep(1:27, 20)
food_type <- rep(1:4, each=nrow(fLikert)*5)
flkrt <- unlist(fLikert[,1:20])
longdat <- data.frame(id=id, food_type=food_type, flkrt=flkrt)
head(longdat, 30)
mean(longdat$flkrt[1:135])
mean(longdat$flkrt[1:135+135])
mean(longdat$flkrt[1:135+135*2])
mean(longdat$flkrt[1:135+135*3])
```
The means for the four different food types are 3.76, 4.11, 2.43 and 3.35, respectively. The smallest is Challenging food, and the largest is Fast food.
```{r}
head(longdat[order(longdat$id), ], 20)
```
# 4
```{r}
runningSum <- function(x){
res <- x[1]
for(i in 2:length(x)) res[i] <- res[i-1] + x[i]
return(res)
}
First <- c(1, 2, 3, 5, 4, 3, 6, 4, 3, 5, 7, 7, 9, 8)
runningSum(First)
Second <- c(2, 4, 5, 8, 7, 10, 10, 11, 11, 14, 17, 18, 21, 24)
runningSum(Second)
runningSum(Second) - runningSum(First)
```
The differences are positive.