初心者のための R および RjpWiki に関する質問コーナー
以下の 4 つの「●項目」のどれかをクリック!!
● 新規投稿欄: --- その前に,「投稿における注意事項」を読んでから!
● このページの目次 --- 質問への回答・コメントの参照
● 最新のスレッド --- 最も最近に書き込まれた「親」記事(最新のコメントが付いた記事ではありません)
● Q&A (初級者コース) 過去の目次 --- 今までの 17 個の書庫の参照
参照は,個々の項目をクリック(質問への回答・コメントの参照も,個々の項目をクリック)
●● 新規投稿 ●●
まずは,「投稿における注意事項」を読んで,それに従ってください。
これに従わない場合には,回答が得られにくい場合があります。
show (2020-03-10 (火) 12:58:28)
ccf(rw,rw2)と入力した場合、どちらを基準にしてlagを取っているのでしょうか?
私は誰でしょう? (2020-03-01 (日) 21:20:52)
今まで,最新の R にするために(新しもの好きなので)
あ,見ての通り,Mac ユーザです。
curl https://mac.r-project.org/el-capitan/R-3.6-branch/R-3.6-branch-el-capitan-sa-x86_64.tar.gz | sudo tar fvxz - -C /
をやっていたのですが,2 ヶ月ほど前から,実行結果が思わしくなくなっています。
つまるところ,件の URL はアクセス不可ということのようで,アップデートは出来ません。
念の為
http://mac.r-project.org/el-capitan/R-3.6-branch/R-3.6-branch-el-capitan-signed.pkg
を使えばアップデートは出来ます。
curl -O http://mac.r-project.org/el-capitan/R-3.6-branch/R-3.6-branch-el-capitan-signed.pkg sudo installer -pkg ./R-3.6-branch-el-capitan-signed.pkg -target /とかでお茶を濁しては... -- 何処の誰かは知らないけれど 2020-03-02 (月) 16:03:50
Rビギナー (2020-02-26 (水) 10:12:45)
お世話になります.
下記のように1~3まで連番のファイルを読み込みたいのですが,例えば2番のファイルが無い場合にそのまま1,3のファイルを読み込んでプログラムが止まらないようにするにはどうしたらいいでしょうか.
try関数で可能かと思いやってみたのですが,だめでした.
ご教示ください.
よろしくお願いいたします.
==ここからpoint <- 1:3 # for文を回す数を設定 for (i in point) { mypath <- file.path(getwd(), paste0(i,"t2-1.csv")) df <- assign(paste0("data",i), read.csv(mypath, header = T)) df <- data.frame(df) df head(df) df$X <- strptime(df$X,"%Y/%m/%d %H:%M'%S") head(df) df$X <- as.POSIXlt(df$X,"%Y/%m/%d %H:%M:%S",tz="GMT0") head(df) #https://qiita.com/tktz/items/733c37b1d6102ae52120 library(xts) df$X <- align.time(df$X-10*60/2,10*60) head(df) date = paste0("date",i) Temp = paste0("Temp",i) RH = paste0("RH",i) colnames(df) <- c(date,Temp,RH) head(df) mypath <- file.path(getwd(), paste0(i,"t1.csv")) write.csv(df,mypath) }
mypath <- file.path(getwd(), paste0(i,"t2-1.csv")) if (! file.exists(mypath)) next
files = list.files(getwd(), pattern="[0-9]*t2-1.csv") for (f in files) { # describe what you want to do }
マリン (2020-02-23 (日) 10:13:04)
2つの時系列のデータ(約80個)の相関(どのくらい類似性があるか)を
見たいのですが、方法を教えてください。
(2020-02-14 (金) 14:34:30)
Book1というcsvファイルのデータを用いてGLMMによる解析を行ったのですが、固定効果の値を用いた回帰線がうまく引けません。
csvファイルおよび解析を行ったスクリプトをBook1GLMMというファイル名でアップロードしましたので、ご教授いただけますと幸いです。
よろしくお願いいたします。
data <- read.csv("Book1.csv") data # update.packages # install.packages("lme4") library(lme4) par(family = "HiraKakuProN-W3") model1 <- glmer(POMN ~ log(siltclayN) + (1 | Place), family = Gamma(link = "log"), data = data) summary(model1) place = c("HA", "TN", "OG", "YN", "KG", "WA", "AS") # データフレームではこちらを使っているのでこれでなきゃだめ place2 = c("半田山", "田野", "小川", "与那", "上賀茂", "西粟倉", "足寄") # 凡例用には別のものを用意する pch = c(25, 18, 17, 16, 15, 9, 8) col = c("black", "red", "cadetblue", "magenta", "brown", "coral", "darkorchid") plot(data$siltclayN, data$POMN, pch = rep(pch, each = 5), col = rep(col, each = 5), cex = 3, cex.axis = 2) legend("bottomright", legend = place2, pch = pch, col = col, cex = 2) xx <- seq(min(data$siltclayN), max(data$siltclayN), length = 100) # yy <- exp(1.1136 + 0.4651 * xx) # そもそも間違い # lines(xx, yy, lwd = 2) for (i in seq_along(place)) { # 7 本の曲線を引くんでしょ? newdata = data.frame(siltclayN=xx, Place=rep(place[i], 100)) yy = predict(model1, newdata=newdata, type="response") lines(xx, yy, col=col[i]) }
(2020-02-06 (木) 21:14:35)
GLMMなどで、目的変数に用いるデータが連続値で0以上であればガンマ分布を用いると統計の教科書等には書いてあるのですが、連続値で0以上(データの最小値は0)のデータでガンマ分布を用いて解析しようとすると
「non-positive values not allowed for the 'gamma' family」というエラーが出ます。
また、同じデータで線形混合効果モデルなら結果は出るのですが、「boundary (singular) fit: see ?isSingular」という警告?が出るせいなのかランダム効果を考慮した回帰線が一本しか出ません。
解決策などありましたら、お教えいただけますと幸いです。
> library(lme4) > d = read.csv("data.csv") > model1 <- glmer(A ~ log(B) + (1|Place),f amily=Gamma(link="log"),data=d) eval(family$initialize, rho) でエラー: non-positive values not allowed for the 'gamma' familyusing d2
> d2 = d[d$A > 0, ] > model1-2 <- glmer(A~log(B) + (1|Place), family=Gamma(link="log"), data=d2) > model1-2no error happend.
model2<-lmer(NitMAOMrecover~MAOMCN+(1|Place),data=example) summary(model2) place=c("HA","TN","OG","YN","KG","WA","AS") pch=c(25,18,17,16,15,9,8) col=c("black","red","cadetblue","magenta","brown","coral","darkorchid") plot(example$B,example$A,pch=rep(pch,each=5),col=rep(col,each=5),cex=3,cex.axis=2) legend("bottomright",legend=place,pch=pch,col=col,cex=1.5) xx<-seq(min(example$B),max(example$B),length=1000) for(i in seq_along(place)){ yy<-predict(model2,newdata=data.frame(B=xx,Place=rep(place[i],1000)),type="response") lines(xx,yy,col=col[i],lwd=2)}
ra (2020-01-27 (月) 11:58:45)
R初心者です。
GLMMで解析を行った後、散布図に回帰線を引きたいのですが、predict関数を読み込ませようとすると(オブジェクト"Place"がありません)というエラーが出ます(Placeはランダム効果です)。このエラーを解消し、回帰線を引くにはどうすれば良いでしょうか?
入力したコマンドを載せておきます。
よろしくお願いいたします。
model1<-glmer(POMN~log(siltclayN)+(1|Place),family=Gamma(link="log"),data=data)
plot(data$siltclayN,data$POMN,pch=c(25,18,17,16,15,9,8),cex=3,cex.axis=2)
legend("bottomright",legend=c("HA","TN","OG","YN","KG","WA","AS"),pch=c(25,18,17,16,15,9,8),cex=2)
xx<-seq(min(data$siltclayN),max(data$siltclayN),length=100)
yy<-predict(model1,newdata=data.frame(siltclayN=xx),type="response")
lines(xx,yy,lwd=2)
POMN <- c(data$POMN) siltclayN <- c(data$siltclayN + Place) dataframe <- data.frame(POMN, siltclayN) model1 <- glmer(POMN~log(siltclayN) + (1|Place1), family=Gamma(link="log"), data=dataframe) plot(dataframe$siltclayN, dataframe$POMN, pch=c(25,18,17,16,15,9,8), cex=3, cex.axis=2) legend("bottomright", legend=c("HA","TN","OG","YN","KG","WA","AS"), pch=c(25,18,17,16,15,9,8),cex=2) xx <- seq(min(dataframe$siltclayN), max(dataframe$siltclayN), length=100) yy <- predict(model1, newdata=data.frame(siltclayN=xx), type="response")-- 2020-01-27 (月) 23:14:23
> df = data.frame(x=c(3,2,1,3,5), f=factor(c(1,2,2,3,3)), y=c(2,4,7,5,6)) > fit = lm(y ~ x + f, data=df) # f may not be a factor > predict(fit) # this prediction uses data-frame df 1 2 3 4 5 2.0 5.4 5.6 5.7 5.3if you predict with another data-frame, the data-frame must contain 'x' and 'f' following newdata contains 'x' and 'f'. first 5 elements are same in df(df$x, df$f), last 3 elements is exactly new data
> newdata = data.frame(x=c(3,2,1,3,5, 2,3,5), f=factor(c(1,2,2,3,3, 1,2,2))) > predict(fit, newdata) 1 2 3 4 5 6 7 8 2.0 5.4 5.6 5.7 5.3 2.2 5.2 4.8 # first five values are identical to previous prediction. last 3 values are new prediction.do you understand?? -- 2020-01-28 (火) 16:28:06
siltclayN <- c(data$siltclayN + Place) dataframe <- data.frame(POMN, siltclayN)if Place is a factor variable you cant add to data$siltclayN. 'data$siltclayN + Place' cause an error. -- 2020-01-29 (水) 14:47:36
library(lme4) data = read.csv("Book1.csv") model1 <- glmer(POMN ~ log(siltclayN) + (1 | Place), family = Gamma(link = "log"), data = data) summary(model1) place = c("HA", "TN", "OG", "YN", "KG", "WA", "AS") pch = c(25, 18, 17, 16, 15, 9, 8) col = c("black", "red", "cadetblue", "magenta", "brown", "coral", "darkorchid") plot(data$siltclayN, data$POMN, pch = rep(pch, each = 5), col = rep(col, each = 5)) # , cex=3, cex.axis=2) legend("bottomright", legend = place, pch = pch, col = col) # ,cex=2) xx <- seq(min(data$siltclayN), max(data$siltclayN), length = 100) for (i in seq_along(place)) { yy <- predict(model1, newdata = data.frame(siltclayN = xx, Place = rep(place[i], 100)), type = "response") lines(xx, yy, col = col[i]) }
(2020-01-26 (日) 22:45:01)
半角・全角が混在する文字列からバイト数を指定して、部分文字列を抽出したいのですが、どのような関数がありますでしょうか。
例えば
x <- "あaいbうc"
という文字列から3-5byte目を抽出したいです。
("aい"という部分文字列を抽出したいです。)
substringは文字数を指定する関数のため、byte単位で抽出が不可能なようです。
お手数ですがご教示お願いいたします。
> x <- "あaいbうc" > charToRaw(x) [1] e3 81 82 61 e3 81 84 62 e3 81 86 63「あ」は3バイト文字なので途中で切られて、3-5byte目は「82 61 e3」です。これは「あ」の3バイト目、1バイト文字の「a」、「い」の1バイト目です。
> x <- "あaいbうc" # UTF-8: あ == \xe3\x81\x82 > xx = iconv(x, "utf-8", "euc-jp") > xx [1] "\\xa4\\xa2a\\xa4\\xa4b\\xa4\\xa6c" # EUC-JP: あ == \xa4\xa2 > Encoding(xx) = "bytes" > iconv(substr(xx, 3, 5), "euc-jp", "utf-8") [1] "aい"cp932 uses 2 bytes for 1 japanese character, too
> x <- "あaいbうc" > yy = iconv(x, "utf-8", "cp932") > yy [1] "\x82\xa0a\x82\xa2b\x82\xa4c" # CP932: あ == \x82\xa0 > Encoding(yy) = "bytes" > iconv(substr(yy, 3, 5), "cp932", "utf-8") [1] "aい"do you understand? -- 2020-01-28 (火) 11:38:51
TY (2020-01-07 (火) 15:59:04)
シミュレーションで同じ関数を繰り返し(10^5~10^6回程度)実行する場面を想定しています.
例えば,以下のsample_dataでREP毎にlm(y~x)を実行したいです.
for文で実行することはできるのですが,一般にapply系関数の方が実行が速いと聞き,
探しています.apply系関数に関する知識は,tapply関数にmean関数を適用できる,
tapply(sample_data$y, sample_data$REP,mean)
程度のレベルです.
【sample_data】y x REP1 133 3.9 1
2 81 -2.3 1
3 104 -1.2 1
4 89 0.7 1
5 142 4.0 1
6 74 -2.9 1
7 140 3.9 1
8 170 4.4 1
9 128 1.6 1
10 107 1.2 1
11 49 -4.3 2
12 79 -2.9 2
13 77 -3.2 2
14 125 1.8 2
15 82 -1.1 2
16 132 2.6 2
17 99 -0.0 2
18 140 2.1 2
19 140 4.9 2
20 88 -1.1 2