##for(i in 1:length(lets))
##{
## index <- which(lets[i]==LETTERS)
##data[i,1] <- lf.table$freq[index]
##data[i,2] <- lf.table$points[index]
##data[i,3] <- lf.table$ntiles[index]
## }
test$meantiles <- mean(test[,3])
#test$suminvfreq <- sum(1/test[,1])
test$points <- sum(test[,2])
#test$length <- length(lets)
#temp <- scoreme("hello")
##test$meantiles[i] <- temp$meantiles
##or do this:
#test[5,i] <-temp[1]
#scoreme(test[2,1])
#scoreme(test$word[2])
#test[2,4] <= temp$meantiles
#test$meantiles[i] <= temp$meantiles
#i<- 2
#for(i in 1:nrow(test))
#{
# print(i)
#tmp<= scoreme(i)
#}
setwd("~/Dropbox/courses/5210-2024/Exams/exam1")
pooled <- read.table("pooled.csv", header = T, sep = ",",
colClasses = c("factor","factor","numeric","numeric","numeric","numeric", "factor",
"factor","numeric","numeric","numeric","factor","numeric"))
setwd("~/Dropbox/courses/5210-2024/Exams/exam1")
pooled <- read.table("pooled.csv", header = T, sep = ",",
colClasses = c("factor","factor","numeric","numeric","numeric","numeric", "factor",
"factor","numeric","numeric","numeric","factor","numeric"))
ls()
pooled <- read.table("pooled.csv", header = T, sep = ",",
colClasses = c("factor","factor","numeric","numeric","numeric","numeric", "factor",
"factor","numeric","numeric","numeric","factor","numeric"))
pooled <- read.table("pooled.csv", header = T, sep = ",",
colClasses = c("factor","factor","numeric","numeric","numeric","numeric", "factor",
"factor","numeric","numeric","numeric","factor","numeric"))
# Mean and SD of RT by time-of-day
meanSD <- aggregate(rt ~ tod,
data = pooled,
function(x) c(mean = mean(x, na.rm = TRUE), sd = sd(x, na.rm = TRUE)))
meanSD
# Mean RT by session
rtSession <- aggregate(rt ~ session,
data = pooled,
mean,
na.rm = TRUE)
rtSession
# Mean accuracy by session
accuracySession <- aggregate(corr ~ session,
data = pooled,
mean,
na.rm = TRUE)
accuracySession
# Mean RT across trials within the first session
session1 <- subset(pooled, session == 1)
rtSession1 <- aggregate(rt ~ trial, data = session1, mean, na.rm = TRUE)
rtSession1
# Mean RT by time-of-day and response type
rtResponseTOD <- aggregate(rt ~ tod + resp,
data = pooled,
mean,
na.rm = TRUE)
rtResponseTOD
# Mean accuracy by time-of-day and response type
accuracyResponseTOD <- aggregate(corr ~ tod + resp,
data = pooled,
mean,
na.rm = TRUE)
accuracyResponseTOD
# Define the PlotCombo function
PlotCombo <- function(subnum, session, pooled) {
# Subset the data for the given participant (subnum) and session
subData <- pooled[pooled$subnum == subnum & pooled$session == session, ]
# Check if the subset is empty
if (nrow(subData) == 0) {
# Create an empty plot with "no data available" message
plot(1,
type = "n",
main = paste("No data available for Subject", subnum, "Session", session),
xlab = "",
ylab = "")
return()
}
# Extract time of day (tod) for the subset
valueTOD <- unique(subData$tod)
# Create plot for RT by trial number
plot(subData$trial, subData$rt,
type = "p",
col = "blue",
pch = 16,
main = paste("RT by Trial for Subject", subnum, "Session", session),
xlab = "Trial Number",
ylab = "Response Time (RT) in ms")
# Add labels for the time-of-day
mtext(paste("Time of Day:", valueTOD), side = 3, line = 0.4)
}
# Define the PlotCombo function
PlotCombo <- function(subnum, session, pooled) {
# Subset the data for the given participant (subnum) and session
# subData <- pooled[pooled$subnum == subnum & pooled$session == session, ]
subData <- subset(pooled, pooled$subnum == subnum & pooled$session == session)
# Check if the subset is empty
if (nrow(subData) == 0) {
# Create an empty plot with "no data available" message
plot(1,
type = "n",
main = paste("No data available for Subject", subnum, "Session", session),
xlab = "",
ylab = "")
return()
}
# Extract time of day (tod) for the subset
valueTOD <- unique(subData$tod)
# Create plot for RT by trial number
plot(subData$trial, subData$rt,
type = "p",
col = "blue",
pch = 16,
main = paste("RT by Trial for Subject", subnum, "Session", session),
xlab = "Trial Number",
ylab = "Response Time (RT) in ms")
# Add labels for the time-of-day
mtext(paste("Time of Day:", valueTOD), side = 3, line = 0.4)
}
# Create a PDF to save the plots
pdf("Plots.pdf")
par(mfrow = c(4, 2))
# List of participants to plot
participants <- c(222, 3001, 312)
# Loop through participants and sessions
for (subnum in participants) {
for (session in 1:8) {
PlotCombo(subnum, session, pooled)
#print(paste(subnum,session))
}
}
# Close the PDF file
dev.off()
par(mfrow = c(4, 2))
# List of participants to plot
participants <- c(222, 3001, 312)
# Loop through participants and sessions
for (subnum in participants) {
for (session in 1:8) {
PlotCombo(subnum, session, pooled)
#print(paste(subnum,session))
}
}
par(mfrow = c(4, 2))
# List of participants to plot
participants <- c(222, 3001, 312)
# Loop through participants and sessions
for (subnum in participants) {
for (session in 1:8) {
PlotCombo(subnum, session, pooled)
#print(paste(subnum,session))
}
}
participants
PlotCombo(222,1,pooled )
PlotCombo(222,1,pooled )
subset(pooled,pooled$subnum==222)
dim(subset(pooled,pooled$subnum==222))
dim(pooled)
dim(subset(pooled,pooled$subnum==222 & pooled$session==1))
# Define the PlotCombo function
PlotCombo <- function(subnum, session, pooled) {
# Subset the data for the given participant (subnum) and session
# subData <- pooled[pooled$subnum == subnum & pooled$session == session, ]
subData <- subset(pooled, pooled$subnum == subnum & pooled$session == session)
print(dim(subData))
# Check if the subset is empty
if (nrow(subData) == 0) {
# Create an empty plot with "no data available" message
plot(1,
type = "n",
main = paste("No data available for Subject", subnum, "Session", session),
xlab = "",
ylab = "")
return()
}
# Extract time of day (tod) for the subset
valueTOD <- unique(subData$tod)
# Create plot for RT by trial number
plot(subData$trial, subData$rt,
type = "p",
col = "blue",
pch = 16,
main = paste("RT by Trial for Subject", subnum, "Session", session),
xlab = "Trial Number",
ylab = "Response Time (RT) in ms")
# Add labels for the time-of-day
mtext(paste("Time of Day:", valueTOD), side = 3, line = 0.4)
}
PlotCombo(222, 1, pooled)
pooled
dim(pooled)
subnum <- 222
session <_ 1
session <- 1
pooled$subnum == subnum & pooled$session == session
subset(pooled, pooled$subnum == subnum & pooled$session == session)
subData <- subset(pooled, pooled$subnum == subnum && pooled$session == session)
subData <- subset(pooled, pooled$subnum == subnum & pooled$session == session)
sub <- 222
sess <- 1
subData <- subset(pooled, subnum == sub & session == sess)
dim(subData)
knitr::opts_chunk$set(echo = TRUE, fig.width = 6, fig.height = 4)
x <- c(2, 4, 6, 8)
y <- c(1, 10)
z <- x * y
z
set.seed(1)
scores <- round(rnorm(20, mean = 75, sd = 12), 1)
keep <- scores[scores >= 70 & scores <= 85]
keep
length(keep)
data(ToothGrowth)
table(ToothGrowth$supp, ToothGrowth$dose)
data(trees)
ord <- order(trees$Girth)
ord[1:5]
trees$Girth[ord][1:5]
order(trees$Girth)
order(trees$height)
order(trees$Height)
data(trees)
ord <- order(trees$Height)
ord[1:5]
trees$Girth[ord][1:5]
set.seed(2)
plot(1:10, rnorm(10), pch = 16, cex = 2,
col = c("steelblue", "orange"))
set.seed(10)
rt <- exp(rnorm(200, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt, breaks = 20, col = "gray70",
main = "Simulated response times", xlab = "RT")
hist(log(rt))
boxplot(len ~ dose, data = ToothGrowth,
col = c("gold", "gray70", "tomato"),
xlab = "Dose", ylab = "Tooth length",
main = "ToothGrowth by dose")
boxplot(len ~ dose, data = ToothGrowth,
col = c("gold", "gray70", "tomato"),
xlab = "Dose", ylab = "Tooth length",
main = "ToothGrowth by dose")
data(ChickWeight)
round(tapply(ChickWeight$weight,
list(Diet = ChickWeight$Diet,
Time = ChickWeight$Time),
mean), 1)
plot(trees$Height, trees$Girth, pch = 16,
xlab = "Height", ylab = "Girth",
main = "trees data")
abline(h = mean(trees$Girth), lty = 2, col = "red")
setwd("/home/home/smueller/Dropbox/courses/5210-2026/web-5210/psy5210/Projects/Chapter6")
#pdf("chap7-distributions.pdf",width=9,height=3.5)
par(mfrow=c(1,4))
barplot(c(.5,.5),names=c("heads","tails"),ylim=c(0,1),
main="Coin toss\nProbability Density",col="navy")
barplot(rep(1/6,6),names=1:6,main="Die Roll\nProbability Density",
ylim=c(0,1),col="navy")
plot(-50:50/10,dnorm(-50:50/10),col="navy",lwd=2,type="l", bty="L",
main="Normal Random Variable\nProbability density",ylab="Density",xlab="Sampled value")
plot(-50:50/10,pnorm(-50:50/10),col="navy",lwd=2,type="l",bty="L",
main="Normal Random Variable\nCumulative distribution",ylab="Cumulative distn.",xlab="Sampled value")
#dev.off()
heads <- function(prob=.5,flips=1,repetitions=1)
{
base <- matrix(runif(flips*repetitions)<prob,
ncol=flips,nrow=repetitions)
return(rowSums(base))
}
##only re-run if we haven't done it already.
if(!exists("fair.10"))
{
set.seed(100)
fair.10 <- table(heads(.5,10,100000))
biased.10 <-table(heads(.55,10,100000))
fair.500 <- table(heads(.5,500,100000))
biased.500 <-table(heads(.55,500,100000))
}
par(mfrow=c(1,2))
plot(as.numeric(names(fair.10)),
fair.10,pch=16,cex=.5,yaxt="n",
col="navy",type="o",xlim=c(0,10),bty="L",
ylab="Frequency of each outcome",
main="Fair (.5) vs Biased (.55) coin\n100,000 experiments",
xlab="Number of heads (out of 10)")
points(as.numeric(names(biased.10)),
(biased.10),
pch=16,cex=.5,col="red4", type="o")
abline(v=5)
grid()
axis(2)
plot(as.numeric(names(fair.500)),
fair.500,pch=16,cex=.5,yaxt="n",
col="navy",type="o",xlim=c(200,320),bty="L",
main="Fair (.5) vs Biased (.55) coin\n100,000 experiments",
ylab="Frequency of each outcome",
xlab="Number of heads (out of 500)")
points(as.numeric(names(biased.500)),
(biased.500), pch=16,cex=.5,
col="red4", type="o")
abline(v=250)
grid()
axis(2)
heads()
heads(repetitions=10)
heads(flips=10,repetitions=10)
#pdf("c7-letterbar.pdf",width=9,height=5)
set.seed(100)
data <-   sample(LETTERS[1:10], 1000, replace=T)
barplot(table(data),col="gold",ylab="Number of occurrences",
xlab="Option",
main="Samples from a uniform discrete random variable")
#dev.off()
##discrete uniform:
#pdf("c7-letterbarnon.pdf",width=9,height=5)
set.seed(101)
data <-   sample(LETTERS[1:10],
prob=10:1,1000, replace=T)
barplot(table(data),col="gold",ylab="Number of occurrences",
xlab="Option",
main="Samples from a non-uniform discrete random variable")
#dev.off()
#pdf("c7-uniform.pdf",width=9,height=5)
data <- 10 +runif(1000)*10
par(mfrow=c(1,2))
hist(data,breaks=20,main="Histogram of random data from uniform(10,20)",col="gold")
plot(data,ylim=c(0,30),pch=16,col="gold",cex=1.5,main="Individual sampled points")
grid()
points(data,cex=1.5)
#dev.off()
data <- rnorm(100)*5+3
data2 <- runif(100)
#pdf("chap7-qqplots.pdf",width=8,height=8)
par(mfrow=c(2,2))
plot(sort(data),qnorm(0:99/100),type="l",main="Q-Q plot of \nsampled normal vs. normal",
xlab="Quantiles of data",ylab="Quantiles of distribution")
plot(sort(data),qunif(0:99/100),type="l",main="Q-Q plot of \nsampled normal vs. uniform",
xlab="Quantiles of data",ylab="Quantiles of distribution")
plot(sort(data2),qnorm(0:99/100),type="l",main="Q-Q plot of\nsampled uniform vs. normal",
xlab="Quantiles of data",ylab="Quantiles of distribution")
plot(sort(data2),qunif(0:99/100),type="l",main="Q-Q plot of\nsampled uniform vs. uniform",
xlab="Quantiles of data",ylab="Quantiles of distribution")
#dev.off()
knitr::opts_chunk$set(echo = TRUE, fig.width = 6, fig.height = 4)
set.seed(1)
scores <- round(rnorm(20, mean = 75, sd = 12), 1)
keep <- scores[scores >= 70 & scores <= 85]
keep
length(keep)
set.seed(1)
scores <- round(rnorm(20, mean = 75, sd = 12), 1)
keep <- scores[scores >= 70 & scores <= 85]
keep
table(ChickWeight$Diet,ChickWeight$Time)
data(ToothGrowth)
table(ChickWeight$Diet,ChickWeight$Time)
data(trees)
ord <- order(trees$Height)
ord[1:5]
trees$Girth[ord][1:5]
ord[1:5]
trees$Girth[ord][1:5]
ord[1:5]
trees$Girth[ord][1:5]
ord
trees$Girth[ord]
set.seed(2)
plot(1:10, rnorm(10), pch = 16, cex = 2,
col = c("steelblue", "orange"))
set.seed(10)
rt <- exp(rnorm(200, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt, breaks = 20, col = "gray70",
main = "Simulated response times", xlab = "RT")
set.seed(10)
rt <- exp(rnorm(200, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt)
hist(rt,col="gold")
set.seed(10)
rt <- exp(rnorm(200, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt)
set.seed(11)
rt <- exp(rnorm(200, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt)
set.seed(11)
rt <- exp(rnorm(500, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt)
set.seed(11)
rt <- exp(rnorm(500, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt,breaks=20 )
set.seed(11)
rt <- exp(rnorm(500, mean = 5.5, sd = 0.4))  # simulated RTs
hist(rt,breaks=30 )
boxplot(len ~ dose, data = ToothGrowth,
col = c("gold", "gray70", "tomato"),
xlab = "Dose", ylab = "Tooth length",
main = "ToothGrowth by dose")
data(ChickWeight)
round(tapply(ChickWeight$weight,
list(Diet = ChickWeight$Diet,
Time = ChickWeight$Time),
mean), 1)
plot(trees$Height, trees$Girth, pch = 16,
xlab = "Height", ylab = "Girth",
main = "trees data")
abline(h = mean(trees$Girth), lty = 2, col = "red")
data(InsectSprays)
totals <- tapply(InsectSprays$count, InsectSprays$spray, sum)
barplot(totals, col = heat.colors(6),
main = "Total insect counts by spray",
ylab = "Sum of counts")
totals
data(ChickWeight)
chickentab <- tapply(ChickWeight$weight,
list(Diet = ChickWeight$Diet,
Time = ChickWeight$Time), mean)
round(chickentab,3)
chickentab - chickentab[,1]
matplot(chickentab - chickentab[,1]))
matplot(chickentab - chickentab[,1])
matplot(chickentab )
ct2
data(ChickWeight)
chickentab <- tapply(ChickWeight$weight,
list(Diet = ChickWeight$Diet,
Time = ChickWeight$Time), mean)
ct2 <-chickentab-chickentab[,1]
round(ct2,3)
ct2[,10]
plot(trees$Height, trees$Girth, pch = 16,
xlab = "Height", ylab = "Girth",
main = "trees data")
abline( mean(trees$Girth), 0, lty = 2, col = "red")
?abline
plot(trees$Height, trees$Girth, pch = 16,
xlab = "Height", ylab = "Girth",
main = "trees data")
abline( a=mean(trees$Girth), b=0, lty = 2, col = "red")
data(InsectSprays)
totals <- tapply(InsectSprays$count, InsectSprays$spray, sum)
barplot(totals, col = heat.colors(6),
main = "Total insect counts by spray",
ylab = "Sum of counts")
totals
totals <- aggregate(InsectSprays$count, InsectSprays$spray, sum)
print(paste("the mean is", 100))
print(paste("the mean is", round(123.4567,2))
print(paste("the mean is", round(123.4567,2))
print(paste("the mean is", round(123.4567,2))
print(paste("the mean is", round(123.4567,2))))
print(paste("the mean is", round(123.4567,2))))
print(paste("the mean is", round(123.4567,2)))
ToothGrowth
# your code here
print(paste("the mean is", round(123.4567,2)))
odd <- function(x){x%%2}
odd(3)
odd(10)
odd <- function(x){x%%2 > 0}
odd(10)
odd(11)
odd(-11)
odd(0)
odd <- function(x){x%%2}
odd(1:6)
sum(vals[odd(vals)]))
sum(vals[odd(vals)])
vals[odd(vals)]
odd(vals)
vals <- c(3, 1, 4, 1, 5, 9, 8, 6, 3, 1)
odd(vals)
vals[odd(vals)]
odd <- function(x){x%%2}
odd(1:6)
odd <- function(x){x%%2 >0}
odd(1:6)
vals[odd(vals)]
sum(vals[odd(vals)])
odd <- function(x){x%%2 >0}
odd(1:6)
vals <- c(3, 1, 4, 1, 5, 9, 8, 6, 3, 1)
##the easy way:
sum(vals[odd(vals)])  ##23
##the hard way:
sum <- 0
# your code here
print(sum)
InsectSprays
InsectSprays[1:5,]
?InsectSprays
boxplot(count ~ spray, data = InsectSprays,
xlab = "Type of spray", ylab = "Insect count",
main = "InsectSprays data", varwidth = TRUE, col = "lightgray")
The `InsectSprays` data set includes repeated tests of different sprays A through F, and a 'count' value which indicates how many insects were killed on that , compute the **mean** count for each spray with `tapply` (or `aggregate`). Make a barplot of those means with:
aggregate(InsectSprays$count,list(InsectSprays$spray),mean)
barplot(aggregate(InsectSprays$count,list(InsectSprays$spray),mean))
# broken:
vals <- factor(c("10", "20", "30", "40"))
as.numeric(vals)   # not the values we want
mean(as.numeric(vals))
set.seed(100)
m <- matrix(runif(12), nrow = 3, ncol = 4)
apply(m, 1, mean)   # author wanted column means
mean(ToothGrowth$len[ToothGrowth$supp = "VC"])
