Download summary to MS Word - r

This is what i've written for my task:
1:12
12:1
rep(1:2,12)
rep("Red",6)
c(rep("Red",6),rep("Blue",6))
sample(1:12,4)
sample(1:12)
sample(c(rep("Red",6),rep("Blue",6)))
alder <- data$alder
respons <- data$respons
plot(alder, respons)
behandling <- rep(c("FB2M","Placebo"), each = 15)
behandling <- factor(behandling)
data$behandling <- behandling
boxplot(respons ~ behandling, data=data)
data$randomisering <- sample(data$behandling)
boxplot(respons ~ data$randomisering, data=data)
Menn <- data[data$kjonn == 'Mann',]
table(Menn$randomisering)
..but when i try to file->compile report to MS Word, this is what happens:
Quitting from lines 3-33 (Oving3.spin.Rmd)
Error in data$alder : object of type 'closure' is not subsettable
Calls: <Anonymous> ... withVisible -> eval_with_user_handlers -> eval -> eval
Execution halted
Can someone help me?

Related

Prediction using a refund::pfr model

I'm trying to perform prediction of a testing set using a refund::pfr model. My model is scalar-on-function and on scalar and looks like the following example. When I'm using lf(cca) for the functional predictor I get the predictions without problem.
data(DTI)
DTI_train <- DTI[DTI$visit==1 & complete.cases(DTI),]
DTI_train <- DTI_train[1:60, ]
DTI_test <- DTI[DTI$visit==1 & complete.cases(DTI),]
DTI_test <- DTI_test[61:66, ]
fit_lf <- pfr(pasat ~ sex + case + lf(cca), data = DTI_train)
predict(fit_lf, newdata = as.list(DTI_test))
However, when I'm using fpc(cca) I'm getting the following error.
fit_fpc <- pfr(pasat ~ sex + case + fpc(cca), data = DTI_train)
predict(fit_fpc, newdata = as.list(DTI_test))
Error in eval(predvars, data, env) : object 'X.tmat' not found
Calls: predict ... eval -> model.frame -> model.frame.default -> eval -> eval
In addition: Warning message:
In (function (object, newdata, type = "link", se.fit = FALSE, terms = NULL, :
not all required variables have been supplied in newdata!
Calls: predict -> predict.pfr -> eval -> eval -> <Anonymous>
I have tried to include the X.tmat of the newdata by including in the list of newdata the fpc(newdata) but I'm getting another type of problem.
new_fit <- as.list(DTI_test)
new_fit$cca <- fpc(DTI_test[,'cca' ])
predict(fit_fpc, newdata = new_fit)
Error in predict.pfr(fit_fpc, newdata = new_fit) :
length(unique(sapply(newdata, function(x) ifelse(is.matrix(x), .... is not TRUE
Calls: predict -> predict.pfr -> stopifnot
Any idea of how to perform predictions with fpc on my model?
Thanks

code can be progress but shown has error when i convert html in r

I would like to convert my r file to HTML, but it always turns this error even if my code is good to process.
error
Quitting from lines 71-90 (HW1.Rmd)
Error in eval(quote(list(...)), env) : object 'MAD' not found
Calls: <Anonymous> ... [.data.frame -> order -> standardGeneric -> eval -> eval -> eval
Execution halted
error part code
ss <- data.frame(
MED = apply(exprs(ESET),1,median),
MAD = apply(exprs(ESET),1,mad)
)
sort(ss$MAD, decreasing = TRUE)[1:101]
highlight_df <- ss %>%
filter(MAD>3.800260)
ss %>%
ggplot(aes(x=MED,y=MAD)) +
geom_point(pch=20) +
geom_point(data=highlight_df,
aes(x=MED,y=MAD,color = top100),
color='red',
size=3)
top <- ss[order(-MAD,-MED),]
top[1:10,]

How to fix an error of "objet de type 'closure' non indiçable" in R

I am trying to do a kNN test on a data set composed of numerical and catagorical data.
I need to convert my catagorical data into numerical so I wrote the following code
summary(factor(data$gender))
data$gender <- as.numeric(data$gender == 'Male')
data$Partner <- as.numeric(data$Partner == 'No')
data$Dependents <- as.numeric(data$Dependents == 'No')
data$PhoneService <- as.numeric(data$PhoneService == 'No')
data$PaperlessBilling <- as.numeric(data$PaperlessBilling == 'No')
summary(data[c("gender","Partner","Dependents","PhoneService","PaperlessBilling")])
In the execution everything works just fine, however when I try to knit the code into HTML I get the following error
Quitting from lines 32-39 (HW-kNN-MariamJallouli_Numercial_Categorical.Rmd)
Error in data$gender : objet de type 'closure' non indiçable
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> summary -> factor

Using output of summarise() in R

How do I use the output of the summarise() in R. I know it creates a table, but I can't seem to use it for further calculations.
I'm trying to calculate the margin of error for a sample proportion.
us_pro <- us12 %>%
summarise(us_prop=sum(response == "atheist")/sum(response == "atheist" | response == "non-atheist"), n=n()) %>%
as.data.frame()
us_pro %>%
me_perc=qnorm(0.975)*sqrt(us_prop*(1-us_prop)/n)
The ME calculation errors out with
Error in eval(expr, envir, enclos) : object 'us_prop' not found Calls:
... handle -> withCallingHandlers -> withVisible -> eval
-> eval Execution halted
I'm guessing it's a very basic error, but i can't understand what

aggregate function fails in knitr but succeeds in console

I have the following R code:
cleanData <- aggregate(x = stormData[c("FATALITIES", "INJURIES")],
by = list(tolower(stormData$EVTYPE)), sum)
It runs fine in the R Studio console, but when I copy/paste this in a chunk in knitr it generates an error:
Error in `$<-.data.frame`(`*tmp*`, "HARM", value = c(15, 0, 2, 2, 2, 6, :
replacement has 902297 rows, data has 898
Calls: <Anonymous> ... withVisible -> eval -> eval -> $<- -> $<-.data.frame
I'm not sure why this is happening.

Resources