程序代写代做代考 1

1
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) ## flkrt1 flkrt6 flkrt11 flkrt16 ## N 27.00 27.00 27.00 27.00 ## Median 5.00 5.00 2.00 4.00 ## Minimum 1.00 2.00 1.00 1.00 ## Maximum 5.00 5.00 5.00 5.00 ## Range 4.00 3.00 4.00 4.00 ## Mean 4.26 4.59 2.70 3.59 ## StdDev 1.29 0.75 1.49 1.34 library(psych) apply(dat, 2, describe) ## $flkrt1 ## vars n mean sd median trimmed mad min max range skew kurtosis se ## X1 1 27 4.26 1.29 5 4.48 0 1 5 4 -1.51 0.84 0.25 ## ## $flkrt6 ## vars n mean sd median trimmed mad min max range skew kurtosis se ## X1 1 27 4.59 0.75 5 4.74 0 2 5 3 -1.9 3.28 0.14 ## ## $flkrt11 ## vars n mean sd median trimmed mad min max range skew kurtosis se ## X1 1 27 2.7 1.49 2 2.65 1.48 1 5 4 0.22 -1.51 0.29 ## ## $flkrt16 ## vars n mean sd median trimmed mad min max range skew kurtosis se ## X1 1 27 3.59 1.34 4 3.7 1.48 1 5 4 -0.84 -0.51 0.26 The results are consistent with the resluts of describe() function in the ‘psych’ package. cor(dat, method='pearson') ## flkrt1 flkrt6 flkrt11 flkrt16 ## flkrt1 1.00000000 -0.12572644 0.04157516 0.2644271 ## flkrt6 -0.12572644 1.00000000 0.09475102 -0.0954854 ## flkrt11 0.04157516 0.09475102 1.00000000 0.1108746 ## flkrt16 0.26442705 -0.09548540 0.11087460 1.0000000 cor(dat, method='spearman') ## flkrt1 flkrt6 flkrt11 flkrt16 ## flkrt1 1.00000000 -0.02140393 0.06571209 0.18373586 ## flkrt6 -0.02140393 1.00000000 0.18412218 -0.08152842 ## flkrt11 0.06571209 0.18412218 1.00000000 0.06604614 ## flkrt16 0.18373586 -0.08152842 0.06604614 1.00000000 cor(dat, method='kendall') ## flkrt1 flkrt6 flkrt11 flkrt16 ## flkrt1 1.00000000 -0.02259130 0.06457364 0.16551869 ## flkrt6 -0.02259130 1.00000000 0.16168395 -0.06720602 ## flkrt11 0.06457364 0.16168395 1.00000000 0.05122606 ## flkrt16 0.16551869 -0.06720602 0.05122606 1.00000000 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 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) ## ## Call: ## lm(formula = dep4.n ~ dep1.n + dep2.n + dep3.n) ## ## Coefficients: ## (Intercept) dep1.n dep2.n dep3.n ## 6.278e-16 -8.922e-02 4.526e-01 5.711e-01 lm(scale(potroy$dep4) ~ scale(potroy$dep1) + scale(potroy$dep2) + scale(potroy$dep3)) ## ## Call: ## lm(formula = scale(potroy$dep4) ~ scale(potroy$dep1) + scale(potroy$dep2) + ## scale(potroy$dep3)) ## ## Coefficients: ## (Intercept) scale(potroy$dep1) scale(potroy$dep2) ## 6.278e-16 -8.922e-02 4.526e-01 ## scale(potroy$dep3) ## 5.711e-01 3 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) ## id food_type flkrt ## 1 1 1 5 ## 2 2 1 5 ## 3 3 1 5 ## 4 4 1 5 ## 5 5 1 5 ## 6 6 1 5 ## 7 7 1 1 ## 8 8 1 5 ## 9 9 1 4 ## 10 10 1 5 ## 11 11 1 1 ## 12 12 1 2 ## 13 13 1 5 ## 14 14 1 4 ## 15 15 1 2 ## 16 16 1 5 ## 17 17 1 5 ## 18 18 1 5 ## 19 19 1 4 ## 20 20 1 5 ## 21 21 1 3 ## 22 22 1 4 ## 23 23 1 5 ## 24 24 1 5 ## 25 25 1 5 ## 26 26 1 5 ## 27 27 1 5 ## 28 1 1 2 ## 29 2 1 5 ## 30 3 1 5 mean(longdat$flkrt[1:135]) ## [1] 3.762963 mean(longdat$flkrt[1:135+135]) ## [1] 4.111111 mean(longdat$flkrt[1:135+135*2]) ## [1] 2.42963 mean(longdat$flkrt[1:135+135*3]) ## [1] 3.348148 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. head(longdat[order(longdat$id), ], 20) ## id food_type flkrt ## 1 1 1 5 ## 28 1 1 2 ## 55 1 1 2 ## 82 1 1 1 ## 109 1 1 1 ## 136 1 2 5 ## 163 1 2 5 ## 190 1 2 5 ## 217 1 2 5 ## 244 1 2 5 ## 271 1 3 1 ## 298 1 3 1 ## 325 1 3 1 ## 352 1 3 1 ## 379 1 3 1 ## 406 1 4 5 ## 433 1 4 5 ## 460 1 4 3 ## 487 1 4 3 ## 514 1 4 3 4 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) ## [1] 1 3 6 11 15 18 24 28 31 36 43 50 59 67 Second <- c(2, 4, 5, 8, 7, 10, 10, 11, 11, 14, 17, 18, 21, 24) runningSum(Second) ## [1] 2 6 11 19 26 36 46 57 68 82 99 117 138 162 runningSum(Second) - runningSum(First) ## [1] 1 3 5 8 11 18 22 29 37 46 56 67 79 95 The differences are positive.